Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
中出错:“closure”类型的对象不可子集_R_R Faq - Fatal编程技术网

中出错:“closure”类型的对象不可子集

中出错:“closure”类型的对象不可子集,r,r-faq,R,R Faq,我终于能够算出的代码。它似乎工作正常,然后当我再次运行它时,突然收到以下错误消息: Error in url[i] = paste("http://en.wikipedia.org/wiki/", gsub(" ", "_", : object of type 'closure' is not subsettable 我不知道为什么,因为我没有在代码中更改任何内容 请告知 library(XML) library(plyr) names <- c("George Clooney

我终于能够算出的代码。它似乎工作正常,然后当我再次运行它时,突然收到以下错误消息:

Error in url[i] = paste("http://en.wikipedia.org/wiki/", gsub(" ", "_",  : 
  object of type 'closure' is not subsettable
我不知道为什么,因为我没有在代码中更改任何内容

请告知

library(XML)
library(plyr)

names <- c("George Clooney", "Kevin Costner", "George Bush", "Amar Shanghavi")

for(i in 1:length(names)) {
    url[i] = paste('http://en.wikipedia.org/wiki/', gsub(" ","_", names[i]) , sep="")

    # some parsing code
}

在尝试将其子集之前,不需要定义向量url。url也是基本包中的一个函数,因此url[i]正在尝试将该函数子集。。。这没有道理


您可能在以前的R会话中定义了url,但忘记将该代码复制到脚本中。

通常,此错误消息表示您试图对函数使用索引。您可以复制此错误消息,例如

mean[1]
## Error in mean[1] : object of type 'closure' is not subsettable
mean[[1]]
## Error in mean[[1]] : object of type 'closure' is not subsettable
mean$a
## Error in mean$a : object of type 'closure' is not subsettable
错误消息中提到的闭包是在调用函数时存储变量的函数和环境

在这个特定的例子中,正如Joshua所提到的,您试图将函数作为变量访问。如果您定义了一个名为url的变量,那么错误就会消失

作为一个好的实践,您通常应该避免在base-R函数之后命名变量。调用变量是此错误的常见原因

尝试子集运算符或关键字时会出现几个相关错误

`+`[1]
## Error in `+`[1] : object of type 'builtin' is not subsettable
`if`[1]
## Error in `if`[1] : object of type 'special' is not subsettable
如果您在Shining中遇到此问题,最可能的原因是您试图使用反应式表达式,而不是使用括号将其作为函数调用

library(shiny)
reactive_df <- reactive({
    data.frame(col1 = c(1,2,3),
               col2 = c(4,5,6))
})
但如果我们试图在没有括号的情况下对其进行子集,那么我们实际上是在尝试索引一个函数,我们会得到一个错误:

isolate(
    reactive_df$col1
)
Error in reactive_df$col1 : object of type 'closure' is not subsettable

我想你是想做url[I]我遇到了这个问题,当时我试图删除事件中的ui元素:

myReactives <- eventReactive(input$execute, {
    ... # Some other long running function here
    removeUI(selector = "#placeholder2")
})

我得到了这个错误,但不是在removeUI元素行上,因为某种原因,它出现在后面的观察者中。将removeUI方法从eventReactive中取出,并将其放置在其他位置,可以为我删除此错误。

如果出现类似错误 警告:类型为“closure”的$:对象中的错误不可子集 [没有可用的堆栈跟踪]

只需使用以下命令添加相应的包名: e、 g

而不是标签


闪亮::标记….

它可能意味着一个未定义的变量。

在我的例子中,当您错误地键入[]而不是!
myReactives <- eventReactive(input$execute, {
    ... # Some other long running function here
    removeUI(selector = "#placeholder2")
})