Difference between revisions of "OpasnetUtils/Drafts"

From Testiwiki
Jump to: navigation, search
(Answer: function timepoints moved from Building model)
(Answer: ana2ova added)
Line 15: Line 15:
 
  objects.latest("Op_en6007", code_name = "answer")
 
  objects.latest("Op_en6007", code_name = "answer")
  
<rcode name="answer" embed=1>
+
<rcode name="answer" embed=0>
  
 
library(OpasnetUtils)
 
library(OpasnetUtils)
Line 689: Line 689:
 
}
 
}
  
 +
# ana2ova takes in variable tables from Analytica (produced with Copy Table). The output is a data.frame in long format.
 +
ana2ova <- function(dat)
 +
{
 +
i <- 1 # Number of indices
 +
cols <- character() # Names of indices
 +
locs <- list() # Vectors of locations for each index.
 +
out <- data.frame() # output
 +
repeat{ # Find out cols and locs from dat.
 +
temp <- strsplit(dat[i + 1], split = "\t")[[1]]
 +
cols[i] <- temp[1]
 +
if(length(temp) == 1) break
 +
locs[[i]] <- temp[2:length(temp)]
 +
i <- i + 1
 +
}
 +
locs[[i]] <- NA # The innermost index should have its place in locs even if the locations are not yet known.
 +
 +
inner <- (1:length(dat))[dat %in% cols[i]] # Places where the innermost index is mentioned (the data starts from the next row).
 +
if(!all(dat[inner - 1] == dat[i]) & i > 1) stop("Structure incorrect\n")
 +
if(length(inner) == 1) # len is the length of the innermost index.
 +
{
 +
len <- length(dat) - i - 1
 +
} else {
 +
len <- inner[2] - inner[1] - i
 +
}
 +
 +
for(k in inner) # Go through each table with the inner and second most inner indices.
 +
{
 +
ins <- strsplit(dat[(k + 1):(k + len)], split = "\t") # Make a data.frame.
 +
ins <- data.frame(matrix(unlist(ins), nrow = len, byrow = TRUE))
 +
 +
if(i == 1) cn <- "value" else cn <- locs[[i - 1]]
 +
colnames(ins) <- c(cols[i], cn) # Give location names to columns
 +
if(i > 1) ins <- melt(ins, id.vars = cols[i], variable.name = cols[i - 1]) # If several columns.
 +
if(i > 2) # If there are several tables.
 +
{
 +
uplocs <- data.frame(Removethis = 0)
 +
for(l in 1:(i - 2))
 +
{
 +
uplocs[[cols[l]]] <- strsplit(dat[k - i + l], split = "\t")[[1]][2]
 +
}
 +
out <- rbind(out, cbind(uplocs, ins)) # Collects all tables into a data.frame.
 +
} else {
 +
out <- ins
 +
}
 +
}
 +
out$Removethis <- NULL
 +
colnames(out)[colnames(out) == "value"] <- "Result"
 +
return(out)
 +
}
  
 
objects.store(ograph, collapsemarg, MyPointKML, ova2spat, MyRmap, MyPlotKML, truncateIndex, findrest,  
 
objects.store(ograph, collapsemarg, MyPointKML, ova2spat, MyRmap, MyPlotKML, truncateIndex, findrest,  
unkeep, timing, makeTimeline, timepoints)
+
unkeep, timing, makeTimeline, timepoints, ana2ova)
  
 
cat(paste("The following objects are stored: ograph, collapsemarg, MyPointKML, ova2spat, MyRmap, MyPlotKML,",
 
cat(paste("The following objects are stored: ograph, collapsemarg, MyPointKML, ova2spat, MyRmap, MyPlotKML,",
"truncateIndex, findrest, unkeep, timing, makeTimeline, timepoints.\n"))
+
"truncateIndex, findrest, unkeep, timing, makeTimeline, timepoints, ana2ova.\n"))
 
</rcode>
 
</rcode>
  

Revision as of 08:23, 23 February 2014



Question

Which functions are so useful that they should be taken into OpasnetUtils package? This page contains draft function which will be included when they are good enough and found important.

Answer

Call the objects stored by this code from another rode with this command:

objects.latest("Op_en6007", code_name = "answer")

+ Show code

See also

References


Related files

<mfanonymousfilelist></mfanonymousfilelist>