安装时发生错误:无法将类型'closure'强制转换为类型为'character'的vector [英] Error when installing: cannot coerce type 'closure' to vector of type 'character'

查看:2080
本文介绍了安装时发生错误:无法将类型'closure'强制转换为类型为'character'的vector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试通过键入以下命令安装所有与拨浪鼓相关的软件包:

Tried to install all rattle related packages by typing:

install.packages(rattle, dependencies = c("Depends","Suggests"))

得到了

Installing package into ‘C:/Users/Hooman/Documents/R/win-library/3.1’
(as ‘lib’ is unspecified)
Error in as.character(x) : 
  cannot coerce type 'closure' to vector of type 'character'

我已经在该文件夹中安装了两个单独的软件包,没有问题.

I already installed two individual packages in that folder and had no issue.

推荐答案

@BenBolker发布了一个答案作为评论,也许是因为他希望您盯着您的代码和他的代码之间的区别并获得Zen时刻.他还认为自己是位谦虚的绅士,太简单了,不能保证任何支持.因此,我感到内up的是,任何投票都是他的,而不是我的.我现在将尝试合法地赢得任何投票.

@BenBolker posted an answer as a comment, perhaps because he is hoping that you will stare at the difference between your code and his and get a Zen-moment. He also thinks, being the modest gentleman that he is, that it's too simple to warrant any upvotes. So I am feeling guilty that any upvotes are his rather than mine. I will now try to legitimately earn any upvotes.

出现错误的原因是install.packages函数需要一个字符对象,而您却给了它未加引号的表达式rattle. Ben使用单个元素字符向量"rattle",因此符合该功能的要求.与某些函数不同,install.packages不能为其第一个参数提供非标准评估".

The reason you got the error was the the install.packages function expects a character object, while you gave it the unquoted expression rattle. Ben uses a single element character vector "rattle", thus conforming to the requirement of the function. Unlike some functions install.packages is not equipped to provide "non-standard evaluation" of its first argument.

R有时是一种有趣的语言,带有一些不一致的评估约定.您可以通过几种功能提供未加引号的表达式并将其自动转换为字符.该列表包括library及其表亲require以及helpsubset$.知识渊博的用户将这些视为非标准"评估,并且他们可能会在编程中陷入困境.错误消息告诉您R试图将其认为"转换为语言对象,即闭包(通常是R和LiSP的函数术语),然后转换为字符,但未成功.您可以通过此控制台交互看到相同的错误:

R is a funny language at times with some inconsistent evaluation conventions. There are several functions where you can provide an unquoted expression and have it automatically converted to character. The list includes library, and its cousin, require, as well as help, subset, and $. These are considered "non-standard" evaluation by knowledgeable users, and they can have their pitfalls in programming. The error message tells you that R tried to convert what it "thought" would be a language object, a closure (which loosely is an R and LiSP term for function), to a character and did not succeed. You can see the same error with this console interaction:

> as.character(mean)
Error in as.character(mean) : 
  cannot coerce type 'closure' to vector of type 'character'

如果您看一下Ben提到的library函数并向下滚动到内部函数定义,您最终会找到一种机制,通过该机制库可以避免该错误:

If you look at the library function mentioned by Ben and scroll down past the internal function definitions you eventually get to the mechanism whereby library avoids that error:

if (!character.only) 
            package <- as.character(substitute(package))

这也可以避免该错误,我有意将其用作示例:

This would also avoid the error, which I intentionally used as an example:

> as.character(substitute(mean))
[1] "mean"

substitute函数正在对被视为闭包的语言元素进行处理,并且所得对象R名称"具有as.character方法.

The substitute function is doing processing on a language element taken to be a closure and the resulting object an R "name" has an as.character method.

这篇关于安装时发生错误:无法将类型'closure'强制转换为类型为'character'的vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆