Miscellaneous snippets for future reference.


Writing to json

Using the jsonlite package.

library(jsonlite)

json_list <- list(arg1 = "random_arg_value1", arg2 = "random_arg_value2")
json_export <- toJSON(json_list, auto_unbox = TRUE)
write(json_export, "example.json")


Calling R script from batch file with parameters

The batch file.

set arg1=4
set arg2=experiment_02
"C:\Program Files\R\R-4.1.0\bin\Rscript.exe" C:\path\to\R\script\example_r_script.R %arg% %arg2% 

Retrieving arguments in the R script.

args <- commandArgs(trailingOnly = TRUE)

arg1 <- args[1]        
arg2 <- args[2]