Running R via terminal in Windows and leaving R session open

三世轮回 提交于 2019-12-07 18:40:04

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/45894892/running-r-via-terminal-in-windows-and-leaving-r-session-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!