Programing

오류

crosscheck 2020. 8. 14. 07:13
반응형

오류 : '클로저'유형의 개체는 하위 집합이 아닙니다.


드디어 스크래핑 코드를 알아낼 수있었습니다 . 잘 작동하는 것 같았고 갑자기 다시 실행했을 때 다음과 같은 오류 메시지가 나타납니다.

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
}

일반적으로이 오류 메시지는 함수에서 인덱싱을 사용하려했음을 의미합니다. 예를 들어이 오류 메시지를 재현 할 수 있습니다.

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함수에 변수로 액세스하려고 합니다. 라는 변수를 정의 url하면 오류가 사라집니다.

좋은 연습의 문제로, 일반적으로 base-R 함수 뒤에 변수 이름을 지정하지 않아야합니다. (변수 호출 data은이 오류의 일반적인 원인입니다.)


연산자 또는 키워드의 하위 집합을 시도하는 데 몇 가지 관련 오류가 있습니다.

`+`[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괄호를 사용하여 함수로 호출하지 않고 표현식 으로 작업하려고하기 때문입니다.

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

우리는 종종 데이터 프레임 인 것처럼 반짝 거리는 반응식으로 작업하지만 실제로는 데이터 프레임 (또는 다른 객체)을 반환하는 함수 입니다.

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

그러나 괄호없이 하위 집합을 시도하면 실제로 함수를 인덱싱하려고 시도하고 오류가 발생합니다.

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

You don't define the vector, url, before trying to subset it. url is also a function in the base package, so url[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.


I had this issue was trying to remove a ui element inside an event reactive:

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

I was getting this error, but not on the removeUI element line, it was in the next observer after for some reason. Taking the removeUI method out of the eventReactive and placing it somewhere else removed this error for me.


In case of this similar error Warning: Error in $: object of type 'closure' is not subsettable [No stack trace available]

Just add corresponding package name using :: e.g.

instead of tags(....)

write shiny::tags(....)


I think you meant to do url[i] <- paste(...

instead of url[i] = paste(.... If so replace = with <-.

참고URL : https://stackoverflow.com/questions/11308367/error-in-my-code-object-of-type-closure-is-not-subsettable

반응형