1

列出选项selectInput()是通过对值进行硬编码来完成的,如以下示例所示?selectInput

selectInput(inputID = "variable", label ="variable:",
                choices = c("Cylinders" = "cyl",
                  "Transmission" = "am",
                  "Gears" = "gear"))

但是,我希望我的列表是choices来自用户上传的文件 (csv) 的用户选择列的唯一值列表。我该怎么做?这是据我所知:

用户界面

    shinyUI(fluidPage(
          fluidRow(
                   fileInput('datafile', 'Choose CSV file',
                accept=c('text/csv', 'text/comma-separated-values,text/plain')),
                   uiOutput("selectcol10"),
                   uiOutput("pic")
    ))
)

服务器

    shinyServer(function(input, output) {

  filedata <- reactive({
    infile <- input$datafile
    if (is.null(infile)) {
      # User has not uploaded a file yet
      return(NULL)
    }
    temp<-read.csv(infile$datapath)
    #return
    temp[order(temp[, 1]),]
  })

  output$selectcol10 <- renderUI({
    df <-filedata()
    if (is.null(df)) return(NULL)

    items=names(df)
    names(items)=items
    selectInput("selectcol10", "Primary C",items) 
  })

  col10 <- reactive({
    (unique(filedata()$selectcol10))
  })

  output$pic <- renderUI({
    selectInput("pic", "Primary C values",col10())
  })
})
4

2 回答 2

10

对于动态 UI,基本上有两种方法可以选择:updateXXXrenderUI. 这是采用这种updateXXX方法的解决方案。

library(shiny)

ui <- fluidPage(sidebarLayout(
  sidebarPanel(
    selectInput("dataset", "choose a dataset", c("mtcars", "iris")),
    selectInput("column", "select column", "placeholder1"),
    selectInput("level", "select level", "placeholder2")
  ),
  mainPanel(tableOutput("table"))
))

server <- function(input, output, session){
  dataset <- reactive({
    get(input$dataset)
  })

  observe({
    updateSelectInput(session, "column", choices = names(dataset())) 
  })

  observeEvent(input$column, {
    column_levels <- as.character(sort(unique(
      dataset()[[input$column]]
    )))
    updateSelectInput(session, "level", choices = column_levels)
  })

  output$table <- renderTable({
    subset(dataset(), dataset()[[input$column]] == input$level)
  })
}

shinyApp(ui, server)

server函数中,有一行

updateSelectInput(session, "level", choices = column_levels)

它更新了choices第三个下拉菜单的参数level。要计算级别,您可以使用该base::levels函数,但这不适用于 numerc columns。因此我用

as.character(sort(unique( . )))

反而。“占位符”将在启动后立即被替换。

应用截图

使用 fileInput 创建的数据框

下面的代码显示了如何将此逻辑与fileInput. 只要没有选择文件,我就在其中添加了一个隐藏下拉conitionalPanel菜单。ui这里

library(shiny)

# test data
write.csv(reshape2::tips, "tips.csv", row.names = FALSE)
write.csv(mtcars, "mtcars.csv")

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput('datafile', 'Choose CSV file',
                accept = c('text/csv', 'text/comma-separated-values,text/plain', '.csv')),
      conditionalPanel(
        # use a server side condition
        condition = "output.fileUploaded",
        # placeholders will be replaced from the server
        selectInput("pic", "Primay C", "placeholder 1"),
        selectInput("level", "select level", "placeholder 2")
      )
    ),
    mainPanel(
      h3("filtered data"),
      tableOutput("table")
    )
  )
)

server <- function(input, output, session){
  # create reactive version of the dataset (a data.frame object)
  filedata <- reactive({
    infile <- input$datafile
    if (is.null(infile))
      # User has not uploaded a file yet. Use NULL to prevent observeEvent from triggering
      return(NULL)
    temp <- read.csv(infile$datapath)
    temp[order(temp[, 1]),]
  })

  # inform conditionalPanel wheter dropdowns sohould be hidden
  output$fileUploaded <- reactive({
    return(!is.null(filedata()))
  })
  outputOptions(output, 'fileUploaded', suspendWhenHidden=FALSE)

  # update the selectInput elements according to the dataset

  ## update 'column' selector
  observeEvent(filedata(), {
    updateSelectInput(session, "pic", choices = names(filedata()))
  })

  ## update 'level' selector
  observeEvent(input$pic, {
    column_levels <- unique(filedata()[[input$pic]])
    updateSelectInput(session, "level", choices = column_levels,
                      label = paste("Choose level in", input$pic))
  }, ignoreInit = TRUE)

  # show table
  output$table <- renderTable({
    subset(filedata(), filedata()[[input$pic]] == input$level)
  }, bordered = TRUE)
}

shinyApp(ui, server)
于 2017-11-12T12:32:19.190 回答
0

这就是有效的方法,感谢@GregordeCillia 将我引向它:

在我替换的服务器中

col10 <- reactive({
    (unique(filedata()$selectcol10))
  })

  output$pic <- renderUI({
    selectInput("pic", "Primary C values",col10())
  })

  observe({
    valu <- unique(unlist(filedata()[input$selectcol10]))
    updateSelectInput(session, "level", choices = valu)
  })

在我替换的 UI 中

uiOutput("pic")

selectInput("level", "select level", "placeholder")),
于 2017-11-12T22:26:40.040 回答