简体   繁体   English

为了加载或安装程序包而无法进行循环。 我究竟做错了什么?

[英]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中的packagesrequiredpackages对象引用,以解决逻辑错误。 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()来确认ggplot2ggthemes包都已加载到内存中:

> 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.

相关问题 我想给我的馅饼上色,但#numbers 不起作用。 我究竟做错了什么? - I would like to color my pie but the #numbers doesn't work. What am I doing wrong? Ubuntu 12.04 R install.packages()不起作用。 没有警告,没有安装 - Ubuntu 12.04 R install.packages() does not work. No warning, no install 我在这个情节里做错了什么? - What am I doing wrong in this plot? regsubsets我在做什么错? - What am I doing wrong with regsubsets? 我在这个gsub示例中做错了什么? - What am I doing wrong in this gsub example? 我正在尝试在 R 中创建一个带有 while 循环的 Collat​​z 序列。我在这个 while 循环中做错了什么? - I am trying to create a Collatz sequence with while loop in R. What am i doing wrong in this while loop here? R:我在这个带有 if-else 语句的 for 循环中做错了什么? - R: What am I doing wrong in this for-loop with an if-else statement? 有时 lubridate 中的 %within% function 不会返回预期结果 - 我做错了什么? - Sometimes the %within% function in lubridate does not return the expected results - what am I doing wrong? 使用 spatstat 进行点模式分类:我做错了什么? - Point pattern classification with spatstat: what am I doing wrong? dygraph的showZeroValues选项在做什么? - What am I doing wrong with dygraph's showZeroValues option?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM