Run an R-script from command line and store results in subdirectory
Asked Answered
M

1

3

I want to run an R-script from the command line (on a Linux machine) and store the results in a subdirectory of the working directory. It looks like that:

./myscript.r [param 1] [param 2] [param 3]

Given 3 binomial parameters there are 4 conditions in total. I want to store the results of each condition in a subdirectory of the working directory.

Is there an elegant way to do that or do I really need to copy my R-script to every subdirectory?

Monochord answered 28/4, 2015 at 19:52 Comment(4)
Can't you take care of that in the R script itself whether via save, write.csv, etc.? You can create new directories from R using the system command.Extortion
could you pass the parameters within the script and have it write to each directory? Something like a for loop for each parameter?Marrero
If your R program writes to stdout, just pipe the output to a file specified relative to the current directory.Pernickety
You should include a minimal representative example of your program to provide more context for your problem.Ila
M
2

Purcell,try saving and running this script on your computer. Provide it a few different kinds and combinations of arguments.

#!/usr/bin/Rscript
args <- commandArgs(TRUE)
print(args)
class(args)

For instance, I ran ./test.R test=1 one=test.

Using this command in combination with paste0() statements should give you what you need to tailor your output locations based on the parameters provided.

Motherwort answered 28/4, 2015 at 23:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.