Installing a package in R inside a script - r

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)

Related

Running R from Excel using specific old R packages

First time question asker.
I created an Excel/R tool that uses:
Excel VBA to create a CSV file with data for R,
Launch R using a Windows Shell,
Detects when R is finished running and then
Imports the results in a CSV file created by the R script.
Unfortunately, the R code does not work as programed with some package versions created after 3/1/2020, which creates problems for new users because they cannot just install the current package versions or users who want to use conflicting versions for other projects.
I have a solution for users who do not require newer versions of R for their other work; writing a script that installs all the packages and their dependents using the “versions” package. However, I think this approach will constrain users who want to use newer versions of R. **Is this a correct assumption? **
I thought the {checkpoint} package might offer a good solution. I can get it to run in well in RStudio by creating a RStudio project (where I ran the {checkpoint} package to install the 3/1/2020 versions of my packages). However, I have not found a way to run the R script from the Windows Shell. The R script does not seem to be able to access the packages installed in the RStudio project using {checkpoint}. Does anyone have ideas of how I can have Excel VBA launch the start of the R script in a way that it can assess the packages installed in the RStudio project by {checkpoint}? Perhaps there is a Windows Shell call for RStudio similar to the one I use now for R?
Here is the Windows Shell code I currently use for R in case it helps. It works with the versions approach but not the {checkpoint} approach.
rExeCall = "C:\Program Files\R\R-3.6.2\bin\Rscript.exe"
rExeOptns = " --no-environ --no-init-file --no-restore --verbose "
rscrpt=”Tool.R”
Shell (rExeCall & rExeOptns & rscrpt)
Thank you in advance for your help!
I had hoped that my current R shell code would work when I used {checkpoint} to install the correct versions of the packages in an RStudio project.
I tried specifying the .libPaths to the file location for the RStudio projection (per Running R script from PHP in VSCode not recognizing R packages) without success.

Install R packages on the fly

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

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.

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.

Can Rcpp package DLLs be unloaded without restarting R?

When installing a Rcpp package on Windows, you need to ensure that the package DLL is unloaded or you get a "Permission Denied" error when copying the new DLL. This means restarting R on every recompile, which is pretty annoying. Is there any way to unload the package DLL without killing R?
I've tried the detach("package:my_package", force=TRUE) command, but it doesnt unload the DLL.
Opinions are divided on this. I often prefer to run the builds and test outside of my main R session(s), simply by chaining R CMD INSTALL with Rscript (or, on Linux, r calls from littler) to test the new build. If you use proper options to R CMD INSTALL ... to skip parts that may take extra time you get a quick turnaround AND are assured that you do get a fresh build.
And if you want the same behaviour by clicking a button, RStudio offers it too.
If you want to do this in your main R session (without using RStudio, which makes reinstalling the package and reloading R very easy), you can use devtools:
library(devtools)
load_all("path/to/my/package")
Among other things, load_all will reload all your R code, and re-compile and reattach the DLL.
I guess you need to run library.dynam.unload to unload DLLs.

Resources