| Title: | Provides Utility Functions and ShinyApps to work with the modeling framework 'SIMPLACE' |
|---|---|
| Description: | Provides Utility Functions and ShinyApps to work with the modeling framework 'SIMPLACE'. It visualises components of a solution, runs simulations and displays results. |
| Authors: | Gunther Krauss [aut, cre] |
| Maintainer: | Gunther Krauss <[email protected]> |
| License: | GPL-3 |
| Version: | 0.9.1 |
| Built: | 2026-05-19 11:22:44 UTC |
| Source: | https://github.com/gk-crop/simplaceUtil |
Notice: One can only add inputs that are defined by the sim component. If source is not given, then the parameter value is used.
addComponentInput( sol, componentid, id, source = NULL, value = NULL, datatype = NULL, unit = NULL, description = NULL )addComponentInput( sol, componentid, id, source = NULL, value = NULL, datatype = NULL, unit = NULL, description = NULL )
sol |
solution object |
componentid |
id of sim component |
id |
id of variable |
source |
source of variable |
value |
value of variable (is used only if source is not given or NULL) |
datatype |
datatype (optional) |
unit |
unit (optional) |
description |
short description (optional) |
modified solution object
Any output with the same id will be removed. The added output has no output
variables. One has to add new variables via addOutputVariable.
addCSVOutput( sol, filename, outputid, frequence = "DAILY", rule = NULL, resetrule = NULL, cachesize = 10, divider = "," )addCSVOutput( sol, filename, outputid, frequence = "DAILY", rule = NULL, resetrule = NULL, cachesize = 10, divider = "," )
sol |
solution object |
filename |
name of the output file |
outputid |
id of new output |
frequence |
one of DAILY, YEARLY, BOOLEAN, COMPLEX |
rule |
optional rule, when frequence is BOOLEAN or COMPLEX |
resetrule |
optional resetrule, when frequence is BOOLEAN or COMPLEX |
cachesize |
optional cachesize |
divider |
character that is used as a divider for the csv file |
modified solution object
Any output with the same id will be removed. The added output has no output
variables. One has to add new variables via addOutputVariable.
addMemoryOutput( sol, outputid, frequence = "DAILY", rule = NULL, resetrule = NULL, cachesize = 10 )addMemoryOutput( sol, outputid, frequence = "DAILY", rule = NULL, resetrule = NULL, cachesize = 10 )
sol |
solution object |
outputid |
id of new output |
frequence |
one of DAILY, YEARLY, BOOLEAN, COMPLEX |
rule |
optional rule, when frequence is BOOLEAN or COMPLEX |
resetrule |
optional resetrule, when frequence is BOOLEAN or COMPLEX |
cachesize |
optional cachesize |
modified solution object
Adds output variable to an existing output
addOutputVariable( sol, outputid, id, rule, datatype, mode = NULL, unit = NULL, description = NULL, format = NULL )addOutputVariable( sol, outputid, id, rule, datatype, mode = NULL, unit = NULL, description = NULL, format = NULL )
sol |
solution object |
outputid |
id of output where the variable should be added |
id |
name of the new variable |
rule |
rule for the variable |
datatype |
of the variable |
mode |
one of FIRST, LAST, AVG, SUM (optional) |
unit |
unit of the variable (optional) |
description |
short description (optional) |
format |
format string (optional) |
modified solution object
Adds elements for timing the execution time of sim components (and transformers)
addTimingSimComponent( sol, filename = NULL, componentlist = NULL, interfaceid = "automatic_timing_interface", outputid = "automatic_timing_output", simcomponentid = "AutomaticTiming" )addTimingSimComponent( sol, filename = NULL, componentlist = NULL, interfaceid = "automatic_timing_interface", outputid = "automatic_timing_output", simcomponentid = "AutomaticTiming" )
sol |
solution object |
filename |
optional filename to write the timing information to csv file |
componentlist |
optional list of component ids (if empty, all components will be timed) |
interfaceid |
id for the interface (optional) |
outputid |
id for the output (optional) |
simcomponentid |
id for the timing simcomponent (optional) |
variables sectionAdds an user variable to the variables section
addUserVariable( sol, id, value, datatype, unit = NULL, description = NULL, rule = NULL )addUserVariable( sol, id, value, datatype, unit = NULL, description = NULL, rule = NULL )
sol |
solution object |
id |
id of the variable |
value |
value of the variable |
datatype |
of the variable |
unit |
unit (optional) |
description |
short description (optional) |
rule |
optional rule |
modified solution object
When the parameter already exists in the parameter set, it is removed and the new parameter is added at the end.
addXMLParameter(paramdata, key, id, value = "", unit = "", description = NULL)addXMLParameter(paramdata, key, id, value = "", unit = "", description = NULL)
paramdata |
parameterdata (xml2 object) |
key |
named vector |
id |
name of the parameter which should added |
value |
value to set (numeric or character vector) |
unit |
unit of the parameter |
description |
description of the parameter |
modified parameter set (xml2 object)
params <- readXMLParameterFile(system.file("input", "crop.xml", package="simplaceUtil")) getXMLParameter(params, c(CROPNAME="soy bean"), "ExtraTableYZZ") params_new <- addXMLParameter(params, c(CROPNAME="soy bean"), "ExtraTableYZZ", c(1,2,4)) getXMLParameter(params_new, c(CROPNAME="soy bean"), "ExtraTableYZZ")params <- readXMLParameterFile(system.file("input", "crop.xml", package="simplaceUtil")) getXMLParameter(params, c(CROPNAME="soy bean"), "ExtraTableYZZ") params_new <- addXMLParameter(params, c(CROPNAME="soy bean"), "ExtraTableYZZ", c(1,2,4)) getXMLParameter(params_new, c(CROPNAME="soy bean"), "ExtraTableYZZ")
When running large amout of runs (e.g. calibration) it's recommended to avoid to write outputs on disk.
changeAllOutputTypesToMemory(sol)changeAllOutputTypesToMemory(sol)
sol |
solution object |
modified solution object
When running large amout of runs (e.g. calibration) it's recommended to avoid to write outputs on disk.
changeInterfaceFile(sol, id, filename, keeppath = FALSE)changeInterfaceFile(sol, id, filename, keeppath = FALSE)
sol |
solution object |
id |
id of interface |
filename |
new filename |
keeppath |
if set to true, original path is kept and only filename is modified |
modified solution object
Changes the type of an output from CSV to MEMORY or vice versa
changeOutputType(sol, outputid, type, filename = NULL, divider = ",")changeOutputType(sol, outputid, type, filename = NULL, divider = ",")
sol |
solution object |
outputid |
id of output to change |
type |
one of 'CSV' or 'MEMORY' |
filename |
name of the output file if type changes to 'CSV' |
divider |
character that is used as a divider for the csv file |
modified solution object
Creates a graph from component and links dataframe
componentsToGraph( comp, links, showinterfaces = FALSE, fillcolors = c(var = "white", interface = "#e0e0e0", resource = "#ffff99cc", alias = "#ffff99cc", transform = "#ffdd88cc", mgm = "#ffaa99cc", simple = "#ff9988cc", normal = "#ff7777cc", grouped = "#ff6655cc", output = "#aaaaaacc"), shapes = c(var = "oval", resource = "egg", simcomponent = "rectangle", output = "polygon", interface = "triangle"), ... )componentsToGraph( comp, links, showinterfaces = FALSE, fillcolors = c(var = "white", interface = "#e0e0e0", resource = "#ffff99cc", alias = "#ffff99cc", transform = "#ffdd88cc", mgm = "#ffaa99cc", simple = "#ff9988cc", normal = "#ff7777cc", grouped = "#ff6655cc", output = "#aaaaaacc"), shapes = c(var = "oval", resource = "egg", simcomponent = "rectangle", output = "polygon", interface = "triangle"), ... )
comp |
components dataframe |
links |
links dataframe |
showinterfaces |
if TRUE, include interfaces in graph |
fillcolors |
vector of colors, named by component's subtype |
shapes |
vector of shapes, named by component's type |
... |
options passed to |
graph object of class dgr_graph
The function creates Java code stubs for defining the fields, adding variables and initialising values. It creates XML subs for parameter file, as well as resource definition for parameter and input data, inputs for simcomponent and outputs.
createCodeStubsForSimVariables( variables, component = "MyComponent", outfolder = NULL, ... )createCodeStubsForSimVariables( variables, component = "MyComponent", outfolder = NULL, ... )
variables |
dataframe or path to csv file with variables |
component |
name of SimComonent |
outfolder |
optional, if given code is written the folder's files |
... |
parameters passed to read.table (e.g. sep, dec etc.) |
The dataframe / CSV file should have columns
contenttype (one of: constant, input, state, rate, out)
id
description
datatype (Simplace Datatype or corresponding Java Datatype)
unit
min
max
default
list of code snippets
Creates XML stubs for interface and resource section from CSV file structure
createResourceStubsFromCsv( filename, id, sep = ",", keyvals = NULL, arraycolumns = NULL, frequence = "DAILY", rule = NULL, data = NULL )createResourceStubsFromCsv( filename, id, sep = ",", keyvals = NULL, arraycolumns = NULL, frequence = "DAILY", rule = NULL, data = NULL )
filename |
name of the CSV file |
id |
id for the resource |
sep |
separator for CSV file |
keyvals |
named vector of column indices or names that act as key column |
arraycolumns |
vector of column indices or names that are arrays |
frequence |
frequence attribute for the resource |
rule |
rule attribute for the resource |
data |
optional data.frame (otherwise data will be loaded from file) |
list of two strings ('interface' and 'resource')
stubs <- createResourceStubsFromCsv( filename = system.file("input","weather.csv", package="simplaceUtil"), id = "weather", sep =",", keyvals = c("CURRENT.DATE" = "Date") ) cat(stubs$interface) cat(stubs$resource) stubs <- createResourceStubsFromCsv( filename = system.file("input","soil.csv", package="simplaceUtil"), id = "soil", sep =";", keyvals = c("vSoilType" = "soiltype"), arraycolumns = 6:20 ) cat(stubs$resource)stubs <- createResourceStubsFromCsv( filename = system.file("input","weather.csv", package="simplaceUtil"), id = "weather", sep =",", keyvals = c("CURRENT.DATE" = "Date") ) cat(stubs$interface) cat(stubs$resource) stubs <- createResourceStubsFromCsv( filename = system.file("input","soil.csv", package="simplaceUtil"), id = "soil", sep =";", keyvals = c("vSoilType" = "soiltype"), arraycolumns = 6:20 ) cat(stubs$resource)
Creates XML stubs for interface and resource section from XML file structure
createResourceStubsFromXml( filename, id, keyvals = NULL, frequence = "DAILY", rule = NULL, xmlnode = NULL )createResourceStubsFromXml( filename, id, keyvals = NULL, frequence = "DAILY", rule = NULL, xmlnode = NULL )
filename |
name of the xml file |
id |
id for the resource |
keyvals |
named vector of column indices or names that act as key column |
frequence |
frequence attribute for the resource |
rule |
rule attribute for the resource |
xmlnode |
optional xml_node (otherwise xml will be read from filename) |
list of two strings ('interface' and 'resource')
stubs <- createResourceStubsFromXml( filename = system.file("input","crop.xml",package="simplaceUtil"), id="soil", keyvals = c("vSoilType"=1) ) cat(stubs$resource)stubs <- createResourceStubsFromXml( filename = system.file("input","crop.xml",package="simplaceUtil"), id="soil", keyvals = c("vSoilType"=1) ) cat(stubs$resource)
The method takes a function as well as values for lower and upper boundaries and returns a modified function. The modified function returns the value of the original function when the parameters are within boundaries and the penalty value otherwise.
enhanceFunctionWithBoundaries( fun, l_bound, u_bound, penalty_value = Inf, boundary_fun = NULL, param_pos = 1, ... )enhanceFunctionWithBoundaries( fun, l_bound, u_bound, penalty_value = Inf, boundary_fun = NULL, param_pos = 1, ... )
fun |
function to be modified |
l_bound |
vector with lower boundary values |
u_bound |
vector with upper boundary values |
penalty_value |
value if parameter outside boundaries |
boundary_fun |
optional function for complex boundary conditions |
param_pos |
argument position of the parameter |
... |
arguments passed to original function |
Optionally an own function can be supplied to calculate whether the parameter is valid. The boundary function must take 3 arguments: parameter, lower boundary and upper boundary and must return TRUE or FALSE.
A main use case of this method are optimisation / calibration tasks. If the optimisation method and the function to optimise are both ignorant to boundaries one can turn the function into a boundary sensitive one.
a modified function that considers boundaries
sqrt_bd <- enhanceFunctionWithBoundaries(sqrt, 0, 10) sqrt_bd(-1) sqrt_bd(1) sqrt_bd(11)sqrt_bd <- enhanceFunctionWithBoundaries(sqrt, 0, 10) sqrt_bd(-1) sqrt_bd(1) sqrt_bd(11)
The method takes a function as well as values for lower and upper boundaries and returns a modified function. The modified function returns the value of the original function when the parameters are within boundaries and a penalised function otherwise.
enhanceFunctionWithPenalty( fun, l_bound, u_bound, penalty_fun = function(value, distance) (value + distance) * exp(1000 * distance), distance_fun = function(x, l, u) { max(0, (l - x)/(u - l), (x - u)/(u - l)) }, param_pos = 1, ... )enhanceFunctionWithPenalty( fun, l_bound, u_bound, penalty_fun = function(value, distance) (value + distance) * exp(1000 * distance), distance_fun = function(x, l, u) { max(0, (l - x)/(u - l), (x - u)/(u - l)) }, param_pos = 1, ... )
fun |
function to be modified |
l_bound |
vector with lower boundary values |
u_bound |
vector with upper boundary values |
penalty_fun |
function taking value and distance and returns value modified value depending on distance |
distance_fun |
function that takes x, l_bound and u_bound and computes the distance of x to the boundaries |
param_pos |
argument position of the parameter |
... |
arguments passed to original function |
Optionally an own function can be supplied to calculate the penalised value. The function must take two arguments: the original value and the distance of the parameter to the boundaries.
Additionally an own distance function can be supplied, that has to take three arguments: the parameter, the lower boundaries and the upper boundaries.
A main use case of this method are optimisation / calibration tasks. If the optimisation method and the function to optimise are both ignorant to boundaries one can turn the function into a boundary sensitive one.
a modified function that changes value when parameter outside bounds
sqr_bd <- enhanceFunctionWithPenalty(\(x) x^2, .1, 10) sqr_bd(-1) sqr_bd(1) sqr_bd(11)sqr_bd <- enhanceFunctionWithPenalty(\(x) x^2, .1, 10) sqr_bd(-1) sqr_bd(1) sqr_bd(11)
Fetches the Description (as HTML source code) of a SimComponent from Simplace Website
fetchDescriptionFromWebsite(class, version = "current")fetchDescriptionFromWebsite(class, version = "current")
class |
class name of the SimComponent |
version |
Use |
string with the description text (including HTML tags)
Fetches the list of SimComponents from the Simplace Website
fetchSimComponentlistFromWebsite(version = "current")fetchSimComponentlistFromWebsite(version = "current")
version |
Use |
character vector with the class names of all SimComponents
Fetches the SimVariables table for a SimComponent from Simplace Website
fetchSimVariablesFromWebsite(class, version = "current")fetchSimVariablesFromWebsite(class, version = "current")
class |
class name of the SimComponent |
version |
Use |
a data.frame with the SimVariables
Selects edges of specific timestep
filterEdges(graph, linkage = "allsteps")filterEdges(graph, linkage = "allsteps")
graph |
graph object of class |
linkage |
all steps, same step or previous step |
graph object of class dgr_graph
Formats / prettyprints a project file
formatProjectFile( file, outfile = file, version = "5.2", stripcomments = FALSE, dropattributes = c() )formatProjectFile( file, outfile = file, version = "5.2", stripcomments = FALSE, dropattributes = c() )
file |
filename of solution or project |
outfile |
filename the reformatted document is written to |
version |
version of the solution |
stripcomments |
if TRUE, then strip xml comments |
dropattributes |
vector of attribute names that will be dropped globally. |
no return value, called for the side effect to write a file to disk
Formats / prettyprints a solution file
formatSolutionFile( file, outfile = file, version = "5.2", stripcomments = FALSE, dropattributes = c() )formatSolutionFile( file, outfile = file, version = "5.2", stripcomments = FALSE, dropattributes = c() )
file |
filename of solution or project |
outfile |
filename the reformatted document is written to |
version |
version of the solution |
stripcomments |
if TRUE, then strip xml comments |
dropattributes |
vector of attribute names that will be dropped globally. |
no return value, called for the side effect to write a file to disk
## Not run: original <- system.file("solution","Yield.sol.xml",package="simplaceUtil") formatted <- tempfile(fileext = ".sol.xml") formatSolutionFile(original, formatted, stripcomments = TRUE) ## End(Not run)## Not run: original <- system.file("solution","Yield.sol.xml",package="simplaceUtil") formatted <- tempfile(fileext = ".sol.xml") formatSolutionFile(original, formatted, stripcomments = TRUE) ## End(Not run)
Gets components for a solution
getComponents(x, description = TRUE)getComponents(x, description = TRUE)
x |
xml object (solution) |
description |
if TRUE, title attribute and text from description tag are included |
data.frame with the solution components (resources, sim components, outputs)
Get component and links dataframe from solution file
getElementsFromSolutionFile(file)getElementsFromSolutionFile(file)
file |
solution |
list with solution (xml2 object) and components, links and variables data.frame
Get components that are linked from given component
getLinkingFromComponent( graph, names, distance = 50, linkage = "allsteps", type = "all" )getLinkingFromComponent( graph, names, distance = 50, linkage = "allsteps", type = "all" )
graph |
graph object of class |
names |
names (id attributes) of the selected components |
distance |
maximum distance from given components |
linkage |
all steps, same step or previous step |
type |
type of components |
graph object of class dgr_graph
Get components that link to given components
getLinkingToComponent( graph, names, distance = 50, linkage = "allsteps", type = "all" )getLinkingToComponent( graph, names, distance = 50, linkage = "allsteps", type = "all" )
graph |
graph object of class |
names |
names (id attributes) of selected components |
distance |
maximum distance from given components |
linkage |
all steps, same step or previous step |
type |
type of components |
graph object of class dgr_graph
Get the dataframe of links between components
getLinks(x, df)getLinks(x, df)
x |
xml object (solution) |
df |
data.frame of components |
data.frame with the linked variables between components
Get ids of memory outputs
getMemoryOutputIds(comp)getMemoryOutputIds(comp)
comp |
components dataframe |
character vector with the memory output ids
Get the simcomponents in the neighborhood of given components
getNeighborhood( graph, names, distance = 1, linkage = "allsteps", type = "all", set_op = "union" )getNeighborhood( graph, names, distance = 1, linkage = "allsteps", type = "all", set_op = "union" )
graph |
graph object of class |
names |
names (id attributes) of the selected sim components |
distance |
maximum number of steps from given component |
linkage |
all steps, same step or previous step |
type |
type of component |
set_op |
"union" - all neighbours of selected components or "intersect" - common neighbours of all selectected components |
graph object of class dgr_graph
Get filenames of file outputs
getOutputFilenames(comp, variables, additional = NULL)getOutputFilenames(comp, variables, additional = NULL)
comp |
components dataframe |
variables |
variables dataframe |
additional |
additional variables as named vector c("var1"="value1", ...), useful for directory placeholder |
named character vector with the filenames
Get variables for a specific resource or transformer
getResourceVariables(x, id, compactkey = FALSE)getResourceVariables(x, id, compactkey = FALSE)
x |
xml object (solution) |
id |
id of the resource or transformer |
compactkey |
if TRUE, then id and key column are merged |
a datatframe with all the variables of a resource
Reads a solution from file
getSolutionFromFile(file)getSolutionFromFile(file)
file |
filename of the solution |
parsed solution as xml_document
Reads a solution from text
getSolutionFromText(text)getSolutionFromText(text)
text |
string with xml markup |
parsed solution as xml_document
Get info for a solution
getSolutionInfoAsDataframe(file, workdir)getSolutionInfoAsDataframe(file, workdir)
file |
solution file |
workdir |
working directory |
data.frame with all solution components as well as meta data for solution file
Converts a solution to text
getTextFromSolution(sol)getTextFromSolution(sol)
sol |
solution object (xml_document) |
text with xml code
Gets user variables for a solution
getUserVariables(x)getUserVariables(x)
x |
xml object (solution) |
data.frame with the user defined variable
Get a parameter value for a specific parameterset
getXMLParameter(paramdata, key, id)getXMLParameter(paramdata, key, id)
paramdata |
parameterdata (xml2 object) |
key |
named vector |
id |
name of the parameter of which the value should be retrieved |
the value of the parameter
params <- readXMLParameterFile(system.file("input", "crop.xml", package="simplaceUtil")) getXMLParameterIDs(params, "CROPNAME") getXMLParameter(params, c(CROPNAME="soy bean"), "TSUM1") getXMLParameter(params, c(CROPNAME="faba bean"), "TSUM1")params <- readXMLParameterFile(system.file("input", "crop.xml", package="simplaceUtil")) getXMLParameterIDs(params, "CROPNAME") getXMLParameter(params, c(CROPNAME="soy bean"), "TSUM1") getXMLParameter(params, c(CROPNAME="faba bean"), "TSUM1")
Get IDs of all parametersets in the parameterfile
getXMLParameterIDs(paramdata, keyid)getXMLParameterIDs(paramdata, keyid)
paramdata |
parameterdata (xml2 object) |
keyid |
id of the parameter that identifies the parameterset |
character vector with all IDs
params <- readXMLParameterFile(system.file("input", "crop.xml", package="simplaceUtil")) getXMLParameterIDs(params, "CROPNAME")params <- readXMLParameterFile(system.file("input", "crop.xml", package="simplaceUtil")) getXMLParameterIDs(params, "CROPNAME")
Creates legends
graphLegend(showinterfaces = TRUE, fillcolors = NULL, shapes = NULL)graphLegend(showinterfaces = TRUE, fillcolors = NULL, shapes = NULL)
showinterfaces |
if TRUE, include interfaces in graph |
fillcolors |
vector of colors, named by component's subtype |
shapes |
vector of shapes, named by component's type |
a list with the component legend and links (arrows) legend
character) column from output file to column of class Date
Converts date (class character) column from output file to column of class Date
parseDate( data, newName = "CURRENT.DATE", format = "%d.%m.%Y", oldName = "CURRENT.DATE" )parseDate( data, newName = "CURRENT.DATE", format = "%d.%m.%Y", oldName = "CURRENT.DATE" )
data |
data.frame |
newName |
name of the transformed date column |
format |
date format |
oldName |
name of the column that holds the date to be transformed |
data.frame with date column of class Date
Plots layered output
plotLayeredOutput( data, column, simulationid = NULL, date_from = NULL, date_to = NULL, datecol = "CURRENT.DATE", nrow = NULL, ncol = NULL, sep = "_" )plotLayeredOutput( data, column, simulationid = NULL, date_from = NULL, date_to = NULL, datecol = "CURRENT.DATE", nrow = NULL, ncol = NULL, sep = "_" )
data |
data from memory output or file |
column |
column name used for the fill color |
simulationid |
plot only data for simulationids |
date_from |
simulation date from where values are plotted |
date_to |
simulateon date until values are plotted |
datecol |
column name for date (default CURRENT.DATE) |
nrow |
number of panel rows when plotting multiple simulation ids |
ncol |
number of panels in a row when plotting multiple simulation ids |
sep |
character that separates layer number from variable name (default "_") |
Plots scalar output
plotScalarOutput( data, column_x, columns_y, simulationid = NULL, date_from = NULL, date_to = NULL, datecol = "CURRENT.DATE", nrow = NULL, ncol = NULL )plotScalarOutput( data, column_x, columns_y, simulationid = NULL, date_from = NULL, date_to = NULL, datecol = "CURRENT.DATE", nrow = NULL, ncol = NULL )
data |
data from memory output or file |
column_x |
column name for x values |
columns_y |
column name(s) for y values (vector of names) |
simulationid |
plot only data for simulationids |
date_from |
simulation date from where values are plotted |
date_to |
simulateon date until values are plotted |
datecol |
column name for date (default CURRENT.DATE) |
nrow |
number of panel rows when plotting multiple simulation ids |
ncol |
number of panels in a row when plotting multiple simulation ids |
Read XML parameter file
readXMLParameterFile(file)readXMLParameterFile(file)
file |
filename of parameterfile |
parameterdata (XML2 object)
Removes an input for component
removeComponentInput(sol, componentid, id)removeComponentInput(sol, componentid, id)
sol |
solution object |
componentid |
id of the sim component |
id |
id of the input |
modified solution object
When running large amout of runs (e.g. calibration) it's recommended to avoid to write outputs on disk.
removeNonMemoryOutputs(sol)removeNonMemoryOutputs(sol)
sol |
solution object |
modified solution object
Notice: the interface for the output will also be removed.
removeOutput(sol, outputid)removeOutput(sol, outputid)
sol |
solution object |
outputid |
id of output to remove |
modified solution object
Removes an output variable from a given output
removeOutputVariable(sol, outputid, id)removeOutputVariable(sol, outputid, id)
sol |
solution object |
outputid |
id of output from where the variable should be removed |
id |
name of the variable to remove |
modified solution object
Notice: there is no check, whether the ids exist. Variables are replaced
literarilly when they are the only content of an attribute or element content.
In other attributes and element contents ${oldid} will be
replaced by ${newid}
replaceVariable(sol, oldid, newid)replaceVariable(sol, oldid, newid)
sol |
solution object |
oldid |
id of variable to be replaced |
newid |
id of the replacing variable |
Notice that the id of variables from resources, SimComponents etc. should be prefixed by the enclosing resource's, SimComponent's etc. id.
modified solution object
Replaces variables with content
replaceVariablesWithValues(text, variables, additional = NULL)replaceVariablesWithValues(text, variables, additional = NULL)
text |
text to replace |
variables |
variables dataframe |
additional |
additional variables as named vector c("var1"="value1", ...), useful for directory placeholder |
User can create automatically a simple GUI for a specific solution/project by specifying solution, project, list of parameters that could be interatively changed as well a list of custom plot functions.
runSimplaceCustomGuiApp( simplacedirs, solution, project = NULL, plotlist = list(Default = simplaceUtil::plotScalarOutput), datalist = list(Default = identity), paramlist = list(), paramtransform = identity, ... )runSimplaceCustomGuiApp( simplacedirs, solution, project = NULL, plotlist = list(Default = simplaceUtil::plotScalarOutput), datalist = list(Default = identity), paramlist = list(), paramtransform = identity, ... )
simplacedirs |
a list containing the named elements instdir, workdir, outdir and options |
solution |
filename of a solution (or solution as text) |
project |
filename of a project |
plotlist |
list of named functions that produce a plot |
datalist |
list of named functions that transform output dataframes |
paramlist |
list of named parameter elements that will be used for input sliders / option lists |
paramtransform |
function to transform the parameterlist |
... |
parameters passed to shiny::runApp |
plotlist takes a named list of functions of the form
f(data, xvalue, yvalues, simulationids, from, to) and creates a plot
data is a data.frame
xvalue is a string with the column name for x
yvalues is a vector of strings with the column names for y
simulationids can be NULL or a vector of strings with simulation ids
from and to are dates for subsetting the data that will be plotted
datalist takes a named list of functions of the form f(data) and return
the modified data.frame
paramlist is a named list of parameters where the name of each element has to
match a var in the solution (unless the list will be transformed). Each parameter is a list with the elements
label
value
min and max or options (vector of possible values)
paramtransform is a function that transforms the input parameters to hand
them over to Simplace
does not return a value, called for the side effect of running the shiny app
## Not run: instdir <- simplace::findFirstSimplaceInstallation() simplacedirs <- list( instdir = instdir, workdir = paste0(instdir,"/simplace_run/simulation/"), outdir = paste0(instdir,"/simplace_run/output/") ) solution <- paste0(simplacedirs$workdir,"/gk/solution/calibration/Yield.sol.xml") paramlist <- list( vLUE = list( label = "Light Use efficiency", value = 3.0, min = 1.0, max = 6.5 ) ) plotlist <- list( "Default" = simplaceUtil::plotScalarOutput, "Boxplots" = function(data,x,y,sim,from,to) { if(length(y)==1 && is.numeric(data[[y]])) { boxplot(formula(paste(y,"~simulationid")),data) } } ) datalist <- list( "Aggregated Yield"=function(data) { dplyr::group_by(data, simulationid) |> dplyr::summarise(minY=min(Yield),maxY=max(Yield))}, "Raw Data"=identity ) simplaceUtil::runSimplaceCustomGuiApp( simplacedirs, sol, plotlist = plotlist, datalist = datalist, paramlist = paramlist ) ## End(Not run)## Not run: instdir <- simplace::findFirstSimplaceInstallation() simplacedirs <- list( instdir = instdir, workdir = paste0(instdir,"/simplace_run/simulation/"), outdir = paste0(instdir,"/simplace_run/output/") ) solution <- paste0(simplacedirs$workdir,"/gk/solution/calibration/Yield.sol.xml") paramlist <- list( vLUE = list( label = "Light Use efficiency", value = 3.0, min = 1.0, max = 6.5 ) ) plotlist <- list( "Default" = simplaceUtil::plotScalarOutput, "Boxplots" = function(data,x,y,sim,from,to) { if(length(y)==1 && is.numeric(data[[y]])) { boxplot(formula(paste(y,"~simulationid")),data) } } ) datalist <- list( "Aggregated Yield"=function(data) { dplyr::group_by(data, simulationid) |> dplyr::summarise(minY=min(Yield),maxY=max(Yield))}, "Raw Data"=identity ) simplaceUtil::runSimplaceCustomGuiApp( simplacedirs, sol, plotlist = plotlist, datalist = datalist, paramlist = paramlist ) ## End(Not run)
Runs Shiny app
runSimplaceGuiApp(...)runSimplaceGuiApp(...)
... |
parameters passed to shiny::runApp |
Scales a parameter value by a value
scaleXMLParameter(paramdata, key, id, factor = 1)scaleXMLParameter(paramdata, key, id, factor = 1)
paramdata |
parameterdata (xml2 object) |
key |
named vector |
id |
name of the parameter which should be scaled |
factor |
scaling factor |
modified parameter set (xml2 object)
params <- readXMLParameterFile(system.file("input", "crop.xml", package="simplaceUtil")) getXMLParameter(params, c(CROPNAME="soy bean"), "RUETableRUE") params_new <-scaleXMLParameter(params, c(CROPNAME="soy bean"), "RUETableRUE",0.9) getXMLParameter(params_new, c(CROPNAME="soy bean"), "RUETableRUE")params <- readXMLParameterFile(system.file("input", "crop.xml", package="simplaceUtil")) getXMLParameter(params, c(CROPNAME="soy bean"), "RUETableRUE") params_new <-scaleXMLParameter(params, c(CROPNAME="soy bean"), "RUETableRUE",0.9) getXMLParameter(params_new, c(CROPNAME="soy bean"), "RUETableRUE")
Sets the value of an input for a given parent element
setInputValue(sol, parentid, id, value, datatype = NULL)setInputValue(sol, parentid, id, value, datatype = NULL)
sol |
solution object |
parentid |
id of input parent |
id |
id of the input |
value |
new value |
datatype |
datatype (optional) |
modified solution object
It's main use is to change consistently inputs of all transformers,
e.g. layerthickness. It can also be used to set sim component inputs
to a fixed value.
setInputValueForCategory(sol, category, id, value, datatype = NULL)setInputValueForCategory(sol, category, id, value, datatype = NULL)
sol |
solution object |
category |
tag of categories containing inputs, e.g. |
id |
id of the input |
value |
new value |
datatype |
datatype (optional) |
modified solution object
Set a parameter value for a specific parameterset
setXMLParameter(paramdata, key, id, value = "")setXMLParameter(paramdata, key, id, value = "")
paramdata |
parameterdata (xml2 object) |
key |
named vector |
id |
name of the parameter of which the value should be set |
value |
new value to set (vector) |
modified parameter set (xml2 object)
params <- readXMLParameterFile(system.file("input", "crop.xml", package="simplaceUtil")) getXMLParameter(params, c(CROPNAME="soy bean"), "TSUM1") params_new <-setXMLParameter(params, c(CROPNAME="soy bean"), "TSUM1",400) getXMLParameter(params_new, c(CROPNAME="soy bean"), "TSUM1")params <- readXMLParameterFile(system.file("input", "crop.xml", package="simplaceUtil")) getXMLParameter(params, c(CROPNAME="soy bean"), "TSUM1") params_new <-setXMLParameter(params, c(CROPNAME="soy bean"), "TSUM1",400) getXMLParameter(params_new, c(CROPNAME="soy bean"), "TSUM1")
Provides functions to work with the modeling framework Simplace
get elements of a simplace solution as dataframes
visualise the structure of a solution as a graph
helper functions for transforming output data
simplified plot functions for simulation output variables
shinyApp to visualise graphs, run simulations and plot results
Gunther Krauss
## Not run: # run the GUI runSimplaceGuiApp() ## End(Not run) # visualise the solution structure as graph graph <- solutionToGraph(system.file("solution", "Yield.sol.xml", package = "simplaceUtil")) DiagrammeR::render_graph(graph)## Not run: # run the GUI runSimplaceGuiApp() ## End(Not run) # visualise the solution structure as graph graph <- solutionToGraph(system.file("solution", "Yield.sol.xml", package = "simplaceUtil")) DiagrammeR::render_graph(graph)
Creates a graph from a solution
solutionToGraph( file, showinterfaces = FALSE, fillcolors = NULL, shapes = NULL, ... )solutionToGraph( file, showinterfaces = FALSE, fillcolors = NULL, shapes = NULL, ... )
file |
solution file |
showinterfaces |
if true include interfaces |
fillcolors |
vector of colors, named by component's subtype |
shapes |
vector of shapes, named by component's type |
... |
options passed to DiagrammeR::create_graph() |
graph object of class dgr_graph
Rearranges the order of SimComponents. All components that are mentioned in
the vector order will be rearranged according to their position in the order
vector. All other components remain on the same position.
E. g. an order of c(5,6,1,3) will put components on position 1,3,5,6 to
position 5, 6, 1, 3.
swapComponents(sol, order)swapComponents(sol, order)
sol |
solution object |
order |
a vector of component positions |
modified solution object
Transform layered output data in long format
transformLayeredData(data, sep = "_")transformLayeredData(data, sep = "_")
data |
dataframe |
sep |
character that separates layer number from variable name (default "_") |
dataframe in long format
Symbols for unit URIs
data(unitsymbols)data(unitsymbols)
A named vector of unit symbols where the names ar the unit URIs
# data(unitsymbols) # unitsymbols['http://www.wurvoc.org/vocabularies/om-1.8/gram_per_square_metre']# data(unitsymbols) # unitsymbols['http://www.wurvoc.org/vocabularies/om-1.8/gram_per_square_metre']
Writes solution to file
writeSolutionToFile(sol, file)writeSolutionToFile(sol, file)
sol |
solution object (xml_document) |
file |
filename for the solution |
nothing, function writes a file as a side effect
Write parameterdata to xml file
writeXMLParameterFile(paramdata, file)writeXMLParameterFile(paramdata, file)
paramdata |
parameterdata (xml2 object) |
file |
filename of parameterfile |
just for side effects, returs paramdata invisibly