2017-08-06 58 views
1

我想創建一個「selectInput」小部件,其值的選擇是由「fileInput」小部件導入的數據集中列的名稱。selectInput的值列表

我試着「tableOutput」數據集的名稱,作爲「selectInput」小部件的「選擇」參數的參數,但它不起作用。我在小部件中獲得的唯一選擇是「名稱」,「id」和「class」。

這是我使用的代碼:

library(shiny) 

ui <- fluidPage(

    # Widget for loading data set 
    fileInput("file", label = h4("Input csv data set")), 

    # Widget for selecting the variable among names of columns of the data set 
    selectInput("select.variable", label = h4("Select variable from data set"), 
    choices = tableOutput("list.var"), selected = 1) # This approach doesn't work 

) 

server <- function(input, output) { 

    # The goal was to get the list of names of columns to use it as "choices" 
    # in the "selectInput" widget 
    output$list.var <- renderTable({ 

    inFile <- input$file 
    if (is.null(inFile)) # To avoid error messages when the file is not yet loaded 
    return(NULL) 

    # After the file is loaded 
    data.fr <- read.csv(inFile$datapath) 
    list.var <- names(data.fr[1,]) # Get the names of the columns of the dataset 

    }) 

    } 

shinyApp(ui = ui, server = server) 

有沒有辦法使用的進口數據集作爲選擇的列的名稱爲「selectInput」窗口小部件的方法嗎?

回答

0

像這樣的東西應該做的伎倆。我以前renderUI從數據集中產生滑動部件

library(shiny) 

ui <- fluidPage(

     # Widget for loading data set 
     fileInput("file", label = h4("Input csv data set")), 
     uiOutput("myslider") 
) 
server <- function(input, output) { 

     # The goal was to get the list of names of columns to use it as "choices" 
     # in the "selectInput" widget 

     output$myslider <- renderUI({ 
       # Widget for selecting the variable among names of columns of the data set 
       selectInput("select.variable", label = h4("Select variable from data set"), 
          choices = names(mydata()), selected = 1) # This approach doesn't work 

     }) 

     mydata <- reactive({ 
       inFile <- input$file 
       if (is.null(inFile)) # To avoid error messages when the file is not yet loaded 
         return(NULL) 

       # After the file is loaded 
       data.fr <- read.csv(inFile$datapath) 
       names(data.fr[1,]) # Get the names of the columns of the dataset 
     }) 

     output$list.var <- renderTable({ 
       mydata() 
     }) 

} 

shinyApp(ui = ui, server = server) 
+0

謝謝你這麼多,它的工作原理!現在,我將嘗試更好地瞭解如何... :) – Loyushka

+0

如果這是您要尋找的東西,請接受答案 –

相關問題