Home > Mobile >  Access elements of dataframe from uploaded CSV file in R Shiny for SliderInput arguments
Access elements of dataframe from uploaded CSV file in R Shiny for SliderInput arguments

Time:05-11

I would like to upload a CSV file to a Shiny app and have the values that are in the CSV file to be updated for the value, min, and max, on input sliders in the UI. This seems like a simple task in Shiny but all the examples on SO that I can find are very long winded.

The test file to be uploaded is here:

write.csv(data.frame("parameter" = c("a", "b"), 
                     "value" = c(1.2, 3.4), 
                     "min" = c(1, 3), 
                     "max" = c(2, 4)),
          "input_test.csv", 
          row.names = FALSE)

A very simple Shiny app is here:

library(shiny)

ui <- fluidPage(
    fileInput("filename", "Choose CSV File", accept = ".csv"),
    tableOutput("contents"),
    sliderInput(inputId = 'num', label = "a", value = 1.5, min = 0, max = 5),
    sliderInput(inputId = 'num', label = "b", value = 3.5, min = 0, max = 5)
)

server <- function(input, output) {
    output$contents <- renderTable({
        inFile <- input$filename
        if (is.null(inFile))
            return(NULL)
        read.csv(inFile$datapath)
    })
}

shinyApp(ui, server)

How do I make the sliderInput arguments update to those values in the CSV file after it's been uploaded for the value, min, and max columns for parameters a and b?

Note: I do not want to specify the data.frame within the R code, this is just an example, it will be uploaded so has to load dynamically.

CodePudding user response:

I am not sure to understand your problem well but I think you need to use your sliderInput within the server and not within the ui.

To do so, you must use renderUI() and UIOutput() functions. You will have something like that :

In your server you will have :

UIOutput("mysliderinput")

And in your ui you will have :

output$mysliderinput <- renderUI({
    sliderInput(inputId = 'num', label = "a", value = 1.5, min = 0, max = 5)
})

Of course you need to adapte value, min and max with references to columns of your dataframe.

You can refer to this : https://shiny.rstudio.com/reference/shiny/1.6.0/renderUI.html

Hope this clue helps you.

CodePudding user response:

Try this,

library(shiny)

ui <- fluidPage(
  fileInput("filename", "Choose CSV File", accept = ".csv"),
  tableOutput("contents"),
  sliderInput(inputId = 'num_1', label = "a", value = 1.5, min = 0, max = 5),
  sliderInput(inputId = 'num_2', label = "b", value = 3.5, min = 0, max = 5)
)

server <- function(input, output, session) {
  
  inputFile <- reactiveValues()
  
  observe({
    req(input$filename)
    inFile <- input$filename
    inputFile$data <- read.csv(inFile$datapath)
    inputFile$Slide1 <- unname(unlist(inputFile$data[1, 2:4]))
    inputFile$Slide2 <- unname(unlist(inputFile$data[2, 2:4]))
  })

  output$contents <- renderTable({
    inputFile$data
  })
  
  observe({
    req(inputFile$Slide1, inputFile$Slide2)
    updateSliderInput(session, "num_1", value = inputFile$Slide1[1],
                      min = inputFile$Slide1[2], max = inputFile$Slide1[3], step = 0.1)
    updateSliderInput(session, "num_2", value = inputFile$Slide2[1],
                      min = inputFile$Slide2[2], max = inputFile$Slide2[3], step = 0.1)
  })
  
}

shinyApp(ui, server)
  • Related