15

Every time I restart RStudio-it requires me to reload all of the packages that were loaded in the workspace previously. I can't seem to figure out what the problem is, RStudio is saving the projects when it closes them.

How can I make sure that RStudio reloads the necessary packages when I open the project?

Joris Meys
  • 106,551
  • 31
  • 221
  • 263
bwilliams18
  • 359
  • 2
  • 3
  • 9
  • You need to provide us with at least the basic information about which version of RStudio you use and which version of R is coupled to it. But well... – Joris Meys Jan 09 '13 at 15:17
  • 1
    @Gsee said this, but I'll make the more question more explicit. Do you mean `install.packages()` or `library()` or `require()`? – Andrie Jan 09 '13 at 15:30
  • 3
    Assuming it is "library": I would like to point out that possibley the OP's workflow is not optimal. You should try to write a script that does everything, including the required library calls. As a test if you are doing things right: try to restart R (from R-Studio it is easy), and run your script. If you get error, wrong workflow. – Dieter Menne Jan 09 '13 at 15:34
  • Edited the question so it actually makes sense, as the OP didn't move but was active before. – Joris Meys Jan 10 '13 at 11:10
  • Related, but not exact duplicate: [How to load packages in R automatically?](https://stackoverflow.com/questions/10300769/how-to-load-packages-in-r-automatically). See my answer here for what's distinct about RStudio. – smci Feb 19 '18 at 03:51

2 Answers2

31

I presume you want to say that you have to reload all of the packages that were loaded in the workspace previously. That's not an error, that's by design.

If you want to load some packages at startup in a project, you can do so by creating a file called .Rprofile in the project directory, and specify whatever code you want RStudio to run when loading the project.

For example:

cat("Welcome to this project.\n")
require(ggplot2)
require(zoo)

would print a welcome message in the console, and load ggplot2 and zoo every time you open the project.

See also http://www.rstudio.com/ide/docs/using/projects

Joris Meys
  • 106,551
  • 31
  • 221
  • 263
  • 1
    If Joris's assumption is correct, this question would be a duplicate of this: http://stackoverflow.com/questions/10300769/how-to-load-packages-in-r-automatically/14708285#14708285. However, it appears before that question if you google "automatically open packages r". – Mikko Feb 05 '13 at 13:14
  • 2
    @Largh Not exactly a duplicate. Note the difference in where you have to save the `.Rprofile` file when talking about projects in RStudio versus loading in R at startup. Although the mechanism behind both approaches is the same, the place where you save the file really matters in this case. – Joris Meys Feb 05 '13 at 15:49
  • 1
    I got slightly different behavior -- I had to include both "require" and "library" commands in .Rprofile in order to automatically load the package. I don't understand why this is -- the documentation indicates that either should be sufficient, but I double-checked and I need to call both in order to have access to the methods. I am using R 3.0.3 and RStudio 0.98. The package is seqinr, if that matters. While working from the console, either command works. Weird. – adam.r Mar 31 '14 at 16:01
  • If you are using Windows and want to create ``.Rprofile``, there will be an error message. Create ``.Rprofile.`` instead (see the additional dot after the filename). The last dot will be omitted and the file is there. (found here: https://gist.github.com/ozh/4131243) – Phann Aug 01 '19 at 11:31
  • @Phann You can, but you have to set your Windows to allow it. You can't through the normal explorer. But in R you can do `file.create(".Rprofile")` without problems. Just make sure your Windows is set to show hidden files, or you won't find it. – Joris Meys Aug 07 '19 at 08:14
  • I can note in Win10 today it worked without a trailing period, and it needed to be UTF-8 without BOM. – Mike M Aug 20 '20 at 02:40
3

In general there's nothing different to default package loading in RStudio than in R (How to load packages in R automatically?). Upon startup R checks for an .Rprofile file in either your local, or fail, that, home or install directory (on Mac/Linux: ./.Rprofile or else ~/.Rprofile) and executes it, and hence any options(defaultPackages...)) or other package-load-related commands it contains.

The only small difference is that RStudio "helpfully" changes your default path before startup see "RStudio: Working with Projects", so you might load a different or missing .Rprofile or the wrong .Rprofile, depending on whether you've opened an RStudio Project or just plain files, and what your RStudio default working directory is set to. It's not always clear what directory you're in, so sometimes this causes real grief.

I tend to use RStudio without defining my code as an RStudio Project, simply because it's heavy-handed and creates more files and directories without adding anything (to my use case, anyway). So the solution I found to maintaining .Rprofile and making sure the right one gets loaded is a trusty old Unix link from the project directory to my ~

ln -s ~/.Rprofile ./.Rprofile

(If you're on Windows it's more painful.)

You don't need to have one global .Rprofile, you could keep task-specific ones for different types of projects, or trees, or (say) a .Rprofile.nlp, .Rprofile.financial, .Rprofile.bio and so on. As well as options(default.packages, you can gather all your thematically-related settings: scipen, width, data.table/dplyr-specific options, searchpath...

Power tips:

  • obviously keep backups or SCM of your valuable .Rprofile(s)). Definitely make sure git is tracking it, so don't put it in .gitignore
  • if you have multiple .Rprofiles, put a cat("Loading .Rprofile.foo") line in each one so you can see from console that the right .Rprofile.xyz got loaded
  • after every project, revise, trim, tweak your .Rprofile; add new use case stuff, comment out irrelevant stuff, commit the changes to git
smci
  • 32,567
  • 20
  • 113
  • 146