1

I am using shiny to upload different data files from a certain folder and plot a histogram based on a certain column. The name of each file looks like "30092017ARB.csv" (date + ARB.csv). The code loops over all file names in the data-folder and print the name of files in a drop-down bottom. After selecting the name of file it should be uploaded and plot a histogram of the mw-column (the name of column is "mw). My GUI looks as follows:

library("shiny")
  dataset <- list.files("C:/R_myfirstT/data", pattern=".*.csv$")
# Define UI for dataset viewer app ----
ui <- fluidPage(


  # App title ----
  titlePanel("Data plot"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Selector for choosing dataset ----
      selectInput(inputId = "date",
                  label = "Choose a date:",
                  choices = dataset)

    ),

    # Main panel for displaying outputs ----
    mainPanel(
  plotOutput("plot")
     )
  )
)

and the server

# Define server  ----
dataset <- list.files("C:/R_myfirstT/data", pattern=".*.csv$")
dat.name<-paste("C:/R_myfirstT/data/",dataset,sep = "")

server <- function(input, output) {

  datasetInput <- reactive({
    switch(input$dataset,
           for (i in 1:length(dataset)){
             toString(dataset[i])=read.csv(file=dat.name[i], header=TRUE, sep=";")
           }
          )
    output$plot <- renderPlot({
      hist(dataset.mw, breaks = 40)
    })
  })

}

My problem is: I do not get any histogram! I get just the drop down bottom which is nice however, not entirely my goal! Any idea what could be the reason?

maniA
  • 1,437
  • 2
  • 21
  • 42

1 Answers1

1

Something like this works:

ui.R

library("shiny")

# Define UI for dataset viewer app ----
ui <- fluidPage(

  # App title ----
  titlePanel("Data plot"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Selector for choosing dataset ----

      selectInput(inputId = 'date',
                           label = 'Choose a date:',
                           choices = list.files(path = "./data",
                                                full.names = FALSE,
                                                recursive = FALSE))
    ),

    # Main panel for displaying outputs ----
    mainPanel(
      plotOutput("plot")
    )
  )
)

server.R

# Define server  ----
server <- function(input, output) {

    dataset <- reactive({
      infile <- input$date
      if (is.null(infile)){
        return(NULL)
      }
      read.csv(paste0('./data/',infile))
    })

    output$plot <- renderPlot({
      x <- dataset()[,1]
      hist(x, breaks = 40)
    })

}

enter image description here

hpesoj626
  • 3,529
  • 1
  • 17
  • 25
  • There is problem: I read all csv-files and then load with 'read.csv' in a loop depending on the choice of drop-down bottom. In your solution I get always the same data. – maniA Mar 30 '18 at 10:23
  • thanks I solved it. your solution is correct ma problem was: https://stackoverflow.com/questions/40623749/what-is-object-of-type-closure-is-not-subsettable-error-in-shiny – maniA Mar 30 '18 at 11:24