Several functions are designed to modify the apsimx file, including * replace
an existing model * remove
an existing model * append
a new model as sibling node * insert
a new model as child
read_apsimx
is used to read files with apsimx json format, i.e. *.apsimx
for simulations and *.json
for model under resource.
library(rapsimng)
<- read_apsimx(system.file("Wheat.json", package = "rapsimng"))
wheat #wheat <- read_apsimx("inst/Wheat.json")
The existing model can be replaced
and removed
. The example below shows to update the critical thermal and then remove it.
# Find the ThermalTime model
<- search_path(wheat, '[Wheat].Phenology.ThermalTime')
a $node$Children[[1]]$X[[2]]
a#> [1] 26
# Update the optimum temperature
$node$Children[[1]]$X[[2]] <- 27
a# Replace with new value
<- replace_model(wheat, a$path, a$node)
wheat_new <- search_path(wheat_new, '[Wheat].Phenology.ThermalTime')
b # The optimum temperature should be updated now
$node$Children[[1]]$X[[2]]
b#> [1] 27
# The ThermalTime model can also be removed
<- search_path(wheat, '[Wheat].Phenology.ThermalTime')
a <- remove_model(wheat, a$path)
wheat_new <- search_path(wheat_new, '[Wheat].Phenology.ThermalTime')
b # The ThermalTime model should not be found now (i.e. Empty list)
b#> list()
Function new_model
is used to generate the required elements for a new model, e.g. createing a new cultivar Hartog
<- new_model("PMF.Cultivar", name = "Hartog")
new_cultivar
new_cultivar#> $`$type`
#> [1] "Models.PMF.Cultivar, Models"
#>
#> $Name
#> [1] "Hartog"
#>
#> $Children
#> list()
#>
#> $IncludeInDocumentation
#> [1] TRUE
#>
#> $Enabled
#> [1] TRUE
#>
#> $ReadOnly
#> [1] FALSE
#>
#> $Alias
#> list()
#>
#> $Command
#> list()
Then the Command
can be updated to specify new parameter values.
$Command <- list(
new_cultivar"[Phenology].MinimumLeafNumber.FixedValue = 6",
"[Phenology].VrnSensitivity.FixedValue = 0")
Finally the new cultivar can be inserted into apsimx file.
# Read the apsimx file
<- read_apsimx(system.file("wheat.apsimx", package = "rapsimng"))
wheat # Create a new Replacements
<- new_model("Core.Replacements")
replacements # Insert the replacements into root folder
<- insert_model(wheat, 1, replacements)
wheat_new <- search_path(wheat_new, ".Simulations.Replacements")
replacements_node $path
replacements_node#> [1] 1 3
# Insert the new cultivar
<- insert_model(wheat_new, replacements_node$path, new_cultivar)
wheat_new
# Check the new cultivar
<- search_path(wheat_new,
cultivar_node ".Simulations.Replacements.Hartog")
$path
cultivar_node#> [1] 1 3 1
$node$Command
cultivar_node#> [[1]]
#> [1] "[Phenology].MinimumLeafNumber.FixedValue = 6"
#>
#> [[2]]
#> [1] "[Phenology].VrnSensitivity.FixedValue = 0"
A new model can be generated in R according to Models assemble in APSIM Next Generation. The rapsimng package stores a copy of Models.xml
from APSIM Next Generation on 1st August, 2020
.
The available models can be listed using function available_models
.
head(data.frame(model = available_models()))
#> model
#> 1 AgPasture.AGPBiomass
#> 2 AgPasture.BiomassAndN
#> 3 AgPasture.BiomassAndNLayered
#> 4 AgPasture.TissuesHelper
#> 5 AgPasture.SimpleGrazing
#> 6 AgPasture.SimpleGrazing.GrazingRotationTypeEnum
Function update_cultivar
is a short way to update parameter values for cultivars.
<- read_apsimx(system.file("wheat.apsimx", package = "rapsimng"))
wheat # Update cultivars
<- data.frame(name = rep("Hartog", 3),
df parameter = c("[Phenology].MinimumLeafNumber.FixedValue",
"[Phenology].VrnSensitivity.FixedValue",
"[Phenology].PpSensitivity.FixedValue"),
value = c(9, 7, 3))
<- update_cultivar(wheat, df)
wheat_cultivar # Check update cultivar paramters
<- search_path(wheat_cultivar, "[Replacements].Hartog")
hartog $path
hartog#> NULL