Install R packages on the fly - r

Whenever I want to run a complex application in R that needs many packages and libraries, I have to install one by one all the dependencies and then re run again to see the next dependency. I am having the same problem with running a shinydashboard app. I wonder if there is a way the first time I am about to execute an R script, at the same time to install on the fly all the dependencies this script needs. I am running an R server on windows. Thanks!

Perhaps you want to use pacman
library(pacman)
p_load(dplyr, reshape2) # install if needed, then load

Related

R equivalent to pip install -e

When developing your own R package locally on your machine with devtools, is there is an editable install option? I.e. if you make a local package in python you can pip install with -e which means that if you then change the scripts of the package then it automatically updates your package rather than working with a frozen version since (I believe) it creates pointers to scripts rather than compiled copies. The alternative of removing and reinstalling the package each time is 20 seconds of my life I could do without after each change. I know I could source(rscript) but then I would lose the fact it is a package, for example package::function would no longer work.

Installing a package in R inside a script

I'm a beginner with R. I want to launch R scripts at specific moments, managing this with Linux's cron and launching the scripts as Rscript name_of_the_script.
I have installed tidyverse in Rstudio, with install.packages("tidyverse"). Ok, but I guess that installation is specific to the Rstudio environment. When working in a script (not using Rstudio), and launching that script with Rscript, the library tidyverse is not installed. Even worse, I couldn't install it in the script with install.packages("tidyverse").
What do you suggest? Thanks.
The library is, in fact, probably installed. It is difficult to be sure of what is the problem without more details, but I would guess that you did not load your library in your script. Try to add the following at the beginning on the first line of your script
library(tidyverse)
The solution is you simply use require() to load your package without worrying about the path.
require(dplyr)

R install packages from Shell

I am trying to implement a reducer for Hadoop Streaming using R. However, I need to figure out a way to access certain libraries that are not built in R, dplyr..etc. Based on my research seems like there are two approaches:
(1) In the reducer code, install the required libraries to a temporary folder and they will be disposed when the session is done, like this:
.libPaths(c(.libPaths(), temp <- tempdir()))
install.packages("dplyr", lib=temp, repos='http://cran.us.r-project.org')
library(dplyr)
...
However, this approach will have a dramatic overhead depending on how many libraries you are trying to install. So most of the time will be wasted on installing libraries(sophisticated libraries like dplyr has tons of dependencies which will take minutes to install on a vanilla R session).
So sounds like I need to install it before hand, which leads us to approach2.
(2) My cluster is fairly big. And I have to use some tool like Ansible to make it work. So I prefer to have one Linux shell command to install the library. I have seen R CMD INSTALL... before, however, it feels like will only install packages from source file instead of doing install.packages() in R console, figure out the mirror, pull the source file, install it in one command.
Can anyone show me how to use one command line in shell to non-interactively install a R package?
(sorry for this much background knowledge, if anyone thinks I am not even following the right phylosophy, feel free to leave in the comment how this whole cluster R package should be managed.)
tl;dr
Rscript -e 'install.packages("drat", repos="https://cloud.r-project.org")'
You mentioned you are trying to install dplyr into custom lib location on your disk. Be aware that dplyr package does not support that. You can read more in dplyr#4641.
Moreover if you are installing private package published in internal CRAN-like repository (created by drat or tools::write_PACKAGES), you can easily combine repos argument and resolve dependencies from CRAN automatically.
Rscript -e 'install.packages("priv.pkg", repos=c("cran.priv","https://cloud.r-project.org"))'
This is very handy feature of R repositories, although for production use I would recommend to cache packages from CRAN locally, and use those, so you will never be surprised by a breaking changes in your dependencies. For quality information about handling R in production I suggest to look into talk by Wit Jakuczun at WhyR2019 How to make R great for machine learning in (not only) Enterprise: slides, video.
You may find littler useful. It is a command-line front-end / variant of R (which uses the R-embedding interface).
I use the install.r script all the time to install package from the shell. There is a second variant with more command-line argument parsing but it has an added dependency.

Checking installed version of R package and update if old

We are managing the R packages in cluster via puppet and we have created one file which has commands like below. We have mirror of R package repo internally.
install.packages("BH",repos=NULL, dependencies=TRUE, contriburl=http://our_internal_repo.com)
in the rPackages.xt
This using puppet we are executing via RScript rPackages.txt
Now in next week we get 3 more additional packages , we modify the rPackages to include additional lines for new packages.
Now since scirpt will read from start to end , it will try to reinstall all packages.
My question is , how to install package only if the version installed is not same as the one present in our internal repo.
How to do those checks in the RScript and execute puppet accordingly.
What are the best practices to manage R installations.
Thanks
Change the Rscript to
if("BH" %in% rownames(installed.packages()) == FALSE) {install.packages("BH")}
as for the version, you could probably use packageVersion somehow.

R develop packages without installing every time

I am developing an R package, but every time I make some modifications
I have to use R CMD INSTALL to install it and see if the new version
is working.
I would like to know if there is some easier way to develop a package in R.
Specifically I would like to be able to develop the package without having
to install it every time I want to test it.
If you are familiar with Python and setuptools, I would like to achieve the
same effect you get using
python setup.py develop.
Install devtools, then all you need to do is:
require(devtools)
load_all("/wherever/your/package/is")
It reloads all the changed code in .R files, recompiles, links, loads C code and so on.
devtools will also compile your documentation, and run checks.
Nothing else comes close for package development.

Resources