在Windows中通过终端运行R并保持R会话打开 [英] Running R via terminal in Windows and leaving R session open

查看:199
本文介绍了在Windows中通过终端运行R并保持R会话打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为 test.R 的R脚本,存储在 C:\ 中,并且以下内容:

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:

但是,我想知道的是是否有一种方法可以通过Windows控制台运行 test.R ,但之后仍停留在已执行的R会话中,而不是关闭并返回控制台光标?也就是说,留在R会话中,而不是返回上图中的 C:\R\R-3.4.1\bin>

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>.

例如,当使用 python.exe 编译Python代码时,我可以通过传递 -i 参数调用 python.exe

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.

如何

推荐答案

将此添加到您的.Rprofile:

Add this to your .Rprofile:

STARTUP_FILE <- Sys.getenv("STARTUP_FILE")
if (file.exsts(STARTUP_FILE)) source(STARTUP_FILE) 

,然后在R之外设置指示的环境变量,然后运行R。例如从Windows cmd行:

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()

变化

有很多变化。例如,我们可以在特定目录(例如〜/ test)中复制.Rprofile文件,并将此代码添加到该副本中。

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")

在这种情况下如果R在该目录中启动,R只会运行test.R。

in which case R would only run test.R if R were started in that directory.

这篇关于在Windows中通过终端运行R并保持R会话打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆