I'm always faced with a problem of re-installing all the packages I've already installed in Rstudio whenever I'm trying to make use of it. I want to believe installed packages should still be available for me to make use of some other time but it's not so in my case. Now, I even find it difficult to install any package, it kept telling me 'package not available'. Please what do I need to do. Thanks
Any time R session restarts you'll need to reload any packages your code depends on.
If it's really important, you can automatically load packages each time you open a project with a .Renviron
file. But that can lead to issues with reproducibility.
Sean Lopp wrote a nice article on RStudio's startup process here:
Another discussion from Colin Gillespie's Efficient R programming >> 3.3 R startup
https://csgillespie.github.io/efficientR/3-3-r-startup.html#r-startup
In my experience, the 'package not available' most frequently occurs when I had a typo in the package name, for exmaple:
install.packages('thispackagedoesnexists')
#> Installing package into 'C:/Users/OKComputer/Documents/R/win-library/3.5'
#> (as 'lib' is unspecified)
#> Warning: package 'thispackagedoesnexists' is not available (for R version
#> 3.5.0)
Created on 2018-08-08 by the reprex package (v0.2.0).
If I've completely missed your point (wouldn't be my first time!), could you supply a reprex of the error or additional details, such as system info?
system Information that is often helpful
#### System Information:
- RStudio Edition: (Desktop or Server)
- RStudio Version:
- OS Version:
- R Version:
#### Also:
<!-- Depending on your issue, the following may be useful /-->
<!-- If a section isn't relevant or you can't collect it just delete that section below /-->
- RStudio diagnostics report: <!--see rstd.io/support-diagnostics-report /-->
- Your `sessionInfo()`:
- RStudio crash report: <!-- rstd.io/support-crash-report /-->
- RStudio application log files: <!-- rstd.io/support-ide-log-files /-->
Thanks alot, I think the question has been addressed. So, it means whenever I'm restarting Rstudio, I'll have to do fresh installation for my required packages. I'd thought otherwise seeing that I'm new to R. Thanks for your time.
Well I think it is important to note the difference between installing packages and loading packages.
To install packages you use either install.packages("my_package")
or devtools::install_github("my_package_repo/my_package")
. This is not something that you should have to do every time you open RStudio. If you have already installed the package than it should be available the next time you open RStudio.
To load a package you need to use library(my_package)
. This is something that you will have to do every time you open RStudio.
Can you confirm which of these you are trying to do?
I think what I've been leaving behind which has been source of my problem is that I don't normally load packages whenever I'm just trying to make use of Rstudio afresh. I just lunch it and run codes and it keeps feeding me back with error, can't find ggplot in case I'm trying to make a chat for example. I never knew I'll have to load packages afresh before working on Rstudio, I just started using Rstudio like a week ago. Also, using the command library(my_package), will i have to load the packages one by one? Thanks
Yes, but you only need to load the packages whose functions you are actually using in that session. You don’t need to load all the packages you have installed.
R loads a small set of core packages by default (as described in one of @EconomiCurtis’s links). For the rest, it’s up to the user. Most people start out just giving R commands at the console, but eventually you should move toward writing scripts. The first few lines of most scripts are library()
statements loading all the packages necessary for that script.
Since you’re just getting started, here’s another tip to help you navigate the landscape better: R and RStudio are different things (see: Differentiating R from RStudio). What we’re discussing in this thread is actually how R works (regardless of whether you’re using RStudio or not).