OpasnetUtils/CheckInput: Difference between revisions

From Opasnet
Jump to navigation Jump to search
(Created page with "{{method|moderator=|stub="yes"}} Category:OpasnetUtils ==Description== Checks if an input variable (with the "Inp" -prefix) of the same name as the given argument (ovariabl...")
 
mNo edit summary
 
(3 intermediate revisions by one other user not shown)
Line 8: Line 8:
==Code==
==Code==


<rcode
https://www.opasnet.org/svn/opasnet_utils/trunk/R/CheckInput.r
    label="Initiate functions"
    showcode="1"
>
CheckInput <- function(variable, substitute = FALSE, ...) {
if (nrow(variable@output) == 0) stop(paste(variable@name, "output not evaluated yet!"))
if (exists(paste("Inp", variable@name, sep = ""))) {
inputvar <- get(paste("Inp", variable@name, sep = ""))
if (substitute) {
colnames(inputvar@output)[colnames(inputvar@output) == "Result"] <- "InpVarRes" # Should probably be changed to something
colnames(variable@output)[colnames(variable@output) == "Result"] <- "VarRes" # that would never be used in any data.
finalvar <- merge(variable, inputvar)
finalvar@output$Result <- ifelse(is.na(finalvar@output$InpVarRes), finalvar@output$VarRes, finalvar@output$InpVarRes)
finalvar@output$Source <- ifelse(is.na(finalvar@output$InpVarRes), finalvar@output$Source, "Input")
return(finalvar[!colnames(finalvar) %in% c("InpVarRes", "VarRes")])
}
temp <- data.frame() #variable@output[variable@output$Source,]
for (i in levels(variable@output$Source)) {
temp <- merge(temp, variable@output[variable@output$Source == i, !colnames(variable@output) %in% "Source"])
colnames(temp)[colnames(temp) %in% "Result"] <- i
}
return(melt(temp, measure.vars = levels(variable@output$Source), variable.name = "Source", value.name = "Result", ...))
}
return(variable@output)
}
</rcode>


==See also==
==See also==

Latest revision as of 13:27, 16 August 2012

Description

Checks if an input variable (with the "Inp" -prefix) of the same name as the given argument (ovariable) exists. If input exists appends or substitutes it to the output and marks the source as "Input". Returns a data.frame.

Code

https://www.opasnet.org/svn/opasnet_utils/trunk/R/CheckInput.r

See also