write_r_file_with_content <- function(content) {
"
This function creates a temp R file. It writes the provided
content in the R file. Then it returns the path of the script.
"
path <- tempfile(fileext = ".R")
file.create(path)
writeLines(content, con = path)
return(path)
}
## let's fake a galaxy env
Sys.setenv(GALAXY_SLOTS = 1)
## let's says the tool has been launched with this command line
log_file <- tempfile()
file.create(log_file)
raw_args <- list(
"--args",
"--input", "in.csv",
"--output", "out.csv",
"--logs", log_file,
"--one-float", "3.14",
"--one-integer", "456",
"--one-logical", "FALSE",
"--some-floats", "1.5,2.4,3.3",
"--some-characters", "test,truc,bidule",
"--debug", "TRUE",
"--verbose", "FALSE"
)
##
# example 1
##
my_r_script <- write_r_file_with_content('
my_processing <- function(args, logger) {
logger$info("The tool is running")
logger$infof("Input file: %s.", args$input)
logger$info("The tool ended.")
}
')
W4MRUtils::run_galaxy_processing(
"Test tool 1",
my_processing(args, logger),
source_file = my_r_script,
args = W4MRUtils::parse_args(args = raw_args)
)
##
# example 2
## let's say we have a R script with this content:
path <- write_r_file_with_content('
setup_logger <- function(args, logger) {
if (!is.null(args$verbose)) {
logger$set_verbose(args$verbose)
}
if (!is.null(args$debug)) {
logger$set_debug(args$debug)
}
if (!is.null(args$logs)) {
logger$add_out_paths(args$logs)
}
}
stop_logger <- function(logger) {
logger$close_files()
}
processing <- function(args, logger) {
setup_logger(args, logger)
logger$info("The tool is working...")
logger$infof("Input: %s.", args$input)
logger$info("The tool stoping.")
stop_logger(logger)
return(NULL)
}')
## wrapper script:
args <- W4MRUtils::optparse_parameters(
input = W4MRUtils::optparse_character(),
output = W4MRUtils::optparse_character(),
logs = W4MRUtils::optparse_character(),
one_float = W4MRUtils::optparse_numeric(),
one_integer = W4MRUtils::optparse_integer(),
one_logical = W4MRUtils::optparse_flag(),
some_floats = W4MRUtils::optparse_list(of = "numeric"),
some_characters = W4MRUtils::optparse_list(of = "character"),
debug = W4MRUtils::optparse_flag(),
verbose = W4MRUtils::optparse_flag(),
args = raw_args[raw_args != "--args"]
)
W4MRUtils::run_galaxy_processing("A Test tool", args = args, {
## processing is from the other R script
processing(args, logger)
}, source_files = path)
Run the code above in your browser using DataLab