SelectInput 选项包括列 header

SelectInput choices include column header

我在 .rmd 文件中使用 shiny 并从 .csv 文件填充 selectInput 选择列表,从子集中获取唯一值。

我的问题是下拉列表包含我不想要的列 header(在我下面的示例中为 = 'area')。

我看过下面的链接并尝试了很多选项(下面显示了 4 个示例),但我不知道如何从下拉列表中排除列 header。

[https://github.com/rstudio/shiny/issues/1864]

[https://github.com/rstudio/shiny/issues/326]

[https://shiny.rstudio.com/reference/shiny/latest/selectInput.html]

这是我正在使用的数据和代码的精简版。 (在我读取 .csv 文件的完整版本中,我在 read.csv 语句中包含了 'header=TRUE' 但这并没有什么不同。)

---
output: html_document
resource_files:

runtime: shiny
---

```{r global_options, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)


library(shiny)
library(dplyr)
library(data.table)
library(flexdashboard)
library(shinydashboard)

```

```{r data}

si_data <- structure(list(area = c("England", "England", "England", "North UHB", 
"North UHB", "North UHB", "South West UHB", "South West UHB", 
"South West UHB", "South UHB", "South UHB", "South UHB", "Hampshire", 
"Hampshire", "Hampshire", "South West UHB", "South West UHB", 
"South West UHB", "West UHB", "West UHB", "West UHB", "North West UHB", 
"North West UHB", "North West UHB", "North East UHB", "North East UHB", 
"North East UHB"), a_type = c("Country", "Country", "Country", 
"HB", "HB", "HB", "HB", "HB", "HB", "HB", "HB", "HB", "County", 
"County", "County", "HB", "HB", "HB", "HB", "HB", "HB", "HB", 
"HB", "HB", "HB", "HB", "HB"), order = c(0L, 0L, 0L, 1L, 1L, 
1L, 5L, 5L, 5L, 4L, 4L, 4L, 10L, 10L, 10L, 5L, 5L, 5L, 6L, 6L, 
6L, 2L, 2L, 2L, 3L, 3L, 3L)), .Names = c("area", "a_type", "order"
), row.names = c(NA, 27L), class = "data.frame")


```

```{r select input}

  fluidRow(
    column(width=8,
      inputPanel(width=400,
      selectInput("hb", label = "Select HB", choices = (unique(subset(si_data[order(si_data$order),], a_type=="HB",select=area))))
                            )))

  fluidRow(
    column(width=8,
      inputPanel(width='400px',
      selectInput("hb", label = "Select HB - naming the optgroup", choices = ('input_list'=unique(subset(si_data[order(si_data$order),], a_type=="HB",select=area))))
      )))

  fluidRow(
    column(width=8,
      inputPanel(width='400px',
      selectInput("hb", label = "Select HB - naming the optgroup, no quotes", choices = (input_list=unique(subset(si_data[order(si_data$order),], a_type=="HB",select=area))))
      )))

    fluidRow(
    column(width=8,
      inputPanel(width='400px',
      selectInput("hb", label = "Select HB - naming optgroup wrapped in a list", choices = (list("input_list"=unique(subset(si_data[order(si_data$order),], a_type=="HB",select=area))))
                            ))))

```

如果有任何建议,我将不胜感激,谢谢!

我将按以下方式更改您的过滤,因此我直接从子集列表中获取值:

unique(subset(si_data[order(si_data$order),], a_type=="HB",select=area))[[1]]

以下是您传递给 selectInput 的选项的输出。您正在传递一个数据框列(这是一个列表),这就是您在选择中看到列名的原因。这实际上是一项设计功能,以防开发人员希望在他们的选择中创建部分。

(unique(subset(si_data[order(si_data$order),], a_type=="HB",select=area)))

             area
4       North UHB
22 North West UHB
25 North East UHB
10      South UHB
7  South West UHB
19       West UHB

如果您不需要这个,那么只需确保将向量传递给选择。我已经简化了生成选择的代码 -

unique(si_data$area[order(si_data$order)][si_data$a_type == "HB"])

[1] "North UHB" "North West UHB" "North East UHB" "South West UHB" "West UHB" "Hampshire"