首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >selectInput的值列表

selectInput的值列表
EN

Stack Overflow用户
提问于 2017-08-06 21:42:16
回答 1查看 696关注 0票数 2

我想创建一个"selectInput“小部件,它的值选择是由"fileInput”小部件导入的dataset中的列的名称。

我尝试将数据集的名称"tableOutput“,作为"selectInput”小部件的"choices“参数的参数,但它不起作用。我在小部件中得到的唯一选择是"name“、"id”和"class“。

下面是我使用的代码:

代码语言:javascript
复制
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“小部件的选项?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-06 23:17:01

像这样的东西应该能起到作用。我使用renderUI根据您的数据集创建了滑块微件

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45532598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档