Read data file and use names in selectInput.

1,168 views
Skip to first unread message

Daniel Loera

unread,
May 27, 2014, 12:38:34 PM5/27/14
to shiny-...@googlegroups.com
Hi, I'm new to Shiny and I have a problem, hope you can help me, the problem is that I want to read data from a file and use the names of the variables in a selectInput and can not find the way.

Ok I know how to use the datasetInput1 (), but it would be something.

#ui.R
conditionalPanel(
        'input.Etap01 === "Date"',
        fileInput('file1', ' File codes ( dbf)',
                  accept=c('.dbf')),
        tags$hr(),
conditionalPanel(
        'input.Etap01 === "variable"',
helpText('Choose variables to use'),
selectInput('xcol', 'X Variable', names(datasetInput1())) 
)

#server.R
shinyServer(function(input, output) {
    datasetInput1 <- reactive({
    read.dbf(input$file1$datapath)
  })

And as I put the names of the variables in selecInput ()?

She tells me not find the datasetInput1 () function but data should be. K

Herman Sontrop

unread,
May 27, 2014, 2:04:27 PM5/27/14
to shiny-...@googlegroups.com



Hi.

here is a simple example that shows how to select a dataset and subsequently how to populate an input selector with the column names from the selected dataset. The key is to use renderUI to construct the input selector, which is not in the ui.r script, but lives in the server.r script. To see more examples on how specific elements fit together please see the Shiny gallery.

###
### ui.r
###

library(shiny)

shinyUI(fluidPage(
 
    fluidRow(
      br(),
      h1("simple dynamic selector example")
    ),
    fluidRow(
      column(2, 
        selectInput("dataset", "Choose a dataset:", choices = c("rock", "pressure", "cars")),
        uiOutput("ColumnSelector")        
      ),
      column(2,
        verbatimTextOutput("summary")       
      )
    )
  )
)

###
### server.r
###

library(shiny)
library(datasets)

shinyServer(function(input, output) {
 
  datasetInput <- reactive({
    switch(input$dataset,
           "rock" = rock,
           "pressure" = pressure,
           "cars" = cars)
  })
 
  output$ColumnSelector <- renderUI({
    selectInput("SelectedColumn","select a column", choices = colnames(datasetInput()))
  })
 
  output$summary <- renderPrint({
    dataset <- datasetInput()
    summary(dataset[input$SelectedColumn])
  })
})
   

Daniel Loera

unread,
May 27, 2014, 3:31:16 PM5/27/14
to shiny-...@googlegroups.com
Excellent, the renderUI was the key, Thanks, J
Reply all
Reply to author
Forward
0 new messages