简体   繁体   中英

How to dynamically create histogram upon variable selection from drop down box in Shiny

I want to create Shiny app where user will upload a csv file, upon uploading drop down box will populate with all the variables in dataset.

When user selects one of the variables from drop down box , corresponding histogram should be plotted in main panel.

Here is what I have done

ui.r File

library(shiny)

shinyUI(pageWithSidebar(
headerPanel( "Demand Forecast", "Flowserve"),
sidebarPanel(
fileInput('file1', 'Select csv file',accept=c('text/csv')
),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator', c(Comma=',',Tab='\t',  Semicolon=';' )
),
tags$hr(),
selectInput("product", "Select: ","")
 ),
mainPanel(tableOutput('contents'),

       plotOutput("hist")
      )
       ))

server.r File

    library(shiny)
    library(ggplot2)


    shinyServer(function(input,output,session){



    dataUpload<-reactive({

    inFile<-input$file1
    print(inFile)
    if(is.null(inFile))
      return(NULL)
    dt_frame = read.csv(inFile$datapath, header=input$header, sep=input$sep)
    updateSelectInput(session, "product", choices = names(dt_frame))
    })


    output$hist <- renderPlot({

    dataset<- dataUpload()
    hist(dataset[,dataset$product])

    })
  }) 

But when I run this app it gives me an error object of type 'closure' is not subsettable

Please Help..

Thanks in advance

Welcome to the world of R shiny!

  • You want to use selected column for histogram -> hist(dataset[,input$product])
  • Your reactive function doesn't return anything but you have written dataset<- dataUpload() -> add return() to the reactive function
  • You also need error check in renderPlot()

Minimal working example:

library(shiny)
library(ggplot2)

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

  dataUpload<-reactive({

    inFile<-input$file1
    print(inFile)
    if(is.null(inFile))
      return(NULL)
    dt_frame = read.csv(inFile$datapath, header=input$header, sep=input$sep)
    updateSelectInput(session, "product", choices = names(dt_frame))
    return(dt_frame)
    })

    #output$contents <- renderTable({
    #   dataset<- dataUpload()
    #   dataset
    #})
    output$hist <- renderPlot({
    # we need error check also here
    if(is.null(input$file1))
      return(NULL)
    dataset<- dataUpload()
    hist(dataset[,input$product])

    })


}

ui <- pageWithSidebar(
headerPanel( "Demand Forecast", "Flowserve"),
sidebarPanel(
fileInput('file1', 'Select csv file',accept=c('text/csv')
),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator', c(Comma=',',Tab='\t',  Semicolon=';' )
),
tags$hr(),
selectInput("product", "Select: ","")
 ),
mainPanel(tableOutput('contents'),

       plotOutput("hist")
      )
)


shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM