如何在 R 中安装多个软件包?
我尝试了以下代码:
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")
此代码有效:
install.packages("ggplot2")
为什么多包线路无法工作?
初级:通过
c(...)
: 形成向量
install.packages(c("EIAdata", "gdata", "ggmap", "ggplot2")) # rest omitted
这样你就有一个第一个长度> 1的参数。
就个人而言,我更喜欢
littler中的
install.r
所以我会这样做(在 Unix 命令行):
install.r EIAdata gdata ggmap ggplot2 # rest omitted again
请注意,参数的数量没有限制。对我来说,用 4 个包来写这个比用你的例子中的 20 个包更容易。
# 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)
在这里,我将展示安装多个 R 数据科学包的示例。
您还需要注意确保您没有使用不同样式的引号,如果您使用外语,这些引号有时会在文本编辑器中创建。
$ 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"))
如果您从 CLI 安装,R 会显示
--- Please select a CRAN mirror for use in this session ---
,几秒钟后,将弹出一个 GUI,并显示全局下载镜像的列表。
如果您使用的是最新版本的 R,您可能会收到一条警告,指出某些较旧的软件包不适用于您的 R 版本,您可以选择忽略该软件包、查找较新的软件包或使用较旧版本的 R。
警告信息: 软件包‘editR’、‘rga’、‘rcdimple’、‘Prophet’、‘cloudyR’不可用(适用于 R 版本 3.4.2)
压缩的 .tgz 文件将下载到类似
/private/var/folders/2k/p756_j5x0x5fqplwrq74j1sh0000gn/T/RtmpMTzQQ5/downloaded_packages
的位置
实际包裹位于
/Users/tymac/Library/R/3.4/library
和/Library/Frameworks/R.framework/Versions/3.4/Resources/library
。
您可以通过其他几种方式查看包。
或