Saurabh Datta
Saurabh Datta

Reputation: 33

R Shiny - Error Subsetting data

I am very new to shiny and I am trying to subset a data based on a variable strata after having read the file in CSV. I am constantly getting the error "object of type 'closure' is not subsettable". I would appreciate any help. Thank you.

The code is:

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)

}

Upvotes: 2

Views: 73

Answers (1)

Florian
Florian

Reputation: 25375

Don't define your reactive inside your renderTable call, but make that a separate element. Also, as Martin points out in the comments, do not use df() there. You need to do that when you call a value from a reactive function, which is not what you are doing there.

A working example is given below. Hope this helps!

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)

}

Upvotes: 1

Related Questions