83

How would I got about installing multiple packages in R?

I tried the following code:

install.packages("EIAdata", "gdata", "ggmap", "ggplot2","gridExtra","ISOweek","kobe","lubridate","maps","MASS","memisc","pander","plyr","psych","Quandl","quantmod","reshape2","rgeos","Rgnuplot","RODBC","scales","sp","sqldf","stockPortfolio","stringi","stringr","XLConnect", "xlsReadWrite","zipcode")

This code works:

install.packages("ggplot2")

Why won't the line with the multiple packages work?

Scarabee
  • 5,437
  • 5
  • 29
  • 55
user2946746
  • 1,740
  • 3
  • 21
  • 36

3 Answers3

133

Elementary: form a vector via c(...):

 install.packages(c("EIAdata", "gdata", "ggmap", "ggplot2")) # rest omitted

so that you have one first argument of length > 1.

Personally, I prefer install.r from littler so I'd do (at the Unix command-line):

  install.r EIAdata gdata ggmap ggplot2    # rest omitted again

Note that there is no limit to the number of arguments. It was just easier for me to write this with four packages than the 20-some from your example.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Worked like a charm...takes maybe milsec for littler to install and everything else is smooth ! – Prathamesh dhanawade Jul 15 '19 at 14:21
  • 3
    :-) And better still, with the current version on `options("Ncpus"=4)` (or another value) it uses the number of chosen cpus (or, rather, cores) to install in parallel. – Dirk Eddelbuettel Jul 15 '19 at 14:29
  • Ooo thats gr8 ! Thanks Dirk ! I wonder by any means littler "checks" and "skips" unchanged package versions for faster builds ?! – Prathamesh dhanawade Jul 15 '19 at 14:46
  • 2
    If you _list them explicitly_ it will install them. If you don't want a package, don't list it. Dependencies are automagically pulled in. Also, `install2.r` is more featureful; we could add a 'skip if present in same version' but I don't consider this to be urgent. – Dirk Eddelbuettel Jul 15 '19 at 14:48
  • Alright. I am trying to automate the installation with "requirements.txt". Is there any way that install.r directly takes a file name (maybe an option like -f ...i dindnt see in examples !) and does the magic ?! – Prathamesh dhanawade Jul 15 '19 at 15:47
13
# Here we have a list of packages we want to install

load.lib<-c("EIAdata", "gdata", "ggmap","ggplot2","gridExtra","ISOweek",
"Kobe","lubridate","maps","MASS","memisc","pander","plyr","psych",
"Quandl","quantmod","reshape2","rgeos","Rgnuplot","RODBC","scales",
"sp","sqldf","stockPortfolio","stringi","stringr","XLConnect", 
"xlsReadWrite","zipcode")

# Then we select only the packages that aren't currently installed.

install.lib <- load.lib[!load.lib %in% installed.packages()

# And finally we install the missing packages, including their dependency.
for(lib in install.lib) install.packages(lib,dependencies=TRUE)
# After the installation process completes, we load all packages.
sapply(load.lib,require,character=TRUE)
Community
  • 1
  • 1
Gaurav
  • 1,329
  • 1
  • 8
  • 4
8

Here is a sweet suite of data science packages

You will also need to pay attention to make sure you're not using different styled quotation marks that are sometimes created in text editors if you’re using a foreign language.

$ R

> install.packages(c("remotes","readxl","googlesheets","haven", "readr", "rio", "Hmisc", "sqldf", "jsonlite", "XML", "httr", "quantmod", "tidyquant", "rvest", "dplyr", "purrr", "reshape2", "tidyr", "magrittr", "validate", "testthat", "data.table", "stringr", "lubridate", "zoo", "editR", "knitr", "officer", "listviewer", "DT", "ggplot2", "ggiraph", "dygraphs", "googleVis", "metricsgraphics", "RColorBrewer", "sf", "leaflet", "ggmap", "tmap", "tmaptools", "mapsapi", "tidycensus", "glue", "rga", "RSiteCatalyst", "roxygen2", "shiny", "flexdashboard", "openxlsx", "gmodels", "janitor", "car", "rcdimple", "foreach", "scales", "plotly", "highcharter", "profvis", "tidytext", "diffobj", "Prophet", "feather", "fst", "googleAuthR", "cloudyR"))

If you're installing from CLI R will say --- Please select a CRAN mirror for use in this session --- and after a couple of seconds a GUI will pop up and show a list of global download mirrors.

If you're using the latest version of R you might get a warning that certain older packages aren't available for your R version which you can choose to ignore, find newer packages or use an older version of R.

Warning message: packages ‘editR’, ‘rga’, ‘rcdimple’, ‘Prophet’, ‘cloudyR’ are not available (for R version 3.4.2)

The compressed .tgz files will be downloaded somewhere like /private/var/folders/2k/p756_j5x0x5fqplwrq74j1sh0000gn/T/RtmpMTzQQ5/downloaded_packages

Actual packages located in /Users/tymac/Library/R/3.4/library and /Library/Frameworks/R.framework/Versions/3.4/Resources/library.

You can view packages a couple of other ways.

  • Open R app/console
  • --> Help --> Html help
  • Reference --> Packages

or

  • Open RStudio
  • --> Help --> R Help
  • help area
  • --> Reference --> Packages
Edison
  • 11,881
  • 5
  • 42
  • 50
  • 1
    officeR can not be installed because it does not exist, it should be `officer` – David Gohel Oct 26 '17 at 09:18
  • This answer starts off with a long list of packages that has nothing to do with the OP's question. It also does not seem to answer the question at all. Third strike is blaming the appearance of non-standard quotation marks on "foreign" languages, it has little to do with that language used. Besides that, for most users on this site, it is English that is foreign. – KeithWM Apr 13 '21 at 09:16