Dynamic input selector based on loaded data

Thanks in advance for your help. I understand how to manipulate dynamic inputs based on other inputs for predefined datasets. those. Download the vehicle dataset. The user selects the Radio button to say that they only want to look at the blue cars. This changes the parameters in some input selectors in the user interface.

However, if I want to allow the user to download the csv file, how can I dynamically update all the relevant widgets. that is, the user loads his data, the input selector displays all the variables in the data set for graphs and regressions.

The italic part is my problem.

ui.r

library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

  titlePanel("My R Shiny App"),

  sidebarPanel(
    fileInput('file', 'Choose file to upload.'),
    #Select Box: y
    selectInput("y_input", label = h5("Select Time Series/Response Variable"),
                choices = names(myData),
                selected = NULL)

  )
) 
)

server.r

library(shiny)

#Run once when app is launched
#Load data

shinyServer(function(input, output) {

  #Run once each time a user visits the app
  #Put code to build a distinct set of reactive objects for user


  output$Variable_Selector <- renderUI({
    if(is.null(input$file))
      return(NULL)
    inFile <- input$file
    myData <- read.csv(inFile$datapath)
    if (is.null(myData))
      return(NULL)
  })
})

global.r

myData = NULL

Thank!

+4
1

observe updateSelectInput - . , csv :

Df1 <- data.frame(
  x=1:5,
  y=2*(1:5),
  z=3*(1:5))
##
Df2 <- data.frame(
  a=6:10,
  b=2*(6:10),
  c=3*(6:10),
  d=letters[1:5],
  stringsAsFactors=F)
##
write.csv(Df1,file="~/tempfiles/Df1.csv",row.names=F)
##
write.csv(Df2,file="~/tempfiles/Df2.csv",row.names=F)

ui.R:

library(shiny)

shinyUI(fluidPage(

  titlePanel("My R Shiny App"),

  sidebarPanel(

    fileInput(
      'file', 
      'Choose file to upload.'
    ),

    selectInput(
      "y_input", 
      label = h5("Select Time Series/Response Variable"),
      ""
    )

  )

))

server.R:

library(shiny)

shinyServer(function(input, output, session) {

  inFile <- reactive({
    if (is.null(input$file)) {
      return(NULL)
    } else {
      input$file
    }
  })

  myData <- reactive({
    if (is.null(inFile())) {
      return(NULL)
    } else {
      read.csv(inFile()$datapath)
    }
  })

  observe({
    updateSelectInput(
      session,
      "y_input",
      choices=names(myData()))

  })

})

global.R:

myData <- NULL

, , :

Uploading <code> Df1.csv </code>

Uploading <code> Df2.csv </code>

+6

All Articles