How do I use R packages in an Anaconda environment that are not in the Anaconda r channel - r

I want to use Anaconda to provide separate isolated environments, each with different combinations of r-packages. If this was Python, I would specify an environment .yml file with Python Anaconda packages where those exist, and pip-ones for those that are not in Anaconda.
For R, a similar situation exist, some R packages are not in Anaconda. If I do install.packages("package_name") the packages end up in my user homedir (on windows in Documents/R/win-library...). This is not ideal, because these installed packages are not confined within the Anaconda environment and can be seen from all Anaconda environments.
Two questions:
How can I specify these (non-Anaconda r-packages) in the environment .yml file, if possible?
How can I install non-Anaconda r-packages so they are confined within an Anaconda environment.
In summary, I want to achieve environent isolation for R in the same manner it is achived with Python.
Regards Niels Jespersen

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.

Creating an R environment using anaconda

I want to create a new R environment using anaconda. Following this page 'https://docs.anaconda.com/anaconda/user-guide/tasks/using-r-language/' I created an environment called eeEnv_r using this command in anaconda prompt conda create -n r_env r-essentials r-base. However the environment that was created looks like it was python environment. But when I type conda list I see some r packages such as dplyr appearing in addition to some python packages. Also there is python.exe file in environment folder. Any ideas why this is ?
I am trying to use VS code to run R, since I am familier with the IDE (used for python work). Also I want to create environments in R and then use those environment ins VS code (similar to python).
r-base depends on glib``notebook, which depends on python, therefore installing the latest r-base package will always pull in a python interpreter as well.
I would look at this from a different angle: with conda there is no such thing as a python or r-environment environments can mix packages from various languages. For most use cases this should not be a problem.

How best to update / upgrade RStudio inside or underneath Anaconda Navigator?

The version of R Studio that is bundled with the latest release of the Anaconda distribution is over a year old RStudio in Anaconda Navigator, specifically version rstudio-1.1.456. If I check the Rstudio website current RStudio download, it says the latest version is RStudio-1.2.5033.exe.
I want to use the current RStudio release. Anaconda Navigator package manager doesn't give me an option to install any later versions. Also, Anaconda put my R-dedicated virtual environment (which I named Rdev') and installed R in a wierd place too:C:\Users\rlysakow.conda\envs\Rdev\Lib\R\bin`.
My judgement is that R and RStudio are basically UNIX programs that have been ported to Windows, and have all kinds of "leftover" Unix-izms and quirks in the way they reference files, directories, and scripts. Plus, Anaconda makes a lot of choices without always asking the user about where to put things.
It looks like to get a good RStudio update, I must update RStudio from within RStudio, but this will likely break a lot of Anaconda R functionality. RStudio doesn't use Anaconda Navigator's conda program to do the upgrade. The Anaconda Navigator's conda package index will likely not reflect the changes that RStudio makes.
Before I make a likely irreversible and breaking change to Anaconda, can anyone recommend a better way to do this Rstudio update and have Anaconda's package index correctly reflect changes that RStudio will make with executables and packages that it updates and installs?
Please do not recommend something like "ditch Anaconda and just use RStudio for all your R work", because that doesn't allow easy integration with Jupyter Notebook and the conda package manager inside Anaconda. I could install Jupyter Notebook separately, but then I have to manager that outside of Anaconda too. The end users of my application want to use Jupyter Notebook for their R work.

How do you add Jupyter Notebook kernels for prior versions of Julia?

I am using a Windows machine and trying to have Jupyter Notebook kernels for multiple versions of Julia (0.7.0 and 1.1.1) because package AWS does not support the latest version, but does support 0.7.0.
I had Julia 1.1.1 installed on my computer first and got something similar to the following error when I tried to install package AWS: https://github.com/JuliaLang/Pkg.jl/issues/792
Then I installed Julia 0.7.0 and was able to install AWS in the Julia 0.7.0 terminal with Pkg.add("AWS") with no problems.
In the Julia 0.7.0 terminal, I installed IJulia again with Pkg.add("IJulia") and restarted my Jupyter notebook instance. Now I'd like to use AWS via Jupyter notebook but when I create a new one, only Julia 1.1.1 appears.
I ended up having success by showing which kernels I had using jupyter kernelspec list in terminal, which showed where my other Julia kernel was located.
>>> jupyter kernelspec list
Available Kernels:
julia-1.1 C:\Users\{%USERNAME%}\AppData\Roaming\jupyter\kernels\julia-1.1
python3 C:\ProgramData\Anaconda3\share\jupyter\kernels\python3
I navigated to the file path listed after julia-1.1
Created a julia-0.7 folder in that same directory
Copied over contents from the julia-1.1 folder
Edited the kernel.json file by replacing every instance of julia-1.1.1 with julia-0.7.0
What I ended up having success with seems like a very rudimentary way to solve this problem. I'd like a more elegant way to achieve the same result, similar to when adding multiple kernels for different versions of Python. (Using both Python 2.x and Python 3.x in IPython Notebook)
Please help, thank you!
You (probably) just need to Pkg.build("IJulia") on the second Julia version.
Since Julia 0.7 the package manager uses separate directories for each version of a package, meaning that, from the package managers perspective, the package is already installed, and no downloading or building is performed when you install the same version from a different Julia version. The package manager does not know, however, that IJulia needs to be rebuilt for this new Julia version. You can trigger the build manually by Pkg.build("IJulia").

remove anaconda R from Ubuntu

I have been using ipython notebook to run some R scripts. Now the problem is I have two R versions on my Ubuntu 14.04.
One is R.3.2.2 at /home/MYNAME/anaconda2/bin/R, another one is the R which I need for R studio,
now the problem is I want to only use R.3.3.1 for my system as I need some advanced task to be done.
I use conda uninstall r, after running this, according to the print out, a lot of R related packages is removed, if i run conda uninstall r again, it said
Fetching package metadata .......
Using Anaconda Cloud api site https://api.anaconda.org
Solving package specifications: ..........
Error: no packages found to remove from environment: /home/MyName/anaconda2
but when I run Which R again, still it is the anaconda R, if I run R in the terminal, it is still R3.2.2, anyone knows how could I remove this anaconda R version?
You probably needed to run hash -r in your session (or rehash if using zsh) to update your executables on PATH for the which command. A new terminal session would also fix the problem.
you can try conda uninstall r-base, this will remove R and all of the R-library.
The questions also implies that one cannot use anaconda R with rstudio.
On Linux you can
export RSTUDIO_WHICH_R=/home/USER/anaconda3/bin/R
and add to .profile (d/o your distro) to use rstudio with anaconda R and packages

Resources