Modify apsimx file"

  collapse = TRUE,
  comment = "#>"

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 file

read_apsimx is used to read files with apsimx json format, i.e. *.apsimx for simulations and *.json for model under resource.

wheat <- read_apsimx(system.file("Wheat.json", package = "rapsimng"))
#wheat <- read_apsimx("inst/Wheat.json")

Modify existing model

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 
a <- search_path(wheat, '[Wheat].Phenology.ThermalTime')
# Update the optimum temperature
a$node$Children[[1]]$X[[2]] <- 27
# Replace with new value
wheat_new <- replace_model(wheat, a$path, a$node)
b <- search_path(wheat_new, '[Wheat].Phenology.ThermalTime')
# The optimum temperature should be updated now

# The ThermalTime model can also be removed
a <- search_path(wheat, '[Wheat].Phenology.ThermalTime')
wheat_new <- remove_model(wheat, a$path)
b <- search_path(wheat_new, '[Wheat].Phenology.ThermalTime')
# The ThermalTime model should not be found now (i.e. Empty list)

Create new model

Function new_model is used to generate the required elements for a new model, e.g. createing a new cultivar Hartog

new_cultivar <- new_model("PMF.Cultivar", name = "Hartog")

Then the Command can be updated to specify new parameter values.

new_cultivar$Command <- list(
  "[Phenology].MinimumLeafNumber.FixedValue = 6",
  "[Phenology].VrnSensitivity.FixedValue = 0")

Finally the new cultivar can be inserted into apsimx file.

# Read the apsimx file
wheat <- read_apsimx(system.file("wheat.apsimx", package = "rapsimng"))
# Create a new Replacements
replacements <- new_model("Core.Replacements")
# Insert the replacements into root folder
wheat_new <- insert_model(wheat, 1, replacements)
replacements_node <- search_path(wheat_new, ".Simulations.Replacements")

# Insert the new cultivar
wheat_new <- insert_model(wheat_new, replacements_node$path, new_cultivar)

# Check the new cultivar
cultivar_node <- search_path(wheat_new,

Available models

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()))

Update cultivar parameter values

Function update_cultivar is a short way to update parameter values for cultivars.

wheat <- read_apsimx(system.file("wheat.apsimx", package = "rapsimng"))
# Update cultivars
df <- data.frame(name = rep("Hartog", 3),
                  parameter = c("[Phenology].MinimumLeafNumber.FixedValue",
                 value = c(9, 7, 3))
wheat_cultivar <- update_cultivar(wheat, df)
# Check update cultivar paramters
hartog <- search_path(wheat_cultivar, "[Replacements].Hartog")

Try the rapsimng package in your browser

Any scripts or data that you put into this service are public.

rapsimng documentation built on Sept. 9, 2021, 9:07 a.m.