2017-08-06 46 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

如果这是您要寻找的东西,请接受答案 –