selectInput

时间:2017-08-06 13:42:16

标签: user-interface input server shiny output

我想创建一个“selectInput”小部件,其值的选择是“fileInput”小部件导入的数据集中列的名称。

我尝试“tableOutput”数据集的名称,作为“selectInput”小部件的“choices”参数的参数,但它不起作用。我在小部件中获得的唯一选择是“name”,“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”小部件的选项?

1 个答案:

答案 0 :(得分: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)