Write a R Run Script

In this discussion, we will examine the development of a R Run Script (RRS). Firstly, a thorough list of requirements will be outlined. Following this, all available support tools will be introduced.

RRS Requirements

A RRS must be a callable R script. This RRS is a script that must accepts two arguments: the input file path and the output file path. Please refer to the Run Scripts document for information on the file formats of the output and input files.

Available support

ChemDoE is able to provide assistance with the development of a RRS. In order to use the assistance, it is necessary to install the PyPI package of ChemDoE (See How to run it.).

The configuration of an RRS can be facilitated by using a graphical user interface (GUI). Alternatively, a RRS can also be configured/reorganised via the CLI command of the ChemDoE to facilitate this process.

In addition, this script tests whether the process works according to the requirements. It provides the R script with test inputs and outputs

ChemDoE add_python_script -src "/full/path/to/main.R" -o json -i json

Simple examples

# Define required packages
required_packages <- c("jsonlite", "gtools")

# Install any missing packages
for (pkg in required_packages) {
  if (!requireNamespace(pkg, quietly = TRUE)) {
    install.packages(pkg, repos = "https://cran.rstudio.com/")
  }
}

# Load the packages
library(jsonlite)
library(gtools)

# Get command-line arguments
args <- commandArgs(trailingOnly = TRUE)

if (length(args) < 2) {
  stop("Usage: Rscript script.R input.json output.json")
}

input_file <- args[1]
output_file <- args[2]

# Read the input JSON file
cat(sprintf("Reading %s\n", input_file))
values <- fromJSON(input_file)

# Define the number of variations to generate
NUMBER_OF_VARIATIONS <- 2

# Calculate context-related values
cat("Calculating context\n")
row_count <- length(values)  # Number of keys (variables)
number_of_options <- length(values[[1]]) - 1  # Options per variable (excluding unit)

arr <- seq(NUMBER_OF_VARIATIONS*number_of_options)

# Generate all permutations
perm <- permutations(length(arr), length(arr), arr, repeats.allowed = FALSE)
perm <- (perm %% 3) + 2
perm <- unique(perm)

perm_idx_lst <- sample(length(perm[,1]))

# Initialize results list with VARIABLE and UNIT vectors
results <- list(VARIABLE = names(values), UNIT = sapply(values, function(x) x[[1]]))


# Generate variations
cat("Preparing Variations\n")
for (i in seq_along(arr)) {
  variation_name <- sprintf("Variation.%d", i - 1)
  results[[variation_name]] <- vector("list", row_count)
  for (r_i in seq_along(values)) {
    perm_idx <- perm[perm_idx_lst[r_i],i]
    r_v <- values[[r_i]]
    results[[variation_name]][[r_i]] <- r_v[perm_idx]
  }
}

# Write the results to the output JSON file
cat(sprintf("Writing %s\n", output_file))
write_json(results, output_file, pretty = TRUE, auto_unbox = TRUE)