Shiny R drop-down lists

0

I'm trying to make an interface in Shiny of R, the idea is to select a .csv file, when reading it take the columns and put them in a drop-down list

I have tried it in several ways, at the moment it seems that it reads the file, but it does not update the drop-down list, any idea of what is happening?

library(shiny)
library(shinythemes)

ui = fluidPage(
theme = shinytheme("flatly"),
navbarPage(theme = "flatly","Analisis de Datos",
           tabPanel("Analisis Exploratorio")),
sidebarPanel(
fileInput("archivo", "Escoger archivo CSV",multiple = TRUE, accept = 
c("text/csv","text/comma-separated-values,text/plain",".csv")),
tabPanel("Histograma",
selectInput("columnas", "Seleccionar Columna", choices= NULL))))


 server <-function(input, output , session) {
 Datos <- reactive({
   inFile <- input$archivo
   req(inFile)
   # Leyendo el csv 
   df <- read.csv(input$archivo$datapath,header = TRUE,sep =',',quote = '"')
   #Nombre de las columnas
   variables <- names(df)
   updateSelectInput(session, "columnas", "Seleccionar Columna", choices = 
   variables)
   return(df)   

})
    }
   shinyApp(ui = ui, server = server)
    
asked by Rodrigo A 22.10.2017 в 02:31
source

1 answer

0

To make it work as you are building the interface you should incorporate an "output", without it it does not make sense a reactive , you could do something like this:

library(shiny)
library(shinythemes)

ui <- shinyUI(fluidPage(
    theme = shinytheme("flatly"),
    navbarPage(theme = "flatly","Analisis de Datos", tabPanel("Analisis Exploratorio")),
    sidebarLayout(
        sidebarPanel(
            fileInput("archivo", "Escoger archivo CSV",multiple = TRUE, accept = 
                          c("text/csv","text/comma-separated-values,text/plain",".csv")),
            tabPanel("Histograma",
                     selectInput("columnas", "Seleccionar Columna", choices= NULL))
        ),
        mainPanel(
            uiOutput("tb")
        )
    )
))

server <-function(input, output , session) {
    data <- reactive({
        inFile <- input$archivo
        req(inFile)
        df <- read.csv(input$archivo$datapath,header = TRUE,sep =',',quote = '"')
        #Nombre de las columnas
        variables <- names(df)
        updateSelectInput(session, "columnas", "Seleccionar Columna", choices = variables)
        return(df)          
    })

    output$table <- renderTable({
        if(is.null(data())){return ()}
        data()    
    })

    output$tb <- renderUI({
        if(is.null(data()))
            h5("Sin datos")
        else
            tabsetPanel(tabPanel("Datos", tableOutput("table")))
    })    
}

We incorporate a mainPanel where we draw the output that will not be anything other than a tableOutput of the file csv , just as an example, in your case it will surely be something else, what is essential is to have a output for you to operate the reactive of Shinny . Otherwise, it can also be implemented using observe , in the following way:

server <-function(input, output , session) {

    observe({
        inFile <- input$archivo
        req(inFile)
        df <- read.csv(input$archivo$datapath,header = TRUE,sep =',',quote = '"')
        #Nombre de las columnas
        variables <- names(df)
        updateSelectInput(session, "columnas", "Seleccionar Columna", choices = variables)
        return(df)          
    })
}
    
answered by 23.10.2017 в 01:33