Skip to content

Instantly share code, notes, and snippets.

@cimentadaj
Created March 11, 2020 16:21
Show Gist options
  • Save cimentadaj/fdc3b1ba24196cdf514c42cb5608ac18 to your computer and use it in GitHub Desktop.
Save cimentadaj/fdc3b1ba24196cdf514c42cb5608ac18 to your computer and use it in GitHub Desktop.
---
title: "Corona Survey Tracker"
output: flexdashboard::flex_dashboard
runtime: shiny
---
Column {.sidebar}
-----------------------------------------------------------------------
```{r}
selectInput("country", label = "Select country:",
choices = c("Italy", "United States", "United Kingdom"),
selected = "Italy")
```
```{r setup, include=FALSE}
# Load library
library(flexdashboard)
library(limer)
library(kableExtra)
library(hrbrthemes)
library(dplyr)
library(tidyr)
library(shiny)
library(plotly)
# Setup API details
options(lime_api = 'https://survey3.gwdg.de/index.php?r=admin/remotecontrol')
options(lime_username = 'cimentada')
options(lime_password = 'BQ4J4VWpNdtQ')
# Do stuff with LimeSurvey API
get_session_key() # Log in
autoInvalidate <- reactiveTimer(60000)
tst <- reactive({
autoInvalidate()
upd_time <<- paste("Data updated at", Sys.time())
print(paste("Data updated at", upd_time))
print(input$country)
cnty_code <- switch(
input$country,
"Italy" = 684964,
"United States" = 928449,
"United Kingdom" = 558948
)
tst <- get_responses(cnty_code)
tst$email_yes <- ifelse(tst$EMAIL == "", 0, 1)
# Excludes email column
tst <- tst[!names(tst) %in% "EMAIL"]
tst$birth_groups <- cut(tst$BIRTH, breaks = c(0, 18, seq(20, 100, by = 10)))
tst <- tst[, c("AREA", "birth_groups", "SEX", "email_yes")]
tst <-
tst %>%
count(AREA, birth_groups, SEX, email_yes) %>%
complete(AREA, birth_groups, SEX, email_yes, fill = list(n = 0)) %>%
mutate(SEX = ifelse(SEX == "Prefer not to answer", "NA", SEX),
birth_groups = gsub("\\(|\\]", "", birth_groups),
birth_groups = gsub(",", "-", birth_groups),
AREA = ifelse(AREA == "", "NA", AREA),
age_sex = paste0(birth_groups, "-", SEX),
perc = round(n / sum(n), 3),)
tst
})
```
Column {data-width=650}
-----------------------------------------------------------------------
### Stratification tracker
```{r}
renderPlotly({
plt <-
tst() %>%
group_by(AREA, age_sex) %>%
summarize(n = sum(n)) %>%
ggplot(aes(x = age_sex, y = AREA, fill = n)) +
geom_tile() +
scale_x_discrete(guide = guide_axis(n.dodge = 2)) +
scale_fill_viridis_c() +
hrbrthemes::theme_ipsum() +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
plt %>%
ggplotly()
})
```
Column {data-width=270}
-----------------------------------------------------------------------
### Update time
```{r}
renderText({
autoInvalidate()
upd_time
})
```
### Total completed surveys
```{r}
renderPlotly({
plt2 <-
tst() %>%
summarize(`Complete surveys` = sum(n),
`Complete surveys w/ email` = sum(n[email_yes == 1])) %>%
pivot_longer(cols = 1:2) %>%
ggplot(aes(name, value)) +
geom_col() +
hrbrthemes::theme_ipsum()
plt2 %>%
ggplotly()
})
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment