R functions come in packages like tools come in different toolboxes. You will use the function install.packages("PackageName") to get the needed package and library(PackageName) to load it in your R environment. After the package is installed, add a comment to the installation code like this
# install.packages("PackageName")
so your script doesn't reinstall the package each time but keeps it in
library(PackageName)
because it is needed each time you start a new R session. It is a good idea to keep all library() statements at the top of your script so you can easily see all packages needed and do not duplicate the library calls.
R Packages
Install packages from (almost) anywhere
devtools
R package makes it easy to install packages from locations other than the CRAN website. devtools provides functions like install_github
, install_gitorious
, install_bitbucket
, and install_url
. These work similar to install.packages
, but they search new locations for R packages. install_github
is especially useful because many R developers provide development
versions of their packages on GitHub. The development version of a
package will contain a sneak peek of new functions and patches but may
not be as stable or as bug free as the CRAN version.What's the best way to learn about R packages?
It is difficult to use an R package if you don't know that it exists. You could go to the CRAN website and click the Packages link to see a list of available packages, but you'll have to wade through thousands of them. Moreover, many R packages do the same things.
How do you know which package does them best? The R-packages mailing list is a place to start. It sends out announcements of new packages and maintains an archive of old announcements. Blogs that aggregate posts about R can also provide valuable leads. I recommend R-bloggers. RStudio maintains a list of some of the most useful R packages in the Getting Started section of http://support.rstudio.com. Finally, CRAN groups together some of the most useful - and most respected - packages by subject area. This is an excellent place to learn about the packages designed for your area of work.Source: G. Grolemund, https://rstudio-education.github.io/hopr/packages2.html This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 License.