+ Show code- Hide code
# This is code Op_en7457/fill on page [[Branch creator]]
# Parameters:
## newpage: TRUE if a new page is being created
## newdata: TRUE if a new data-based (from t2b) ovariable is being created
## newformula: TRUE if a new formula-based ovariable is being created
## branchname: name of the branch
## output.ovariable: name of the ovariable that calculates the final output of this branch. The name is used in the Answer code.
## data.ovariable: name of an ovariable that takes data as input
## unit: unit(s) of the data
## t2bdata: table of data to be converted in the data slot of the variable
## data.rationale: textbox of description for data.ovariable
## formula.ovariable: name of an ovariable based on formula and dependencies
## dependencies: character vector of the names of dependencies
## formula: the body of the formula (should contain the function 'return' in the end)
## formula.rationale: textbox of description for formula.ovariable
library(OpasnetUtils)
if(newpage) {
newpage <- paste("[[Category:Life cycle assessment]]
[[Category:Handprinter]]
{{method|moderator=",
wiki_username,
"}}
",
"'''{{PAGENAME}}''' is a '''branch''' of Handprinter. This page is a test case how to manage branches.
== Question ==
How to estimate the life-cycle impacts of '''{{PAGENAME}}'''?
== Answer ==
¤rcode embed=1 variables='
name:PAGEID|type:hidden|default:{{PAGEID}}
'>
# This is code Op_enXXXX/ on page [[",
branchname,
"]]
library(OpasnetUtils)
objects.latest(paste('Op_en', PAGEID, sep = ''), code_name = '",
output.ovariable,
"')
",
output.ovariable,
" ¤- EvalOutput(",
output.ovariable,
")
oprint(summary(",
output.ovariable,
"))
¤/rcode>
== Rationale ==
",
sep = ""
)
}
if(newdata) {
# Take the data out of the input
dat <- as.matrix(t2bdata[-1 , ])
dat <- apply(dat, 1, paste, collapse = "|")
dat <- paste(dat, "
",
sep = "")
# Find parameter values for t2b tag.
cols <- as.matrix(t2bdata)[1 , ]
# Find column(s) with results: has name Result, has semicolons, or is the last one.
rescols <- tolower(cols) %in% "result"
if(!any(rescols)) {
rescols <- grepl(":", cols)
}
if(!any(rescols)) {
rescols <- 1:length(cols) == length(cols)
}
# Indices are to the left from rescols, descriptions are to the right.
firstres <- min(grep(TRUE, rescols))
if(firstres > 1) indices <- cols[1:(firstres-1)] else indices <- character()
lastres <- max(grep(TRUE, rescols))
if(lastres < length(cols)) desc <- cols[(lastres+1):length(cols)] else desc <- character()
resnames <- strsplit(cols[rescols], split = ":")
# If only one rescol
## If first is Splittable
### If first is Result then no index, last is obs else add first as index, last is locations
## If nonsplittable then no index, last is obs
# If more than one rescol, then first is always splittable
## Add first as index, last is locations
if("result" == tolower(resnames[[1]][1]) | length(resnames[[1]]) == 1) {
res <- paste(c("obs='", resnames[[1]][length(resnames[[1]])], "'"), collapse = "")
} else {
indices <- c(indices, resnames[[1]][1])
resnames <- sapply(resnames, FUN = function(x) {return(x[length(x)])})
res <- paste(c(" locations='", paste(resnames, collapse = ","), "'"), collapse = "")
}
if(length(indices) > 0) {
indices <- paste(c("index='", paste(indices, collapse = ","), "'"), collapse = "")
}
if(length(desc) > 0) {
desc <- paste(c("desc='", paste(desc, collapse = ","), "'"), collapse = "")
}
out <- paste(c(
"¤t2b",
paste(c("name='", data.ovariable, "'"), collapse = ""),
indices,
res,
desc,
paste(c("unit='", unit, "'"), collapse = ""),
">"),
collapse = " "
)
out <- c(out, "
",
dat, "¤/t2b>
")
newdata <- paste(c(
"=== ",
data.ovariable,
" ===
",
out,
"¤rcode name='",
data.ovariable,
"' embed=1 label='Initiate ",
data.ovariable,
" (for developers only)' variables='
name:PAGEID|type:hidden|default:{{PAGEID}}
'>
# This is code Op_enXXXX/",
data.ovariable,
" on page [[",
branchname,
"]]
library(OpasnetUtils)
",
data.ovariable,
" ¤- Ovariable('",
data.ovariable,
"',
ddata = paste('Op_en', PAGEID, sep = ''),
subset = '",
data.ovariable,
"')
objects.store(",
data.ovariable,
")
cat('Object',",
data.ovariable,
"@name, 'stored.@@')
",
"¤/rcode>
",
data.rationale,
"
"),
collapse = ""
)
}
if(newformula) {
newformula <- paste(
"=== ",
formula.ovariable,
" ===
¤rcode name='",
formula.ovariable,
"' embed=1 label='Initiate ",
formula.ovariable,
" (for developers only)'>
# This is code Op_enXXXX/",
formula.ovariable,
" on page [[",
branchname,
"]]
library(OpasnetUtils)
",
formula.ovariable,
" ¤- Ovariable('",
formula.ovariable,
"',
dependencies = data.frame(
Name = c('",
paste(dependencies, collapse = "', '"),
"')
),
formula = function(...) {
",
gsub("<", "¤", formula),
" }
)
objects.store(",
formula.ovariable,
")
cat('Object ',",
formula.ovariable,
"@name, ' stored.@@')
¤/rcode>
",
formula.rationale,
"
",
sep = ""
)
}
finale <- paste(
"== See also ==
* [[Handprinter]]
* [[Developing branches]]
",
"== Related files ==
",
sep = ""
)
text1 <- ""
if(!is.logical(newpage)) text1 <- paste(text1, newpage, collapse = "")
if(!is.logical(newdata)) text1 <- paste(text1, newdata, collapse = "")
if(!is.logical(newformula)) text1 <- paste(text1, newformula, collapse = "")
if(!is.logical(newpage)) text1 <- paste(text1, finale, collapse = "")
text2 <- strsplit(text1, split = "\n")[[1]]
text2 <- gsub("@@", "\\n", text2)
text2 <- data.frame(Pagetemplate = text2)
oprint(text2, include.rownames = FALSE)
| |