我正在使用@Jon_Spring提供的solution。我想根据选定的分类更改书形图的某些参数。一切都流经管道,所以我开始寻找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")
。
但出现错误:
$运算符对原子向量无效
我是在正确的轨道上吗,还是有更好的方法根据输入来为图表创建条件绘图参数?
---
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"))
)
})
```
埃里克!
我有一个类似的问题,这个方法为我解决了这个问题:
在{if}内,必须精确地指定管道中要通过dySeries的元素的位置,您可以通过在dySeries函数中放置一个点(。)来做到这一点。
代替您使用的,您可以尝试以下操作:
{if(input$diss=="Total")
dySeries(., "1", label = "All") else
dySeries(., "man", label = "Male")
}%>%
{if(input$diss=="Total")
.
else
dySeries(., "woman", label = "Female")
}
我将{if}分为两个条件评估,不知道它是否会起作用,但值得尝试。使用圆点时,不应出现问题中提到的错误。我有一个question about conditional evaluation by using pipes,并且可以通过类似的方式解决它,如果您想看看的话。