Run same R script from terminal with different arguments in parallel - r

I have an R script that's called the following way from the terminal:
Rscript myscript.R param1 param2
When the script finishes it generates an output .csv file that is saved somewhere on my server.
I want to be able to, by using tmux, on different panes, run the same script, just with different values for param1 param2.
How do I go about to do that? Really struggling to find the best way to do it.

So the best solution I managed to get to this problem was to basically start R sessions on each tmux window and from there just input the arguments and run the actual script.
Not the most practical but gets the job done.

Related

Is it possible to save an R script, with unique parameters used in the script, after it has run?

I'm using R for some processes, and I was wondering if it was possible to save a copy of an Rscript with the parameters in the script after it has ran - instead of args[1]. This would let the user be able to open the saved Rscript in Rstudio after the script has ran, with their argument instead of args[x] for any alterations they might want to make to plots.
For example:
Rscript plot_script.r path/to/file
read.table(args[1])
Should be:
read.table("path/to/file")
In the saved output Rscript.
Is this possible?

How to run R script containing interactive user options from a batch file

I have written an interactive R Script which gives the user certain options using which the rest of the execution takes place. These inputs have to be optional as required by the user. One other requirement from the user was that he did not want to manually select the entire script in RStudio just to execute it. I tried to create a BAT file for this execution. However, the program does not execute. Below is an example user interactive script.
FYI_Splitter <- winDialog(type = "yesno","Do you want to remove FYI from base file?
Yes = Remove FYI
No = Do Not Remove FYI")
Below is what I am using to execute the BAT file.
"C:\Program Files\R\R-3.4.2\bin\Rscript.exe" "<Path_of_file>\Test_Program.R"
I came to know that batch file execution does not support interactive sessions. Is there any way to make interactive sessions in R execute without selecting the entire code in RStudio? Any help on this would be appreciated.
You could try
echo source("<Path_of_file>\Test_Program.R") | R.exe --ess --vanilla
The interactive() command in R then returns TRUE for me.

Running R via terminal in Windows and leaving R session open

Suppose that I have a R script called test.R, stored at C:\, with the following content:
x <- "Hello Stackoverflowers"
print(x)
To run it via terminal one could simply call:
Rscript C:\test.R
And as expected, the result will be:
However, what I wonder is whether there is a way to run test.R via Windows console but after that staying within the executed R session instead of closing and going back to the console cursor? That is, staying inside the R session instead of going back, in the image above, to C:\R\R-3.4.1\bin>.
For instance, when compiling Python code with python.exe I can easily accomplish a similar thing by passing the -i parameter to the python.execall.
How could I do that with R?
Add this to your .Rprofile:
STARTUP_FILE <- Sys.getenv("STARTUP_FILE")
if (file.exsts(STARTUP_FILE)) source(STARTUP_FILE)
and then set the indicated environment variable outside of R and then run R. e.g. from the Windows cmd line:
set STARTUP_FILE=C:\test.R
R
... R session ...
q()
Variations
There are many variations of this. For example, we could make a copy of the .Rprofile file in a specific directory such as ~/test, say, and add this code to that copy
source("~/test/test.R")
in which case R would only run test.R if R were started in that directory.

How to drop into R shell after executing commands from file in R

In Python, running the interpreter with the -i flag first executes the script, then drops back into the interpreter
python -i hello.py
Hello world
>>> print("Python ftw")
Python ftw
>>>
which allows me to type commands and reach the variables after execution.
With R, this seems to be of great difficulty. I have been searching online for some time, and am surprised to see there is not so many results with the keywords "R run file shell interpreter".
With R, you can use
$ R -f myfile.R which executes and then exits the interpreter
$ Rscript myfile.R which still does the same thing.
Even worse, it does not plot when run like this and just exits without showing any signs that something has been plotted.
So, to repeat my question:
How do I make R to drop into the R shell after running commands from a file, a.k.a. a script?
Concurrently, how can I make R really plot the plots and not close them off immediately?
I can do these with Python, MATLAB, Octave, Ruby and many others, and should be able to do with R too.
I will answer your two questions separately:
How do I drop into a shell after my script has executed?
The function "browser" called with no arguments will allow to to drop into a shell on the line that it's called. Appending this to your script should do the trick.
How do I save graphics when not run in interactive mode?
First, check that there isn't a pdf file being created in your working directory. Depending on how you're running R, I believe it may be named "Rplots.pdf". Personally, however, I prefer to explicitly save graphics to a particular file, as such:
pdf("temp.pdf")
plot(rnorm(100))
dev.off()
which will save the plot in a new file called temp.pdf (and will overwrite any existing file by that name, so watch out).
Functions analagous to "pdf" exist for other image formats if you would prefer that.

Is there any way in Linux to show what's going on about the freezing code on the R session?

I am running a set of selected code on R. Like
source("/tmp/r-plugin-honli/Rsource-2704-quantmod.R")
There is no output. Only the prompt '>' flickered there.
I use 'killall' to kill the R session. But I don't know where is wrong on the code. Because R did not give any output. How could I know what's going on about the code.
I'd try two things:
Run the code interactively. As in, open the Rsource-2704 file and run its lines one by one.
If that doesn't replicate the problem or is not possible, you can take Joshua Ulrich's suggestion or use:
R CMD BATCH --vanilla Rsource-2704-quantmod.R out.log
Which will run the code in a batch mode and output the usual console lines to a file called out.log (you can name it whatever you like).
Instead of using print statements, you could also take a look at the browser() command. This drops you into an interactive session at the point where the command is put. This works particularly well when trying to figure out what is happening inside a function, although I don't know if your script contains them.

Resources