SelectInput选项包括列标题

时间:2018-10-18 13:00:08

标签: r shiny r-markdown selectlist

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

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

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

[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))))
                            ))))

```

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

以下是您传递给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"
相关问题