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.
Please advise.
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
}
I think you meant to do
url[i] <- paste(...
instead of
url[i] = paste(...
. If so replace=
with<-
.You don't define the vector,
url
, before trying to subset it.url
is also a function in the base package, sourl[i]
is attempting to subset that function... which doesn't make sense.You probably defined
url
in your prior R session, but forgot to copy that code to your script.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
The closure mentioned in the error message is (loosely) the function and the environment that stores the variables when the function is called.
In this specific case, as Joshua mentioned, you are trying to access the
url
function as a variable. If you define a variable namedurl
, then the error goes away.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.
If you're running into this problem in
shiny
, the most likely cause is that you're trying to work with areactive
expression without calling it as a function using parentheses.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).
But if we try to subset it without parentheses, then we're actually trying to index a function, and we get an error: