Warning: Error in [: object of type 'closure' is not subsettable

library(shiny)
library(shinydashboard)
library(shiny)
#library(leaflet)
#library(leaflet.extras)

header <- dashboardHeader(
  dropdownMenu(
    type = "messages",
    messageItem(
      from = "Data",
      message = "Learn how to do your Shiny App",
      href = "https://rstudio.github.io/shinydashboard/structure.html#sidebar-menu-items-and-tabs"
    )
  )
)

sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
    menuItem("Widgets", icon = icon("th"), tabName = "widgets",
             badgeLabel = "new", badgeColor = "green"),
    fileInput("file1", "Choose CSV File",
              multiple = TRUE,
              accept = c("text/csv",
                         "text/comma-separated-values,text/plain",".csv")),
    sliderInput("bins",
                "Number of bins:",
                min = 1,
                max = 10000,
                value = 500), 
    sliderInput("opacity", "Opacity:",
                min = 0, max = 1,
                value = 0.5, step = 0.1)
  )
)
body <- dashboardBody(
  tabItems(
    tabItem(tabName = "dashboard",
            h2("Dashboard tab content")
    ),
    tabItem(tabName = "widgets",
            h2("Widgets tab content")
    )
  ),
      mainPanel(
        plotOutput("distPlot")
      ) #main panel
  ) #body

ui <- dashboardPage(header, sidebar, body)


# Define server logic required to draw a histogram
server <- function(input, output) {
  mydataset <- reactive({
    print("started mydata <- reactive")
    inputfile1 <- input$file1
    if (is.null(inputfile1)) return(NULL)
    df1 = data.frame(read.csv(inputfile1$datapath, as.is=TRUE))
    #VALIDATE DATA
    print("ended mydata <- reactive")
    return(df1)
  })
  observeEvent(input$file1, {
    output$distPlot <- renderPlot({
      # generate bins based on input$bins from ui.R
      x    <- mydataset[,2] 
      bins <- seq(min(x), max(x), length.out = input$bins + 1)
      
      # draw the histogram with the specified number of bins
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
   }) # renderPlot
}) #observeEvent
}#server    
     

# Run the application 
shinyApp(ui = ui, server = server)

There's a handy Shiny Debugging guide here,


Your error message, in a shiny app, usually means you are trying to perform an operation you'd usually perform on a data frame on a function (that is, a closure).

I suspect this error is popping up on the line with x <- mydataset[,2] , perhaps after you've loaded that gets returned NULL where mydataset is set up...


So what you mean is that I am subsetting a closure which is a function? How can i solve this problem?

How can i specify that I want an specific column for the histogram? Thank you!

Where is this demo app coming from?

The issue is in the part of your code where the file is loaded.

Here's a nice article I'd work through to get familiar with best practices for how to do this,

The req function if your friend.


One quick option is to replace your server seciton with something like

# Define server logic required to draw a histogram
server <- function(input, output) {

  observeEvent(input$file1, {
    output$distPlot <- renderPlot({
      
      req(input$file1)
      df1 <- read.csv(input$file1$datapath)
      # generate bins based on input$bins from ui.R
      x    <- df1[,2] 
      bins <- seq(min(x), max(x), length.out = input$bins + 1)
      
      # draw the histogram with the specified number of bins
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
    }) # renderPlot
  }) #observeEvent
}#server    

Note I load the dataset inside where the plot is created.

The tutorial at Redirect has a really nice, quick overview of how to get a shiny app working. It, plus the Help users upload files to your app guide should get you off to the races.


Also in the line x <- mydataset[,2], your hist will be built only off the second column/variable in the file you provide.

histograms

Here's a nice guide on making histograms with r

Also, GGPLOT2: Histograms and frequency polygons

mydataset is a reactive object, not a data.frame. You need to ask it for its value. Try this:

observeEvent(input$file1, {
    output$distPlot <- renderPlot({
      # generate bins based on input$bins from ui.R
      x    <- mydataset()[,2]