【问题标题】:R Shiny - Error Subsetting dataR Shiny - 错误子集数据
【发布时间】:2018-07-28 13:09:23
【问题描述】:

我对 Shiny 很陌生,在读取 CSV 文件后,我正在尝试根据变量层对数据进行子集化。我经常收到错误“'closure'类型的对象不是子集的”。我将不胜感激任何帮助。谢谢。

代码是:

library(shiny)
library(ggplot2)
library(dplyr)
library(tidyverse)


## Only run examples in interactive R sessions
if (interactive()) {

ui <- fluidPage(

# 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 = TRUE,
              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"),



# Include a Slider for Strata
  sliderInput("strata",
              "strata:",
              min = 1,
              max = 20,
              value = c(1,20),
              step=1)

  ),  

########################## 

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

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

  )  
 )
)

###


server <- function(input, output, session) {

output$contents <- renderTable({



  req(input$file1)

  df <- read.csv(input$file1$datapath,
                 header = input$header,
                 sep = input$sep,
                 quote = input$quote, stringsAsFactors = FALSE)

df<-data.frame(df)

  filtered<-reactive({
                  df()%>% 
         filter(df$strata>=input$strata[1] &          df$strata<=input$strata[2])})



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



    })

  }
  # Run the app ----
  shinyApp(ui, server)

}

【问题讨论】:

  • 当您尝试对函数使用子集 ([]) 运算符时,通常会发生此错误。看起来这部分代码:`df()%>%` 是问题所在,您将df 函数传递给过滤器函数,而不是您的df 变量。只需将 df()%&gt;% 更改为 df %&gt;% 即可解决问题。
  • 感谢您的建议。我根据您的建议更改了代码。但我仍然遇到同样的错误。-“'closure' 类型的对象不是子集”

标签: r shiny


【解决方案1】:

不要在您的 renderTable 调用中定义您的 reactive,而是将其作为一个单独的元素。此外,正如 Martin 在 cmets 中指出的那样,不要在那里使用 df()。当您从反应函数调用一个值时,您需要这样做,这不是您在那里所做的。

下面给出了一个工作示例。希望这会有所帮助!

library(shiny)
library(ggplot2)
library(dplyr)
library(tidyverse)


## Only run examples in interactive R sessions
if (interactive()) {

  ui <- fluidPage(

    # 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 = TRUE,
                  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"),



        # Include a Slider for Strata
        sliderInput("strata",
                    "strata:",
                    min = 1,
                    max = 20,
                    value = c(1,20),
                    step=1)

      ),  

      ########################## 

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

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

      )  
    )
  )

  ###


  server <- function(input, output, session) {

    mytable <- reactive({

      req(input$file1)

      df <- read.csv(input$file1$datapath,
                     header = input$header,
                     sep = input$sep,
                     quote = input$quote, stringsAsFactors = FALSE)

      print(df)
      df<-data.frame(df)

      df<- df %>% 
        filter(df$strata>=input$strata[1] & df$strata<=input$strata[2])

      print(df)

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

    })

    output$contents <- renderTable({
      # Now do use (), since we are calling a value from a reactive.
      mytable()
    })

  }
  # Run the app ----
  shinyApp(ui, server)

}

【讨论】:

  • 非常感谢 Florian 和 Macro。此代码完美运行。衷心感谢您的帮助。
猜你喜欢
  • 2018-10-06
  • 2021-03-02
  • 1970-01-01
  • 2014-06-16
  • 1970-01-01
  • 2015-08-22
  • 2018-09-19
  • 2015-02-26
  • 2021-08-29
相关资源
最近更新 更多