3
votes

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

1
This is a great example of why not to use function names as object names - one reason is that it can lead to confusing error messages. dt is a function in base R (see ?dt), and when you call dt[, colm] without the data frame dt existing in the scope, R will try to subset the function dt. So see if you can work out why your data frame dt is not within the scope of your hist call. - jbaums
@jbaums Thanks for ur rpl.. I have changed my server.r file Now when I run this app it gives me an error that X must b numeric - Neil
are the values passed to the hist() function numeric? - Keniajin
@ Keniajin No.. actually i have directly picked up variables from drop box.So, it contains other type of variables as well. - Neil

1 Answers

0
votes

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)