2017-08-06 3 views
1

「fileInput」ウィジェットによってインポートされたデータセットの列の名前を値の選択肢とする「selectInput」ウィジェットを作成したいとします。selectInputの値のリスト

"selectInput"ウィジェットの "choices"パラメータへの引数として、データセットの名前を "tableOutput"しようとしましたが、機能しません。ウィジェットで私が得る唯一の選択肢は、「名前」、「ID」、「クラス」です。

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

これはあなたが探していたものなら答えを受け入れてください –

関連する問題