Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make R project Automatically open Specific Scripts

I am working in team, we mainly use R, I am quite used to use R project in Rstudio, which I like because when I open them I have all my scripts and everything at the right place. However when another member of the team opens one of my project it loads the values and data but does not open the R script (one can see that by physically clicking on the project through the windows explorer rather than using the menu at the top right in R). I guess something can be done in the .Rprofile but I did not find any command to open physically a script, I tried

file.edit("./Main.R") 

but it did not open anything. It just got me the message :

Error: could not find function "file.edit"

As always, Thanks for your help !

**EDIT I tried to use

file.show
file.edit 
shell.exec(file.path(getwd()), "Main.R")) 

in the .Rprofile. Nothing worked.

Romain

like image 663
Romain Avatar asked Oct 20 '14 19:10

Romain


People also ask

Can you automate R scripts?

2022-03-16. Schedule R scripts/processes with the Windows task scheduler. This allows R users working on Windows to automate R processes on specific timepoints from R itself.

How do I open a saved script in R?

An R script is just a plain text file that you save R code in. You can open an R script in RStudio by going to File > New File > R script in the menu bar.

How do I run an entire R script in RStudio?

To run the entire document press the Ctrl+Shift+Enter key (or use the Source toolbar button).


1 Answers

You can use the following code in the .Rprofile file.

setHook("rstudio.sessionInit", function(newSession) {
  if (newSession)
    rstudioapi::navigateToFile('<file name>', line = -1L, column = -1L)
}, action = "append")

The rstudioapi library has the function navigateToFile to open a file in Rstudio. The problem is that the code in the .Rprofile runs before Rstudio initialization. To deal with this problem you can use the setHook function (from base package) to make the code execute after the Rstudio initialization.

like image 104
rowang Avatar answered Sep 21 '22 11:09

rowang