[英]Loop in order to load or install packages does not work. What am I doing wrong?
I'm learning R and have written my first for-loop. 我正在学习R,并且已经写了我的第一个for循环。 For a character vector with the required packages, requiredpackages , I try to create a function named install_load that checks whether the packages within requiredpackages are already installed.
对于具有必需软件包, requiredpackages的字符向量,我尝试创建一个名为install_load的函数,该函数检查是否已安装必需软件包中的软件包。 If so, then it loads those packages via the library() function.
如果是这样,那么它将通过library()函数加载那些软件包。 Else, it installs them.
否则,它将安装它们。 However, running the code gives me the following error:
但是,运行代码会给我以下错误:
Error in install.packages : Updating loaded packages
install.packages中的错误:更新已加载的软件包
Restarting R session...
正在重新启动R会话...
install.packages(p) Error in install.packages : object 'p' not found**
install.packages(p)install.packages中的错误:找不到对象'p'**
requiredpackages <- c('ggplot2', 'ggthemes')
install_load <- function(packages){
for (p in packages) {
if (p %in% rownames(installed.packages())) {
library(p, character.only=TRUE)
} else {
install.packages(p)
}
}
}
install_load(requiredpackages)
Change the requiredpackages
object reference to packages
within the function to resolve the logic error. 更改对
packages
中的packages
的requiredpackages
对象引用,以解决逻辑错误。 Since packages
is the name of the argument that contains the list of packages being passed to the function, it is the correct object to reference within the for()
loop of the function. 由于
packages
是包含要传递给函数的软件包列表的参数的名称,因此它是在函数的for()
循环中引用的正确对象。
You'll also want to add a library()
function to the branch that installs a package so at the end of the function all required packages are both installed and loaded. 您还需要向安装软件包的分支中添加一个
library()
函数,以便在函数末尾同时安装和加载所有必需的软件包。
requiredpackages <- c('ggplot2', 'ggthemes')
install_load <- function(packages){
for (p in packages) {
if (p %in% rownames(installed.packages())) {
library(p, character.only=TRUE)
} else {
install.packages(p)
library(p,character.only = TRUE)
}
}
}
install_load(requiredpackages)
...and the output: ...以及输出:
> install_load(requiredpackages)
Want to understand how all the pieces fit together? See the R for Data Science book:
http://r4ds.had.co.nz/
trying URL 'https://cran.rstudio.com/bin/macosx/el-capitan/contrib/3.5/ggthemes_4.0.1.tgz'
Content type 'application/x-gzip' length 425053 bytes (415 KB)
==================================================
downloaded 415 KB
The downloaded binary packages are in
/var/folders/2b/bhqlk7xs4712_5b0shwgtg840000gn/T//Rtmp1yfVvz/downloaded_packages
>
We can also use sessionInfo()
to confirm that both ggplot2
and ggthemes
packages have been loaded into memory: 我们还可以使用
sessionInfo()
来确认ggplot2
和ggthemes
包都已加载到内存中:
> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS 10.14.2
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggthemes_4.0.1 ggplot2_3.1.0
...sessionInfo() output continues.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.