Install R packages through "Jobs" tab in Rstudio - r

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.

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.

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
and point to the location of the script and adjust any other setting that you want.
Now click on Start and use R/RStudio while the script finishes in the background.

Related

How can I install a package in "developer mode" in R / RStudio?

I am creating an R package, but I don't want to install and restart every time I make a change. Is there a way to install the package in developer mode, as in python, so that I can run the code that I modify?
If you aren't already I would suggest installing the devtools package. The load_all function will load your package in it's current state.
If you are building it inside RStudio, in a project, you can use CTRL + Shift + L to load the package so that you can test it interactively. This link has some basic details around setting up a package in RStudio.

devtools equivalent of RStudio Build panel buttons

I am making an R package using RStudio. I am comfortable using the buttons on the Build panel. I have a script that I would like to run each time I Build & Reload or Clean and Rebuild. I'd like to write a function that runs my script and then executes the devtools commands associated with one of those buttons, but I am having trouble finding documentation of the correspondence between those buttons and devtools commands. The buttons are as follows:
Build & Reload
Check
Load All
Clean and Rebuild
Test Package
Check Package
Build Source Package
Build Binary Package
For each of the items in that list, what devtools R code would I run to cause the exact same behavior?
In RStudio you can check "Use devtools package functions if available" in Project Options > Build Tools and you can see what devtools functions will be used. If you look at the build console pane, you can check out what RStudio runs. General cases if using devtools:
Build & Reload
devtools::build()
devtools::reload() is possibly an option but Rstudio uses R CMD INSTALL --no-multiarch --with-keep.source <pkgNameGoesHere>
Check
devtools::check()
Load All
devtools::load_all(".")
Clean and Rebuild
R CMD INSTALL --preclean --no-multiarch --with-keep.source <pkgNameGoesHere>
Test Package
devtools::test()
Check Package
devtools::check() (same as Check button)
Build Source Package
devtools::build()
Build Binary Package
devtools::build(binary = TRUE, args = c('--preclean'))
More info at the readme in the devtools repo.
To execute the Clean & Rebuilt action from RStudio inside R you can use the R function
system()
Executing
system("R CMD INSTALL
--preclean
--no-multiarch
--with-keep.source <your_package_name>")
Executes the Shell command from within your R session. Be aware that you will have to refer to the correct location of your package if you run this outside the Package Project (e.g. from another project or session)

Cannot install R packages in Jupyter Notebook

I am trying to run R code in Jupyter and the R Kernel was added. Most of the time, packages can be installed successfully. However, some of the packages, such as RCurl and ggmap, would got error while installing.
Example:
install.packages("RCurl")
Warning message in install.packages("RCurl"):
“installation of package ‘RCurl’ had non-zero exit status”Updating HTML index of packages in '.Library'
Making 'packages.html' ... done
What should I do?
Try to specify CRAN as repository in your install.packages statement when installing RCurl and ggmap. For example:
install.packages("RCurl", repos='http://cran.us.r-project.org')
This Stack Overflow post on installing R packages through Anaconda/Jupyter beyond those included in R essential provides more detail.
(Side note: I had encountered the same issue when trying to install R packages on computer clusters. This solution worked for me.)
use conda comment:
conda install r-RCurl
I kept getting the non-zero exit status when trying to install packages with Jupyter notebook with R kernel and was failing because of multiple dependencies when wanting to install a package. I am not an expert in any of these so please forgive me if I make an error in explaining or if it is a non-issue for you but please feel free to comment to clear things out. I just want to share my success story so hopefully it can help someone else: I am working on a MacBook Pro. Here are the information I get when I run R.version() on my jupyter notebook with R kernel:
$platform 'x86_64-conda_cos6-linux-gnu'
$arch 'x86_64'
$os 'linux-gnu'
$system 'x86_64, linux-gnu'
$language 'R'
$version.string 'R version 3.6.1 (2019-07-05)'
These are the steps to take to fix the issue:
Go to https://anaconda.org/
Search the package name that you are trying to install
Copy the one line that is given to install the package, it should be something like:
Conda install -c r r-caret #conda install -c r r-package_name
NOTE: sometimes during installing packages, you’re asked whether or not you want to continue, so add --y at the end of the above statement to continue, so something like this
Conda install -c r r-caret --y
(I will always add it just to be on the safe side)
Click on the new launcher (+ icon) to create a new notebook with PySpark (once opened it has .ipynp extension)
On the first cell paste the copied line from step 2 and run
Once done, restart the kernel on the current notebook
Restart the kernel on your other notebook with R kernel
Run library(package_name) on your notebook with R kernel (e.x. library(caret))
install.packages("Hmisc", .libPaths(), repos='http://cran.us.r-project.org')
This command will install the packagae in the conda
"/home/user/anaconda3/lib/R/library" and use the cran r repository as source.
Add path in Anaconda
As per this answer,
one can also add additional paths in anaconda to load libraries from (for eg., the location where R studio saves the user-installed packages) with
.libPaths( c( .libPaths(), "~/userLibrary") )
For example, the following worked for me:
In Anaconda :
.libPaths( c( .libPaths(), "C:\Users\name\Documents\R\win-library\3.5") )
When I tried to add anaconda's library path to RStudio, it resulted in errors (The procedure entry point MARK_NOT_MUTABLE could not be located in the dynamic link library << arose 4 times in succession) after installation of a package, though the package seemed to load.
Replace name with your local user folder name
Add/change path in RStudio
A useful link to make changes in default user-installed library paths :
https://www.accelebrate.com/library/how-to-articles/r-rstudio-library
To find out where a package has been installed:
find.package('package_name')
The directions nobody else supplied worked for me, but I found this guide, and it worked. Spent way too much time trying all these when I just needed a few simple commands. https://developers.refinitiv.com/en/article-catalog/article/setup-jupyter-notebook-r
I already had R and python installed, so I skipped to step 3. The only seems to mention windows, but it worked for me on mac as well. After following them I was able to install the packages using install.packages("dplyr", repos = "http://cran.us.r-project.org") in a cell in jupyter.
You have to create a directory in which your package will be and do for eg:
install.packages('ggplot2',loc='your directory')
First Step: You can install the IRkernel packages by running the following command in an R console:install.packages('IRkernel')
Second Step: You will have to make Jupyter see the newly installed R kernel by installing a kernel spec. To install system-wide, set user to False in the installspec command IRkernel::installspec(user = FALSE)
Setup Jupyter Notebook for R

Reload updated package in R

I used to develop some package for R. So, this is an iterative process where I need to check the working of the package in between by installing it via rstudio, and then again adding some necessary functionalities. So, I used the following process:
create a R package
install it via command prompt: R CMD install <{package_name}>
load the package in rstudio as, library(package_name)
Check the necessary functionality
detach package in rstudio using, detach(package:{package_name})
remove package via command prompt as: R CMD remove <{package_name}>
add/update package
repeat steps 2 - 7,until package is fully developed.
Now the problem is that everytime I close rstudio after step 5, otherwise updated package after installation is not reflected in R.
So, How can I avoid to close rstdio everytime; and always get updated copy of installed package. In other words, I don't want to close rstudio everytime. I have found that detach( ) is not effective.
Note: I use rstudio only for checking the functionality of package. I check,build for building packages on command prompt using R CMD check/build commands
RStudio has functionality for building packages that I does what I think you have described.
Basically, use 'new project' and select the R-package option or just open an old project using the '.Rproj' file
Then use the build and reload each time you make a change to the package
and want to reload the package in (see pic).
Seems to work OK for me.
See link for more details:
https://support.rstudio.com/hc/en-us/articles/200486488-Developing-Packages-with-RStudio

How to upload arules package in R

I am not able to upload arules package
Below is the following command I am using
library(arules)
I have downloaded the file from http://cran.r-project.org/web/packages/arules/index.html still it is not able to upload it.
Can anyone help me out
install.packages("arules")
pls. find below GUI - Screen for Rcmdr Package
Open RStudio.
Go to the “Packages” tab and click on “Install Packages”. The first time you’ll do this you’ll be prompted to choose a CRAN mirror. R will download all necessary files from the server you select here. Choose the location closest to you (probably “USA CA 1” or “USA CA 2”, which are housed at UC Berkeley and UCLA, respectively).
Install packages in Windows
Start typing “Rcmdr” until you see it appear in a list. Select the first option (or finish typing Rcmdr), ensure that “Install dependencies” is checked, and click “Install”.
Install Rcmdr in Windows
Wait while all the parts of the R Commander package are installed.
I had a similar problem. I solve it using
#install and load devtools to install directly from github
install("devtools")
library("devtools")
install_github("mhahsler/arules")
If you want to install "arules" package, you may not be able to install it just using the code below.
<< install.packages("arules") >>
follow the steps in order to solve problem:
go to the package's address and download it manually.you will find it in the link below:
https://cran.r-project.org/web/packages/arules/index.html
If you are using R-Studio just install that package you have got, by "tools" menu in your IDE (R-studio)
Tools->Install packages->...
brows the file in your computer and install it.
go to the command line and using library(arules) function test the package. If there is still an error it could belong to incompatibility of the package with the version of R (Not R-Studio or any other IDE that you are using), just upgrade R and try installation period again.
I had a related problem where I could not install arules after being able to successfully install many libraries. I finally learned that I needed to update to 3.4 for arules. In order to do that, I followed duckmayr's answer HERE. Then, I was able to install arules with no issues, and it worked.

Resources