【发布时间】:2018-09-14 15:21:51
【问题描述】:
我正在构建由@Jon_Spring 提供的solution。我想根据所选的分解更改 dygraph 的一些参数。一切都流经管道,所以我开始寻找advice on conditional evaluation using pipes。
我的想法是在管道中使用{if } 方法:
{if(input$diss=="Total")
dySeries("1", label = "All") else
dySeries("man", label = "Male") %>%
dySeries("woman", label = "Female")
}
这一步是在调用dygraph() 之后进行的。我的想法是说“如果没有分解,则使用dySeries("1", label = "All"),否则使用dySeries("man", label = "Male") %>% dySeries("woman", label = "Female")。
但我得到一个错误:
$ 运算符对原子向量无效
我是在正确的轨道上,还是有更好的方法来根据输入为 dygraph 创建条件绘图参数?
---
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 <- dplyr::sample_n(dat, 80)
```
Sidebar {.sidebar}
=====================================
```{r}
radioButtons("diss", label = "Disaggregation",
choices = list("All" = "Total",
"By Sex" = "sex",
"By Language" = "lang"),
selected = "Total")
```
Page 1
=====================================
```{r plot}
renderDygraph({
grp_col <- rlang::sym(input$diss)
dat %>%
mutate(Total = 1) %>%
mutate(my_group = !!grp_col) %>%
group_by(date = lubridate::floor_date(date, "1 week"), my_group) %>%
count() %>% spread(my_group, n) %>% ungroup() %>%
padr::pad() %>% replace(is.na(.), 0) %>%
xts::xts(order.by = .$date) %>%
dygraph() %>%
dyRangeSelector() %>%
{if(input$diss=="Total")
dySeries("1", label = "All") else
dySeries("male", label = "Male") %>%
dySeries("female", label = "Female")
} %>%
dyOptions(
useDataTimezone = FALSE,
stepPlot = TRUE,
drawGrid = FALSE,
fillGraph = TRUE,
colors = ifelse(input$diss=="Total",
"blue",
c("purple", "orange"))
)
})
```
【问题讨论】: