<我的代码>中的错误: 'closure' 类型的对象不是子集 [英] Error in <my code> : object of type 'closure' is not subsettable

查看:48
本文介绍了<我的代码>中的错误: 'closure' 类型的对象不是子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was finally able to work out the code for my scraping. It seemed to be working fine and then all of a sudden when I ran it again, I got the following error message:

Error in url[i] = paste("http://en.wikipedia.org/wiki/", gsub(" ", "_",  : 
  object of type 'closure' is not subsettable

我不确定为什么,因为我的代码中没有任何更改.

I am not sure why as I changed nothing in my code.

请指教.

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
}

推荐答案

一般来说,这个错误信息意味着你试图在一个函数上使用索引.您可以重现此错误消息,例如

In general this error message means that you have tried to use indexing on a function. You can reproduce this error message with, for example

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

错误信息中提到的闭包(松散地)是函数和调用函数时存储变量的环境.

The closure mentioned in the error message is (loosely) the function and the environment that stores the variables when the function is called.

在这种特定情况下,正如 Joshua 所提到的,您正在尝试访问 url 函数作为变量.如果你定义了一个名为 url 的变量,那么错误就会消失.

In this specific case, as Joshua mentioned, you are trying to access the url function as a variable. If you define a variable named url, then the error goes away.

作为一个好的做法,您通常应该避免在 base-R 函数之后命名变量.(调用变量 data 是一个常见的来源错误.)

As a matter of good practise, you should usually avoid naming variables after base-R functions. (Calling variables data is a common source of this error.)

尝试对运算符或关键字进行子集化有几个相关错误.

There are several related errors for trying to subset operators or keywords.

`+`[1]
## Error in `+`[1] : object of type 'builtin' is not subsettable
`if`[1]
## Error in `if`[1] : object of type 'special' is not subsettable

<小时>

如果您在 shiny 中遇到此问题,最可能的原因是您尝试使用 reactive 表达式而不将其作为函数调用使用括号.


If you're running into this problem in shiny, the most likely cause is that you're trying to work with a reactive expression without calling it as a function using parentheses.

library(shiny)
reactive_df <- reactive({
    data.frame(col1 = c(1,2,3),
               col2 = c(4,5,6))
})

虽然我们经常将反应式表达式视为数据帧,但它们实际上是返回数据帧(或其他对象)的函数.

While we often work with reactive expressions in shiny as if they were data frames, they are actually functions that return data frames (or other objects).

isolate({
    print(reactive_df())
    print(reactive_df()$col1)
})
  col1 col2
1    1    4
2    2    5
3    3    6
[1] 1 2 3

但是,如果我们尝试不使用括号对其进行子集化,那么我们实际上是在尝试对函数进行索引,并且会出现错误:

But if we try to subset it without parentheses, then we're actually trying to index a function, and we get an error:

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

这篇关于&lt;我的代码&gt;中的错误: 'closure' 类型的对象不是子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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