Running R via terminal in Windows and leaving R session open - r

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.

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 Python, R, Octave or Julia using BowPad?

I try using a very light-weighted editor BowPad to edit and run codes.
Take running R as an example, which has been added to Windows PATH Environment Variable D:\R\R-3.6.2\bin\x64
I try to take some following steps:
I built a test.r writing some testing line codes as 1+2; plot(1:10)
Menu --> Run --> Configure custom commands
Command Name: run R
Command Line: D:\R\R-3.6.2\bin\R.exe $(SEL_TEXT)
$(LINE) The line where the cursor is
$(POS) the position where the cursor is
$(TAB_PATH) the path to the file of the active tab
$(TAB_NAME) the file name without extension of the current tab
$(TAB_EXT) the file extension of the current tab
$(TAB_DIR) the directory of the file of the active tab
$(SEL_TEXT) the selected text or the word at the cursor position
$(SEL_TEXT_ESCAPED) like $(SEL_TEXT) but escaped, useful for urls
When I press to choose run R to run the code, something wrong happens!
It will open a Rterm(64-bit) window each time when I run R!
it shows RGUMENT '1+2' __ignored__ or ARGUMENT 'plot(1:10)' __ignored__
Did I missing something ? BTW, can we set some shortcuts keys for running Python, R, Octave or Julia?
Also, when I reach the homepage of BowPad for more information, little usage of this software can be found!
R.exe refers to Rterm. You can use Rgui.exe instead but that won't automatically process arguments. I think what you likely want is to send the selected text to a running R session rather than starting up a new R session each time.
To do that
download this file and optionally place it on your path https://raw.githubusercontent.com/ggrothendieck/batchfiles/master/clip2r.js
in the Bowpad Run | Configure window configure a Run R command as the following (or if clip2r.js is not on your path use the entire pathname). It should be just like this with no arguments.
clip2r.js
From the Windows cmd line if Rgui is running we see that tasklist | findstr Rgui finds Rgui as the R gui process and you can create additional js scripts by editing clip2r.js replacing Rgui in the js script with whatever is the appropriate word to locate python, julia, octave or other R front end assuming again that they are running.
Now to invoke it from within Bowpad:
ensure that your Rgui session is already running and
from within Bowpad select the code you want to run and copy it to the clipboard and then invoke Run R. For example, to run everything use ctrl A ctrl C ctrl R 0 assuming that Run R is in position 0 in the configure menu. Alternately use the mouse with the Ribbon.
Regarding the comment about difficulty finding help, it looks like Bowpad is based on Scintilla so the documentation for it and the related SciTE editor likely apply to Bowpad too. You can also check the Bowpad source code on github.

Run same R script from terminal with different arguments in parallel

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.

Rscript - Using same R-file, The R terminal behaves different compared to Rstudio

Problem: Using same R-file, The R terminal behaves different compared to Rstudio.
When running below R-file several times, in Rstudio I get the correct behaviour.
First run [count=20], rest of run [count=1].
When running same R-file several times from a terminal, using [Rscript]:
First run [count=20], rest of run [count=20].
Wanted behaviour:
I need the R terminal to behave same as R studio , creating counter with value [20] and for the rest of the times put it to value [1].
My environment:
Ubuntu Linux 18.04
R-studio: 1.1.453
Terminal (Bash 4.4.19, R v.3.4.4)
Content of R-file:
setwd ("/tmp-r") # Set working directory.
# Set [count] to 20 if [count] does not exists.
# Set [count] to 1 if [count] exists.
if (!exists('count')) {
count <- 20
} else {
count <- 1
}
save.image() # Save.
With input from comments, I found 2 issues that solves the problem. Both for the minified test-script I published as a question, and my expanded script.
1) Add [load ('RData')], in the beginning since the load behaviour differs between Rstudio and R terminal. Rstudio dynamically update the global environment whenever you send in a change in the Rstudio console. R terminal loses the session between calls and therefor R terminal needs the R-file to start with [load ('RData')].
2) To solve my expanded script, I found out that the local [.Rprofile] has a [save.image('.RData)] at the end of the script. When removing that command in [.Rprofile] it solves the bigger script.

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.

Resources