Rscript-使用相同的R文件,R终端的行为与Rstudio不同



问题:使用相同的R文件,R终端的行为与Rstudio不同。

当在R文件下运行几次时,在Rstudio中我得到了正确的行为。

  • 第一次运行[count=20],其余运行[count=1]

从终端多次运行同一R文件时,使用[Rscript]:

  • 第一次运行[count=20],其余运行[count=20]

通缉行为:

我需要R终端的行为与R studio相同,创建值为[20]的计数器,并在其余时间将其设置为值[1]。

我的环境:

Ubuntu Linux 18.04

R-工作室:1.1.453

终端(Bash 4.4.19,R v.3.4.4(

R-文件的内容:

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.

通过注释输入,我发现了两个问题,解决了这个问题。这两个都是我作为问题发布的缩小测试脚本,以及我的扩展脚本。

1( 在开头添加[load('RData'(],因为Rstudio和R终端之间的负载行为不同。每当您在Rstudio控制台中发送更改时,Rstudio都会动态更新全局环境。R终端丢失呼叫之间的会话,因此R终端需要R文件以[load('RData'(]开头。

2( 为了解决我的扩展脚本,我发现本地的[.Rprofile]在脚本末尾有一个[save.image('.RData(]。在[.Rprofile]中删除该命令时,会解决较大的脚本。

相关内容

最新更新