错误:“关闭”类型的对象不可子集化

时间:2019-06-27 06:45:16

标签: shiny

运行以下特定代码段时出现以下错误。对此的任何帮助都非常感谢。

"ERROR:object of type 'closure' is not subsettable"

试图寻求帮助,但未能解决我的问题。

library(shiny)

ui <- fluidPage(tabsetPanel(
  type = "pills",
  tabPanel(
    "Data Upload",
    # App title ----
    titlePanel("Uploading Files"),

    # Sidebar layout with input and output definitions ----
    sidebarLayout(

      # Sidebar panel for inputs ----
      sidebarPanel(

        # Input: Select a file ----
        fileInput("file1", "Choose CSV File",
          multiple = FALSE,
          accept = c(
            "text/csv",
            "text/comma-separated-values,text/plain",
            ".csv"
          )
        ),

        # Horizontal line ----
        tags$hr(),

        # Input: Checkbox if file has header ----
        checkboxInput("header", "Header", TRUE),

        # Input: Select separator ----
        radioButtons("sep", "Separator",
          choices = c(
            Comma = ",",
            Semicolon = ";",
            Tab = "\t"
          ),
          selected = ","
        ),

        # Input: Select quotes ----
        radioButtons("quote", "Quote",
          choices = c(
            None = "",
            "Double Quote" = '"',
            "Single Quote" = "'"
          ),
          selected = '"'
        ),

        # Horizontal line ----
        tags$hr(),

        # Input: Select number of rows to display ----
        radioButtons("disp", "Display",
          choices = c(
            Head = "head",
            All = "all"
          ),
          selected = "head"
        )
      ),

      # Main panel for displaying outputs ----
      mainPanel(

        # Output: Data file ----
        tableOutput("contents")
      )
    ), hr(),

    print("~~~~~~~~~~~~~~~~~~~~footer~~~~~~~~~~~~~~~~~~~~")
  ),
  tabPanel("Variable Information"), tabPanel("Create new Variables"), tabPanel("Correlations")
))

server <- shinyServer(function(input, output) {
  reactive({
    req(input$file1)
  })

  tryCatch({
    df <- reactive({
      read.csv(input$file1$datapath,
        header = input$header,
        sep = input$sep,
        quote = input$quote
      )
    })
  },
  error = reactive({
    function(e) {
      # return a safeError if a parsing error occurs
      stop(safeError(e))
    }
  })
  )


  output$contents <- renderTable({
    req(input$file1)
    # when reading semicolon separated files,
    # having a comma separator causes `read.csv` to error
    df()


    if (input$disp == "head") {
      return(head(df))
    }
    else {
      return(df)
    }
  })
})

shinyApp(ui, server)

我需要创建一个用于下载数据的对象,并在多个选项卡中使用同一对象。在执行此操作时,我收到错误消息“'closure'类型的对象不可子集“

1 个答案:

答案 0 :(得分:0)

head(df())中,output$contents的括号缺失了

  output$contents <- renderTable({
    req(input$file1)
    # when reading semicolon separated files,
    # having a comma separator causes `read.csv` to error
    # df()
    if (input$disp == "head") {
      # return(head(df()))
      head(df())
    }else {
      # return(df)
      df()
    }
  })