在我的柔性仪表板上闪亮的应用程序中,我使用的selectizeInput()有三个选项:“英语”、“西班牙语”和“其他”。在我的玩具数据集中,没有观察到变量lang的值"other“。因此,当只在输入栏中选择 "other“时,R返回一个计算错误:
在需要真/假的地方缺少值。
造成这种情况的原因是“第1页”部分中管道的以下一行:
filter(if(is.null(input$foo)) (new==1) else (lang %in% input$foo)) %>%
当数据集中没有使用输入值的观察时,显示空白图的正确方法是什么?
---
title: "test"
output:
flexdashboard::flex_dashboard:
theme: bootstrap
runtime: shiny
---
```{r setup, include=FALSE}图书馆(柔性仪表板)
图书馆(Tidyverse)
图书馆(Tibbletime)
图书馆(有向图)
图书馆(Magrittr)
图书馆(Xts)
```{r global, include=FALSE}生成数据
set.seed(1)
dat <- data.frame(date = seq(as.Date("2018-01-01") )
as.Date("2018-06-30"), "days"), sex = sample(c("male", "female"), 181, replace=TRUE), lang = sample(c("english", "spanish"), 181, replace=TRUE), age = sample(20:35, 181, replace=TRUE))dat <- sample_n(dat,80)
Sidebar {.sidebar}
=====================================
```{r}selectizeInput(
‘'foo',标签=空,
选项=c(“英语”、“西班牙语”、“其他”)、
multiple = TRUE
)
Page 1
=====================================
```{r}全
总数<-反应性({
dat %>%
mutate(new = 1) %>%arrange(date) %>%filter(if(is.null(input$foo)) (new==1) else (lang %in% input$foo)) %>%时间序列分析
tibbletime::as_tbl_time(index = date) %>% #转换为tibble对象
select(date, new) %>%tibbletime::collapse_by("1 week", side = "start", clean = TRUE) %>%group_by(date) %>%mutate(total = sum(new, na.rm = TRUE)) %>%distinct(date, .keep_all = TRUE) %>%ungroup() %>%# expand matrix to include weeks without datacomplete( date = seq(date[1], date[length(date)], by = "1 week"), fill = list(total = 0))})
转换为xts
totals_ <-反应性({
totals <- totals()xts(totals, order.by = totals$date)})
绘图
renderDygraph({
总数<-总计()
有向图(totals_,“总计”) %>%
dyRangeSelector() %>%dyOptions(useDataTimezone = FALSE, stepPlot = TRUE, drawGrid = FALSE, fillGraph = TRUE) })
发布于 2018-09-13 17:16:57
这样做的一种方法是在运行代码块之前使用shiny::req函数检查需求。
如果你加上:
req(dat$lang %in% input$foo)在运行totals <- reactive({表达式的其余部分之前,它将检查input$foo的值是否在dat$lang中。如果找不到,那么操作将被静默地停止。没有错误将显示,情节将保持空白。
https://stackoverflow.com/questions/52318561
复制相似问题