R 当数据集中没有符合输入要求的观测值时,在selectizeInput上过滤并显示空白图

R 当数据集中没有符合输入要求的观测值时,在selectizeInput上过滤并显示空白图,r,shiny,R,Shiny,在我的flexdashboard应用程序中,我正在使用带有三个选项的selectizeInput(),“英语”、“西班牙语”和“其他”。在我的玩具数据集中,没有观察到值为“other”的变量lang。因此,当在输入栏中仅选择“其他”时,R返回评估错误: 缺少需要TRUE/FALSE的值 这是由“第1页”一节中管道的以下线条引起的: filter(如果为.null(输入$foo))(new==1)else(lang%在%input$foo中))%%>% 当数据集中没有获取输入值的观测值时,显示空白

在我的flexdashboard应用程序中,我正在使用带有三个选项的
selectizeInput()
,“英语”、“西班牙语”和“其他”。在我的玩具数据集中,没有观察到值为“other”的变量
lang
。因此,当在输入栏中仅选择“其他”时,R返回评估错误:

缺少需要TRUE/FALSE的值

这是由“第1页”一节中管道的以下线条引起的:

filter(如果为.null(输入$foo))(new==1)else(lang%在%input$foo中))%%>%

当数据集中没有获取输入值的观测值时,显示空白图的正确方法是什么

---
title: "test"
output: 
  flexdashboard::flex_dashboard:
    theme: bootstrap
runtime: shiny
---

```{r setup, include=FALSE}
  library(flexdashboard)
  library(tidyverse)
  library(tibbletime)
  library(dygraphs)
  library(magrittr)
  library(xts)
```

```{r global, include=FALSE}
# generate data
  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', label = NULL, 
  choices = c("english", "spanish", "other"),
  multiple = TRUE
)
```

Page 1
=====================================

```{r}
# all
  totals <- reactive({
  dat %>%
    mutate(new = 1) %>%
    arrange(date) %>%
    filter(if(is.null(input$foo)) (new==1) else (lang %in% input$foo)) %>%
  # time series analysis
  tibbletime::as_tbl_time(index = date) %>% # convert to tibble time object
    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 data
    complete(
      date = seq(date[1], date[length(date)], by = "1 week"),
      fill = list(total = 0)
    )
  })

# convert to xts
  totals_ <- reactive({
    totals <- totals()
    xts(totals, order.by = totals$date)
  })

# plot
  renderDygraph({

  totals_ <- totals_()
  dygraph(totals_[, "total"]) %>%
    dyRangeSelector() %>%
    dyOptions(useDataTimezone = FALSE,
              stepPlot = TRUE,
              drawGrid = FALSE,
              fillGraph = TRUE) 
  })
```
---
标题:“测试”
输出:
flexdashboard::flex_仪表板:
主题:引导
运行时间:闪亮
---
```{r设置,include=FALSE}
库(flexdashboard)
图书馆(tidyverse)
图书馆(藏书时代)
图书馆(动态图)
图书馆(magrittr)
图书馆(xts)
```
```{r全局,include=FALSE}
#生成数据
种子(1)
dat%
筛选器(如果(is.null(输入$foo))(new==1)其他(lang%在%input$foo中))%>%
#时间序列分析
tibbletime::as_tbl_time(index=date)%>%#转换为tibble time对象
选择(日期,新)%>%
tibbletime::崩溃时间(“1周”,side=“开始”,clean=TRUE)%>%
分组单位(日期)%>%
突变(总计=总和(新,na.rm=真))%>%
不同(日期,.keep_all=TRUE)%>%
解组()%>%
#扩展矩阵以包括没有数据的周数
完整的(
日期=seq(日期[1],日期[长度(日期)],by=“1周”),
填充=列表(总计=0)
)
})
#转换为xts

totals\up>执行此操作的一种方法是在运行代码块之前使用
shinny::req
功能检查需求

如果您添加:

req(dat$lang %in% input$foo)
到你的
总数的顶部非常酷。添加
req(dat$lang%在%input$foo中)
默认情况下在未选择任何内容时给了我一个空白绘图,因此我添加了
req((dat$lang%在%input$foo中)| is.null(input$foo))
来绘制未过滤的数据集。谢谢