Running R from Excel using specific old R packages - r

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.

Related

Can renv be used to create a virtual environment with a specific version of R?

I recently installed R 4.0, after previously using relying R 3.6.3. To manage R repositories, I use Rstudio (currently 1.2.5042 on a Windows 10 machine). After upgrading to R 4.0, I opened a project from a few months ago, and realized that Rstudio is now, by default, using the newer version of R (and it's library folder). When running renv::restore(), renv attempts to re-install all libraries in the .lock file for the newer version of R, and I don't see any way to specify that I want to keep using R 3.6.3 and it's associated library.
Coming from a python background, I had assumed that renv would create a virtual environment that isolates both the interpreter and the libraries that the project uses (similar to how anaconda environments are created). However, after looking through the documentation and doing a few searches, I have found no reference to isolating a particular version of R. I have, however, found that Rstudio defaults to using the latest version of R, which is not necessarily the behaviour that I want.
I have tried using anaconda to manage an R environment. However, Anaconda relies on its own smaller repository of R packages, and many of the libraries I need are from researchers that house their code on GitHub.
Is there a way to create an R environment in which I can isolate both the R libraries and the version of R itself? Or, perhaps there is something I am missing about how environments with R/Rstudio are intended to be used?
You are correct that renv only manages the installed R packages, and not the R interpreter itself.
Depending on how you're using RStudio, you can still "fake" this by setting the RSTUDIO_WHICH_R environment variable. For example:
export RSTUDIO_WHICH_R=/path/to/R
rstudio
would tell RStudio to "bind" to the version of R specified by the RSTUDIO_WHICH_R environment variable.
For what it's worth, the ability to bind projects to a specific version of R is a feature of the professional editions of RStudio; however, it's not available in the open-source version. See here for more details.

How is to add R and Rtools paths in the environment variable?

I have installed R-3.5.1 and Rtools35.exe. But I am unable to install packages in R. Somewhere I got,
Make sure the R and Rtools paths are added in the environment variable.
But I don't know what is environment variable and how is to add R and Rtools paths in the environment variable?
Edit
The answer provided How to add Rtools\bin to the system path in R requires library(devtools). But install.packages("devtools") isn't working.
Installing package that has heavy dependencies just to set environment variables doesn't seem to be good solution.
Below solution is for R 4.0.0 and rtools40.
This uses powershell bash, it should be available on all recent Windows versions.
$ENV:PATH = "C:\R\bin;C:\rtools40\usr\bin;$ENV:PATH"
You can see full R 4.0.0 setup on Windows in this CI yaml.
Note that in powershell it is best to refer to applications using .exe suffix, because powershell has many aliases, so using R or curl will not do what you expect, you need R.exe or curl.exe instead.

Which runtime is getting referenced

I have installed R at following location C:\E_Drive\ProgramFiles\R-3.4.3, so I think R environment installed at C:\E_Drive\ProgramFiles\R-3.4.3 will be used when I run the R console, right? Please correct me if I am wrong.
Now, I install a package using the R console.
Now, I download and install RStudio and from the R console of RStudio if I check whether that package is available or not then I see that it is available. I am wondering, how RStudio's R console reported that package is available, I didn't expect that since C:\E_Drive\ProgramFiles\R-3.4.3 is not on my PATH and in no way is linked RStudio with C:\E_Drive\ProgramFiles\R-3.4.3, so I thought RStudio would be referring its own R environment.
Can you please help me understand how my RStudio is referencing the C:\E_Drive\ProgramFiles\R-3.4.3 R environment.
R normally installs packages in the same directory tree as its own binary, but it can also install them elsewhere. On Windows, this generally happens because regular users often don't have write permission in the Program Files directory. The standard Windows installer also records R's location in the registry, so that it doesn't need to be on the PATH to be found by RStudio.
You can find out where Windows or RStudio found R by running R.home() within R. You can find out where R is finding packages in a particular session by running .libPaths().

How does Anaconda work with installing packages?

I just downloaded anaconda and downloaded their basic R package.
I also got some packages from anaconda, using the anaconda terminal commands that they provide on their website.
My question is -- when I am starting a new R session in r studio, do I still need to install.packages()? Can I just load the package?
When I press libary(rJava) for example -- the R command line doesn't say anything back on whether it was successful, that's why I'm not sure. Thanks.
if you have installed the R packages via the R command line then you can just directly load them. I would recommend that you use the command line rather than R studio.

R CMD INSTALL --build package --> "vignettes missing"

Problem:
C:\>Rcmd.exe INSTALL --build --library=C:/Users/local_aphalo/Documents/R/win-library/3.0 photobiology
C:\>Rcmd.exe INSTALL --build --library=C:/Users/local_aphalo/Documents/R/win-library/3.0 photobiology_0.2.6.tar.gz
The first command (as used by RStudio) builds a ZIP file that is missing the vignettes.
The second command builds a ZIP that includes the vignettes.
Using R CMD instead of Rcmd.exe makes no difference. The .tar.gz was built immediately before attempting to build the .zip file, from exactly the same source files, from within RStudio (which uses Rcmd.exe build photobiology).
The vignettes are coded in .Snw files using knitr, documentation and NAMESPACE use ROxygen2. The problem happens on all of the packages that I have tried to build, but they are very similarly coded. Only one of them uses Rcpp.
When installing the package for use from within RStudio, installing from .tar.gz installs vignettes just fine. If installing from .zip, whether vignettes get installed or not, depends on whether the .zip files contains them or not (which depends on which of the two commands at the top of this message was used to build the .zip file).
I am using R 3.0.1, and also tried a couple of R 3.0.1 patched builds a few days back. I am mostly using Windows 7 (both 32 bit, and 64 bit), I tried once under Ubuntu 64bit, and the problem is reproducible. I first noticed the problem when using RStudio (0.97 and 0.98) and posted a message in the RStudio forum, but have received no answer in a couple of weeks. I have found at least another relatively old post about this problem in the RStudio website forum, but it has not been answered. Today, I investigated a bit further, and the problem is clearly not related to RStudio, as I can reproduce it through the command line.
The question is: Is this behavior a feature? a bug? or I am missing just an option in the command used?
Of course, I can easily work around the problem at the command line by using the .tar.gz file to build the .zip file, but as I think the preferred way of building a package is by just supplying the package name as argument.
Thanks for any insights on the origin of this problem.
I think this is a feature:
if you're installing from source, vignettes are always built
if you're installing from a binary, they're not built, and will only be available if they were built when the binary was made
This approach means that you can distribute vignettes in binary packages to people who might not be able to build them from source.

Resources