6

I've been using for while Rstudio with R pre-installed, as usual. Some days ago I decided to run absolutely everything I can in the terminal/CLI, since some GUI/IDEs keep crashing over and over and doing my stuff in vim seem to me to be enough. I googled and tried to run a R script that I used to run with Rstudio, now using Rscript. I also added R and Rscript to PATH, ran chmod +x to the file, added the shebang to the R file, and yet the message that I get is this:

Error: RStudio not running
Execution halted

So, what am I doing wrong? Why does it seem Rstudio is trying to run this script if I haven't even tried to use it in the command line? I have erased almost all the code and left only one line, that calls another R script, which clears the environment:

#!/usr/bin/Rscript

# Clears everything
source("cleanAll.R")

cleanAll.R code:

rm(list = ls())     # Clears global environment
cat("\014")         # Clears the console
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
graphics.off()
2
  • It seems you are trying to run a function in your rscript that has to be called inside RStudio. What does this script do?
    – Colin FAY
    Commented Feb 5, 2018 at 22:48
  • Not precisely a function, but actually another file in the same directory. Is it this what is calling Rstudio? It is not possible to use the source command without running Rstudio? I added the script code to the question.
    – user260876
    Commented Feb 5, 2018 at 22:55

2 Answers 2

9

You are trying to use the {rstudioapi} package.

You can't use this package unless you are in an interactive RStudio session. Hence the error Error: RStudio not running.

Are you trying to get the working directory? You should use getwd() if this is the case.

0
4

Remove setwd() command in .R file and run, It is working perfectly

2
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
    – Enea Dume
    Commented May 15, 2019 at 12:56
  • Hi @EneaDume I clarified author answer, Because in his code there is a command used called setwd(), If he remove that command, Code is working fine without 'Error: RStudio not running' this error. Thanks Commented May 15, 2019 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.