在闪亮的应用程序中选择多个输入时,为什么错误取决于选择输入的顺序?
When selecting multiple inputs in a shiny app, why does the error depend on the order that the inputs were selected?
我有一个带有 runtime: shiny
的 flexdashboard 文档(我在此处发布了该应用程序 https://arie.shinyapps.io/reproducible_example/ 并嵌入了代码,但我也想将代码放在下面以防该应用程序在 shinyapps.io):
超出了其分配的使用量
---
title: "Example"
runtime: shiny
output:
flexdashboard::flex_dashboard:
source_code: embed
---
给定以下示例数据集:
```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
df <- tibble(name = c("peter", "paul", "mary"),
value = c(1:3))
```
我希望能够从以下用户界面生成多个 select离子:
Column {data-width=250, .sidebar}
-----------------------------------------------------------------------
```{r}
# creates interface
selectInput("name_input", label = "Name", choices = df$name,
selected = NULL, multiple = TRUE, selectize = TRUE)
```
并对 select 离子有一个 ggplot "react"。所以我做了一个反应数据集:
```{r}
# reactive data
df_reactive <- reactive(df[df$name == input$name_input,])
```
并创建以下图:
Column {data-width=750}
-----------------------------------------------------------------------
### Chart B
```{r}
renderPlot(
ggplot(df_reactive(), aes(x = input$name_input, y = value) ) +
geom_col()
)
```
现在,当我 Run Document
和 select 首先 peter
,然后 paul
,然后 mary
,情节的反应完全符合预期:它每次添加名称时都会添加一个栏。例如,当我首先 select paul
然后 peter
时会出现问题,这会引发错误 Aesthetics must be either length 1 or the same as the data (2): x, y
。
在静态图表的上下文中,这个错误对我来说很有意义,但我对为什么 selecting 名称的顺序很重要以及如何解决它感到困惑。
问题在:
df_reactive <- reactive(df[df$name == input$name_input,])
如果 length(input$name_input)
小于 3,您将尝试比较两个不同长度的数组。 R 会抛出一个错误,它也不是您真正想要执行的测试。
如我所见,您想测试 df$name
中的每个元素是否包含在 input$name_input
中。幸运的是,在 R 中有一个快捷方式,因此您不必使用 for 循环或 sapply()
,...
就像我在评论中写的那样:df_reactive <- reactive(df[df$name %in% input$name_input,])
也可以。
有关符号的更多详细信息,我会参考现有答案,因为答案会变得更加重复。
==
和 %in%
的区别解释如下:
difference between `%in%` VS `==`
我有一个带有 runtime: shiny
的 flexdashboard 文档(我在此处发布了该应用程序 https://arie.shinyapps.io/reproducible_example/ 并嵌入了代码,但我也想将代码放在下面以防该应用程序在 shinyapps.io):
---
title: "Example"
runtime: shiny
output:
flexdashboard::flex_dashboard:
source_code: embed
---
给定以下示例数据集:
```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
df <- tibble(name = c("peter", "paul", "mary"),
value = c(1:3))
```
我希望能够从以下用户界面生成多个 select离子:
Column {data-width=250, .sidebar}
-----------------------------------------------------------------------
```{r}
# creates interface
selectInput("name_input", label = "Name", choices = df$name,
selected = NULL, multiple = TRUE, selectize = TRUE)
```
并对 select 离子有一个 ggplot "react"。所以我做了一个反应数据集:
```{r}
# reactive data
df_reactive <- reactive(df[df$name == input$name_input,])
```
并创建以下图:
Column {data-width=750}
-----------------------------------------------------------------------
### Chart B
```{r}
renderPlot(
ggplot(df_reactive(), aes(x = input$name_input, y = value) ) +
geom_col()
)
```
现在,当我 Run Document
和 select 首先 peter
,然后 paul
,然后 mary
,情节的反应完全符合预期:它每次添加名称时都会添加一个栏。例如,当我首先 select paul
然后 peter
时会出现问题,这会引发错误 Aesthetics must be either length 1 or the same as the data (2): x, y
。
在静态图表的上下文中,这个错误对我来说很有意义,但我对为什么 selecting 名称的顺序很重要以及如何解决它感到困惑。
问题在:
df_reactive <- reactive(df[df$name == input$name_input,])
如果 length(input$name_input)
小于 3,您将尝试比较两个不同长度的数组。 R 会抛出一个错误,它也不是您真正想要执行的测试。
如我所见,您想测试 df$name
中的每个元素是否包含在 input$name_input
中。幸运的是,在 R 中有一个快捷方式,因此您不必使用 for 循环或 sapply()
,...
就像我在评论中写的那样:df_reactive <- reactive(df[df$name %in% input$name_input,])
也可以。
有关符号的更多详细信息,我会参考现有答案,因为答案会变得更加重复。
==
和 %in%
的区别解释如下:
difference between `%in%` VS `==`