2

I am building on a solution provided by @Jon_Spring. I want to change some parameters of the dygraph based on the selected disaggregation. Everything flows through a pipe, so I started to look for advice on conditional evaluation using pipes.

My thought was to use the {if } approach within the pipe:

{if(input$diss=="Total") 
      dySeries("1", label = "All") else 
      dySeries("man", label = "Male") %>% 
      dySeries("woman", label = "Female")
    }

This step comes after dygraph() is called. My idea was to say "if there is no disaggregation, use dySeries("1", label = "All"), otherwise use dySeries("man", label = "Male") %>% dySeries("woman", label = "Female").

But I get an error:

$ operator is invalid for atomic vectors

Am I on the right track, or is there a better way to create conditional plot parameters for dygraph based on inputs?

---
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"))
    )
})
```
Eric Green
  • 7,385
  • 11
  • 56
  • 102

1 Answers1

1

Eric!

I had a similar problem and this aproach solved it for me:

Inside the {if} you have to specify axactly where the element that comes through the pipe is going to fit in the dySeries, you do that by placing a dot (.) in the dySeries function.

Instead of the if you used, you could try the following:

{if(input$diss=="Total") 
  dySeries(., "1", label = "All") else 
  dySeries(., "man", label = "Male") 
}%>% 
{if(input$diss=="Total")
  .
else
  dySeries(., "woman", label = "Female")
}

I divided the {if} into two conditional evaluations, I don't know if it's going to work, but it's worth the try. You shouldn't get the error mentioned in the question when you use the dot. I had a question about conditional evaluation by using pipes and was able to solve it in a similar way, if you want to take a look.