【问题标题】:how to add conditional `dySeries()` calls in a pipe based on shiny inputs如何根据闪亮的输入在管道中添加条件 `dySeries()` 调用
【发布时间】: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"))
    )
})
```

【问题讨论】:

    标签: r shiny dygraphs


    【解决方案1】:

    埃里克!

    我遇到了类似的问题,这个方法为我解决了:

    在 {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 并且能够以类似的方式解决它,如果你想看看的话。

    【讨论】:

      猜你喜欢
      • 2020-03-01
      • 1970-01-01
      • 2018-07-21
      • 2021-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多