Jrgsua83 Jrgsua83 - 26 days ago 7
R Question

Problems with selectInput in Shiny

I´m trying to show in a selectInput the names of the variables that a file has. The idea is that the user upload a file in the app and he can select any variable in the selectInput. I´m trying to do this but I don´t realize my mistake. I found this answer but it didn´t helped me. Selecting record date with selectInput in shiny R

This is the Ui.R code: I don´t get the names in my selectInput.

library(shiny)
ui<-fluidPage(
titlePanel(title = "Prueba"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Cargar archivo',
accept = c(
'text/csv',
'text/comma-separated-values',
'text/tab-separated-values',
'text/plain',
'.csv',
'.tsv'
)
),
checkboxInput('header', '¿Contiene Encabezado?', TRUE),
radioButtons('sep', 'Delimitador',
c(Comma=',',
"Punto y coma"=';',
Tab='\t'),
','),
radioButtons('quote', 'Quote',
c(Ninguna='',
'Dobles'='"',
'Simples'="'"),
'"'),
radioButtons('resume', 'Summary',
c('Individual',
'Múltiple'),
inline = TRUE),
conditionalPanel("input.resume === 'Individual'",
selectInput('xcol', 'Variable X', names("fileInput$file1"),
selected = names("fileInput$file1")[[1]])
)
),
mainPanel(h3("Muestra del archivo cargado:"),
tableOutput('contents'),
verbatimTextOutput("summary")
)
)
)

Answer

This is an example on how to do:

library(shiny)
ui<-fluidPage(
  titlePanel(title = "Prueba"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Cargar archivo',
                accept = c(
                  'text/csv',
                  'text/comma-separated-values',
                  'text/tab-separated-values',
                  'text/plain',
                  '.csv',
                  '.tsv'
                )
      ),selectInput('xcol', 'Variable X', columns())),
    mainPanel(h3("Muestra del archivo cargado:"),
              tableOutput('contents'),
              verbatimTextOutput("summary")
    )
  )
)
server <- function(input, output, session){
  columns <- reactive({
    df <- read.table(input$file1$datapath)
    names(df)
  })
  observe({
    if(!is.null(input$file1))
      updateSelectInput(session, "xcol", choices=columns())})
}

shinyApp(ui, server)
Comments