【问题标题】:R shiny ERROR: object of type 'closure' is not subsettable [duplicate]R闪亮错误:“闭包”类型的对象不是子集[重复]
【发布时间】:2014-06-16 09:26:11
【问题描述】:

我正在使用以下代码,但我总是得到这个子集错误。 我在设置什么子集,我错在哪里。 这应该是我修改过的一些基本入口代码 在某些时候工作,我看不到错误。

谢谢

服务器.R

library(shiny)

# Define a server for the Shiny app
shinyServer(function(input, output) {

  # Filter data based on selections
  output$table <- renderDataTable({
    data <- read.table("my.csv", sep =',', header =TRUE)
    if (input$shortdesc != "All"){
      data <- data[data$ShortDescription == input$shortdesc,]
    }
    if (input$taken != "All"){
      data <- data[data$Taken == input$taken,]
    }
    if (input$location != "All"){
      data <- data[data$Location == input$location,]
    }
    data
  })

})

ui.R

library(shiny)
# Define the overall UI

shinyUI(
  fluidPage(
    titlePanel("My Items"),

    # Create a new Row in the UI for selectInputs
    fluidRow(
      column(4, 
             selectInput("man", 
                         "What:", 
                         c("All", 
                           unique(as.character(data$ShortDescription))))
      ),
      column(4, 
             selectInput("trans", 
                         "Where:", 
                         c("All", 
                           unique(as.character(data$Location))))
      ),
      column(4, 
             selectInput("cyl", 
                         "Who:", 
                         c("All", 
                           unique(as.character(data$Taken))))
      )        
    ),
    # Create a new row for the table.
    fluidRow(
      dataTableOutput(outputId="table")
    )    
  )  
)

更新:

为什么示例(见下文)有效,而当我将其更改为 my.csv 时它会中断? 如果“数据”是一个内置函数,那不会与下面的示例发生冲突吗? 很抱歉不明白,但这让我很困惑。

服务器.R

library(shiny)

# Load the ggplot2 package which provides
# the 'mpg' dataset.
library(ggplot2)

# Define a server for the Shiny app
shinyServer(function(input, output) {

  # Filter data based on selections
  output$table <- renderDataTable({
    data <- mpg
    if (input$man != "All"){
      data <- data[data$manufacturer == input$man,]
    }
    if (input$cyl != "All"){
      data <- data[data$cyl == input$cyl,]
    }
    if (input$trans != "All"){
      data <- data[data$trans == input$trans,]
    }
    data
  })

})

ui.R.

library(shiny)

# Load the ggplot2 package which provides
# the 'mpg' dataset.
library(ggplot2)

# Define the overall UI
shinyUI(
  fluidPage(
    titlePanel("Basic DataTable"),

    # Create a new Row in the UI for selectInputs
    fluidRow(
      column(4, 
          selectInput("man", 
                      "Manufacturer:", 
                      c("All", 
                        unique(as.character(mpg$manufacturer))))
      ),
      column(4, 
          selectInput("trans", 
                      "Transmission:", 
                      c("All", 
                        unique(as.character(mpg$trans))))
      ),
      column(4, 
          selectInput("cyl", 
                      "Cylinders:", 
                      c("All", 
                        unique(as.character(mpg$cyl))))
      )        
    ),
    # Create a new row for the table.
    fluidRow(
      dataTableOutput(outputId="table")
    )    
  )  
)

【问题讨论】:

  • 你能包含完整的错误信息吗?通常有关于行号的提示...
  • 监听127.0.0.1:4427 数据$ShortDescription 错误:“闭包”类型的对象不是子集
  • 在基础 R 中有一个名为 data 的函数。因此,如果 R 在当前环境中找不到 data 对象,它会假定您指的是该函数并告诉您它无法对其进行子集化。
  • 嗯。不知道我是否理解你。 “base R”是指“Server.R”,还是建议我不应该使用“data”作为变量名?感谢您的回复。
  • “base R”是指包base或更一般地说是所有在R中自动加载的函数和包。换句话说,data也引用了一个内置函数。

标签: r shiny


【解决方案1】:

扩展@Roland 的评论:您正在发生命名空间冲突。基础R中有一个函数data,所以如果R在当前环境中找不到对象data,则从全局环境中引用函数data。在您的特定情况下,发生这种情况是因为ui.Rserver.R 处于不同的环境中,而且各个函数体都有自己的环境。所以fluidRow(...) 中的data 没有从output$table 引用data。您需要传递参数和/或使用函数动态构建 UI。参见例如here

更新问题的更新:

data 替换为ui.R 中的mpg 可以解决问题,因为mpg 被定义为全局环境中的数据集(这是library(ggplot2) 的副作用)。所以mpg(几乎)总是可以访问的并且具有必要的属性。为了更公平的比较,请将ui.R 中的mpg 替换为data,这应该会导致老问题再次出现,因为全局环境中的data 指的是一个函数,而不是您要操作的数据框。

超级更新,为每个数据集动态定义和加载选择元素提供更通用的解决方案:

服务器代码循环遍历所选数据帧的所有列,并为每个类型不是 double 的列动态生成一个选择框。 (双精度的唯一性和相等性只是自找麻烦。)这避免了范围界定问题,因为 UI 元素是在调用加载数据的反应函数后在 server.R 中创建的。

服务器.R

library(shiny)
library(ggplot2)
# Define a server for the Shiny app
shinyServer(function(input, output) {

  get.data <- reactive({
    switch(input$dataset,
           "rock" = rock,
           "pressure" = pressure,
           "cars" = cars,
           "mpg" = mpg,
           "mtcars" = mtcars,
           "diamonds" = diamonds)
  })

  # Filter my.data based on selections
  output$table <- renderDataTable({
    my.data <- get.data()
    for(n in names(my.data)){
        # avoid too many cases ... 
        # unique() with double is just asking for trouble
        if(typeof(my.data[,n]) != "double"){ 
            val <- eval(parse(text=paste0("input$",n)))
            print(val)
            if(val != "All"){
                my.data <- eval(parse(text=paste0("subset(my.data,",n,"==",val,")")))
            }
        }
    }
    my.data
  })

  output$dyn.ui <- renderUI({
      my.data <- get.data()
      sel <- NULL
      for(n in names(my.data)){
          # avoid too many cases ... 
          # unique() with double is just asking for trouble
          if(typeof(my.data[,n]) != "double"){ 
              sel <- c(sel,
                   selectInput(n, n, choices=c("All",unique(my.data[,n])))
                   )
          }
      }
      sel
  })

})

ui.R

library(shiny)

# Define the overall UI

shinyUI(fluidPage(
    titlePanel("Displaying tables dynamically with renderUI() and eval()"),

    sidebarLayout(
        sidebarPanel(h2("Selection"),
                     selectInput("dataset", "Dataset", c("rock", "pressure", "cars","mtcars","diamonds")),
                     # Create a new Row in the UI for selectInputs
                     uiOutput("dyn.ui")

        )
        ,mainPanel(h2("Data"),
           dataTableOutput(outputId="table")       
        )
    )


))

【讨论】:

  • 我运行history(Inf),发现我输入了以下命令:Sys.getlocale() 和 Sys.setlocale(locale="C")。在这样做之前,它起作用了。这可能是它不起作用的原因吗?我该如何解决?谢谢
  • 我编辑了我的问题。如果您能解决这个问题,那就太好了。谢谢。
  • 谢谢!试图理解你的代码。显示csv是有效的。但是现在在侧面板中我只得到数字而没有文本。我有 12 列是数字和/或文本。
猜你喜欢
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 2015-06-18
  • 1970-01-01
  • 2019-12-30
  • 2018-06-23
  • 2019-03-23
  • 1970-01-01
相关资源
最近更新 更多