use*_*876 4 r command-line-interface rstudio
像往常一样,我一直在使用预装 R 的 Rstudio。几天前,我决定在终端/CLI 中运行我能运行的所有东西,因为一些 GUI/IDE 不断地崩溃,在我看来,在 vim 中做我的事情就足够了。我用谷歌搜索并尝试运行我曾经用 Rstudio 运行的 R 脚本,现在使用 Rscript。我还将 R 和 Rscript 添加到 PATH,运行chmod +x到文件,将 shebang 添加到 R 文件,但我得到的消息是:
Error: RStudio not running
Execution halted
Run Code Online (Sandbox Code Playgroud)
那么,我做错了什么?如果我什至没有尝试在命令行中使用它,为什么 Rstudio 似乎正在尝试运行该脚本?我已经删除了几乎所有的代码,只留下一行,它调用另一个 R 脚本,它清除了环境:
#!/usr/bin/Rscript
# Clears everything
source("cleanAll.R")
Run Code Online (Sandbox Code Playgroud)
cleanAll.R 代码:
rm(list = ls()) # Clears global environment
cat("\014") # Clears the console
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
graphics.off()
Run Code Online (Sandbox Code Playgroud)
您正在尝试使用 {rstudioapi} 包。
除非您处于交互式 RStudio 会话中,否则您无法使用此包。因此错误Error: RStudio not running。
您是否正在尝试获取工作目录?getwd()
如果是这种情况,您应该使用。