gpt4 book ai didi

r - SelectInput 选项包括列标题

转载 作者:行者123 更新时间:2023-12-02 04:27:28 25 4
gpt4 key购买 nike

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

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

我看过下面的链接并尝试了很多选项(下面显示了 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))))
))))

```

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

最佳答案

下面是您传递给 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"

关于r - SelectInput 选项包括列标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52874649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com