Sometimes, you will take a course on DataCamp, and learn how to use a new R package. However, once you switch to your local environment and use the `install.packages()` command to get the package, you may realize that you can't install the package locally. The most plausible reason is that it is not hosted on CRAN. CRAN (Comprehensive R Archive Network) has very strict requirements, which can make listing a package a slow process. This doesn't necessarily mean that the package isn't functional, stable, or can't be used.
Let's take a concrete example. The DataCamp course Financial Trading in R uses the R quantstrat package (at the time this article was written, quantstrat was not listed on CRAN). What you need to do then is install it directly from the GitHub repo where it's hosted.
To do this, you first need to install `devtools`, which will let you use the `install_github()` command and then get the package you need by specifying the username and the repo where the package is hosted. To install quantstrat, you would execute the snippet below:
install.packages("devtools") require(devtools) install_github("braverock/blotter") # dependency install_github("braverock/quantstrat")
In general, it's good practice to check the CRAN list of available packages to see if the package you're looking for is listed or not. Just type Cmd + F on a Mac or Ctrl + F on Windows, and enter the name of the package you're looking for. If it shows 0, then it's not listed.
Then you can check the R-Forge page for the package (here is the one for quantstrat). If the status reads "Failed to build", then it means that the package does not meet the CRAN Repository Policy and is therefore not available from there.
In this case, you need to use the same process as shown above for quantstrat:
- make sure devtools is installed on your local machine (`install.packages("devtools")`)
- load devtools (`require(devtools)`)
- install the dependencies that the package you want needs (`install_github("username/dependency_repo")`)
- install the package you want (`install_github("username/package_repo")`)
Comments
4 comments
Unfortunately the method does not work.
I use a Mac and I tried to apply it both on R and on RStudio. I always get the same error message:
> require(devtools)
> install_github("braverock/blotter") # dependency
Downloading GitHub repo braverock/blotter@master
from URL https://api.github.com/repos/braverock/blotter/zipball/master
Installing blotter
'/Library/Frameworks/R.framework/Resources/bin/R' --no-site-file --no-environ --no-save \
--no-restore --quiet CMD INSTALL \
'/private/var/folders/bq/7gxzb22n42gf_l3lnqypwz9r0000gn/T/RtmpwD2CUD/devtools209fe881ee7/braverock-blotter-bc75cf5' \
--library='/Library/Frameworks/R.framework/Versions/3.3/Resources/library' --install-tests
* installing *source* package ‘blotter’ ...
** libs
Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo.
ERROR: compilation failed for package ‘blotter’
* removing ‘/Library/Frameworks/R.framework/Versions/3.3/Resources/library/blotter’
Installation failed: Command failed (1)
> install_github("braverock/quantstrat")
Downloading GitHub repo braverock/quantstrat@master
from URL https://api.github.com/repos/braverock/quantstrat/zipball/master
Installing quantstrat
'/Library/Frameworks/R.framework/Resources/bin/R' --no-site-file --no-environ --no-save \
--no-restore --quiet CMD INSTALL \
'/private/var/folders/bq/7gxzb22n42gf_l3lnqypwz9r0000gn/T/RtmpwD2CUD/devtools209f8415738/braverock-quantstrat-be01b35' \
--library='/Library/Frameworks/R.framework/Versions/3.3/Resources/library' --install-tests
ERROR: dependency ‘blotter’ is not available for package ‘quantstrat’
* removing ‘/Library/Frameworks/R.framework/Versions/3.3/Resources/library/quantstrat’
Installation failed: Command failed (1)
Hi Pietro,
I'll send you a message to make sure you see our reply, but this is not an issue with the method. It's an issue with Xcode. It looks like a new version of Xcode was installed, and the updated user agreement was not accepted.
You can run the following command to accept the user agreement:
and scroll down to the end and type "agree". Or, run the following command directly:
Thank you Hadrien,
You solved my problem.
Hello,
I want to install the package ggmap and I am facing an error? is it for the same cause listed above ?
How can I install this package ?
Thank you
Please sign in to leave a comment.