闪亮灵活的仪表板 - 努力将三个小部件添加到时间序列 ggplot

Shiny and flex dashboard - struggling to add three widgets to a timeseries ggplot

我的问题延续了我之前的问题 seen here。 我一直在尝试在 shiny 上构建一个仪表板,但我决定将它与 flex 仪表板结合起来,因为它更容易,而且 shiny 一直很难。

我已经使用国家/地区选择小部件创建了一个交互式时间序列图,但我无法将日期和用户类型选择添加为小部件。

到目前为止我的代码:

---
title: "eServices users"
output: 
  flexdashboard::flex_dashboard: 
    orientation: row
    vertical_layout: fill
    country: ["Angola","Kenya", "South Africa"]
    source_code: embed 
    runtime: shiny 
---

```{r setup, include=FALSE, results= 'hide'}
knitr::opts_chunk$set(echo = FALSE)

library(flexdashboard)
library(rmarkdown)
library(shiny)
library(shinythemes)
library(ggplot2)
library(tidyverse)
library(readxl)
library(knitr) 

```

```{r echo=FALSE, results= 'hide'}
#Reading data

library(readr)
df <- read_delim("df.csv", 
    delim = ";", escape_double = FALSE, trim_ws = TRUE)
View(df)
```

```{r echo=FALSE, results= 'hide'}
#Cleaning data

df$date <- as.Date(df$date, format = "%Y-%m-%d")


```

## Column {.sidebar}

```{r}
#creating a sidebar where the user can choose which country they want to view 
selectInput("Country", 
label = strong("Country"), 
choices = c("All","Angola","Kenya", "South Africa"),
selected = "Angola")


## Column {.sidebar key =" value, one per line"}
#selecting dates - this isn't linked to the graphs yet
dateRangeInput("date", strong("Date range"), start = "2020-06-01", end = "2021-11-01", min = "2020-06-01", max = "2021-11-01")

#user type sidebar - isn't connected to the dashboard yet 
 checkboxGroupInput(inputId = "selected_user_type",
                  label = "Select user tpe",
            choices =c ("Active_Users","New_users","Returning_User"),
                      selected = "New_users")
```

## Column {data-width="1000"}

### Trend of eServices Users

```{r echo=FALSE}
shiny::renderPlot ({
df %>% 
    filter(str_detect(Country, if_else(input$Country == "All","",input$Country))) %>% 
    ggplot (aes( x=date, y=Count,color = Users)) +
  geom_line(size = 1) +
  scale_x_date(breaks = function(x) seq.Date(from = min(x), 
                                             to = max(x), 
                                             by = "3 month")) + 
  scale_y_continuous(breaks = scales::pretty_breaks(n = 10)) + 
  scale_colour_manual(values = c("Active_Users" = "#ffae49", "New_users" = "#44a5c2", "Returning_Users" = "#024b7a")) + 
  labs(x = "Time (date)", y = "Users") + 
  theme_classic()
})
```

我希望用户能够过滤所有添加的小部件;国家、日期和用户类型。

也许这就是您要找的。基本上我只添加了用户类型和日期范围的过滤器:

顺便说一句:如果您提供一些示例数据,运行 您的代码会更容易。 (;

---
title: "eServices users"
output: 
  flexdashboard::flex_dashboard: 
    orientation: row
    vertical_layout: fill
    country: ["Angola", "Kenya", "South Africa"]
    source_code: embed 
    runtime: shiny 
---

```{r setup, include=FALSE, results= 'hide'}
knitr::opts_chunk$set(echo = FALSE)

library(flexdashboard)
library(rmarkdown)
library(shiny)
library(shinythemes)
library(ggplot2)
library(tidyverse)
library(readxl)
library(knitr)
```

```{r echo=FALSE, results= 'hide'}
# Reading data
# library(readr)
# df <- read_delim("df.csv",
#   delim = ";", escape_double = FALSE, trim_ws = TRUE
# )
# View(df)
dates <- seq.Date(as.Date("2020-06-01"), as.Date("2021-11-01"), 40)
n <- length(dates)
df <- data.frame(
   date = rep(dates, 3),
   Country = rep(c("Angola", "Kenya", "South Africa"), each = n),
   Users = sample(c("Active_Users", "New_users", "Returning_User"), 3 * n, replace = TRUE),
   Count = sample(seq(20), 3 * n, replace = TRUE)
)
```

## Column {.sidebar}

```{r}
# creating a sidebar where the user can choose which country they want to view
selectInput("Country",
  label = strong("Country"),
  choices = c("All", "Angola", "Kenya", "South Africa"),
  selected = "Angola"
)

## Column {.sidebar key =" value, one per line"}

dateRangeInput("date", strong("Date range"), start = "2020-06-01", end = "2021-11-01", min = "2020-06-01", max = "2021-11-01")

checkboxGroupInput(
  inputId = "selected_user_type",
  label = "Select user tpe",
  choices = c("Active_Users", "New_users", "Returning_User"),
  selected = "New_users"
)
```

## Column {data-width="1000"}

### Trend of eServices Users

```{r echo=FALSE}
renderPlot({
  if (input$Country == "All") {
    countries <- unique(df$Country)[!df$Country %in% "All"]  
  } else {
    countries <- input$Country
  }
  
  df %>%
    filter(Country %in% countries, 
           Users %in% input$selected_user_type,
           date >= as.Date(input$date[1]),
           date <= as.Date(input$date[2])) %>%
    ggplot(aes(x = date, y = Count, color = Users)) +
    geom_line(size = 1) +
    scale_x_date(breaks = function(x) {
      seq.Date(
        from = min(x),
        to = max(x),
        by = "3 month"
      )
    }) +
    scale_y_continuous(breaks = scales::pretty_breaks(n = 10)) +
    scale_colour_manual(values = c("Active_Users" = "#ffae49", "New_users" = "#44a5c2", "Returning_Users" = "#024b7a")) +
    labs(x = "Time (date)", y = "Users") +
    theme_classic()
})
```