7

In Rstudio, I usually install R packages using the console by install.packages('pkg-name') command. However, when some R packages are required in an R script, Rstudio opens a pop-up and asks if I want to install those packages. If I click on "Install", it starts installing those packages inside the "Jobs" tab. This is particularly useful for me because my internet is slow and while a large package is being installed I can continue my works on the console tab. I want to know if there is any way to always install packages through this "Jobs" tab, without using the console.

2 Answers2

6

There's an R package {job} which can be used to run any r code directly from a script or console. To install a package as a Rstudio job:

# install.packages("job")

job::job({
  install.packages("pkg_name")
})

Also {job} provides Rstudio Addins to easily run selected code as a job. see here for more examples.

shafee
  • 15,566
  • 3
  • 19
  • 47
1

You can create a script, let's call it temp.R and include the install.packages command in it. Something like :

install.packages('dplyr', repos = "http://cran.us.r-project.org")
install.packages('tidyr', repos = "http://cran.us.r-project.org")

You can click on Start Local Job

enter image description here

and point to the location of the script and adjust any other setting that you want.

enter image description here

Now click on Start and use R/RStudio while the script finishes in the background.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thanks for your answer. I will accept it. However, typing `repos = "http://cran.us.r-project.org"` every time is not very handy. Is there any way to set a global repository so that `install.packages('pkg-name')` would be enough for the job? – Rasel Biswas Apr 29 '21 at 11:24
  • If there are lot of packages you can always set up a `for` loop or `lapply` to do the work so that you don't have to copy-paste `repos = "http://cran.us.r-project.org"` everytime. See this answer on how to do that. https://stackoverflow.com/a/48830451/3962914 – Ronak Shah Apr 29 '21 at 12:15
  • Why do you say to specify the repo every time? I install packages through jobs with just `install.packages("package_name")` and it works fine with my default repo – stlba May 05 '21 at 09:23
  • @stlba Well, it didn't for me hence I specified the repo. – Ronak Shah May 05 '21 at 10:11