Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
th1460 committed May 9, 2021
0 parents commit bce3122
Show file tree
Hide file tree
Showing 13 changed files with 597 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
.Renviron
.zip
.csv
52 changes: 52 additions & 0 deletions Dockerfile.app
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Install Java
FROM openjdk:8

# Install Ubuntu packages
RUN apt-get update && apt-get install -y \
sudo \
gdebi-core \
pandoc \
pandoc-citeproc \
libcurl4-gnutls-dev \
libcairo2-dev \
libxt-dev \
libssl-dev \
libsasl2-dev \
r-base \
r-base-dev

# Download and install Shiny Server
RUN wget --no-verbose https://s3.amazonaws.com/rstudio-shiny-server-os-build/ubuntu-12.04/x86_64/VERSION -O "version.txt" && \
VERSION=$(cat version.txt) && \
wget --no-verbose "https://s3.amazonaws.com/rstudio-shiny-server-os-build/ubuntu-12.04/x86_64/shiny-server-$VERSION-amd64.deb" -O ss-latest.deb && \
gdebi -n ss-latest.deb && \
rm -f version.txt ss-latest.deb

# Config Java
RUN sudo R CMD javareconf JAVA_HOME=$JAVA_HOME

# Install R packages that are required
RUN R -e "install.packages(c('shiny', 'bslib', 'magrittr', 'thematic', 'dbplyr', 'lubridate', 'leaflet', 'ggplot2', 'remotes'), repos='http://cran.rstudio.com/')"
RUN R -e "remotes::install_github('deepanshu88/summaryBox')"
RUN R -e "remotes::install_version('rJava', '0.9-11')"
RUN R -e "install.packages('RJDBC')"

# Copy configuration files into the Docker image
COPY shiny-server.conf /etc/shiny-server/shiny-server.conf
COPY app.R /srv/shiny-server/

# Make the ShinyApp available at port 80
EXPOSE 8080

# Copy further configuration files into the Docker image
COPY shiny-server.sh /usr/bin/shiny-server.sh

# Copy environment variables
COPY .Renviron /srv/shiny-server/

# Jar file
COPY jars /srv/shiny-server/jars

RUN ["chmod", "+x", "/usr/bin/shiny-server.sh"]

CMD ["/usr/bin/shiny-server.sh"]
7 changes: 7 additions & 0 deletions Dockerfile.update
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM openwhisk/dockerskeleton
RUN apk update && apk add openjdk8 openjdk8-jre R R-dev R-doc build-base tzdata
RUN set -ex && apk --no-cache add sudo
RUN echo "Set disable_coredump false" >> /etc/sudo.conf
RUN sudo R CMD javareconf JAVA_HOME=/usr/lib/jvm/java-1.8-openjdk/jre && \
sudo R -e "install.packages(c('jsonlite', 'rJava', 'RJDBC', 'dbplyr', 'tidyr', 'lubridate', 'curl'), repos = 'http://cran.rstudio.com/')"
COPY jars /action/jars
133 changes: 133 additions & 0 deletions app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
require(bslib)
require(shiny)
require(magrittr)
require(thematic)

readRenviron(".Renviron")

drv <-
RJDBC::JDBC("com.ibm.db2.jcc.DB2Driver", "jars/db2jcc4.jar")

db2 <-
DBI::dbConnect(drv,
Sys.getenv("DB2_HOST"),
user = Sys.getenv("DB2_USER"),
password = Sys.getenv("DB2_PASSWORD"))

# theme
theme <- bs_theme(
bg = "#0b3d91", fg = "white", primary = "#FCC780",
base_font = font_google("Space Mono"),
code_font = font_google("Space Mono")
)

thematic_on(
bg = "auto",
fg = "auto",
accent = "auto",
font = "auto"
)

# ui
ui <- fluidPage(
navbarPage("Covid 19"),
theme = theme,
sidebarLayout(
sidebarPanel(
selectizeInput("country", "País:",
dplyr::tbl(db2, "AVG_SA_COVID19") %>%
dplyr::distinct(COUNTRY) %>%
dplyr::pull()),
dateInput("data", "Data")

),

mainPanel(
uiOutput("summarybox"),
tabsetPanel(id = "panel",
tabPanel("Plot",
tags$h4("Distribuição do número médio de mortes"),
plotOutput("delta_plot")),
tabPanel("Mapa",
tags$h4("Variação % comparando semana anterior"),
leaflet::leafletOutput("mapa")))
)
)
)

# server
server <- function(input, output) {

output$delta_plot <- renderPlot({

data_avg <- dplyr::tbl(db2, "AVG_SA_COVID19") %>%
dplyr::as_tibble()

last <-
data_avg %>%
dplyr::filter(COUNTRY == input$country,
WEEK == lubridate::week(input$data),
YEAR == lubridate::year(input$data)) %>%
dplyr::select(YEAR, WEEK, AVG_VALUE)

data_avg %>%
dplyr::filter(COUNTRY == input$country) %>%
ggplot2::ggplot(ggplot2::aes(WEEK, AVG_VALUE)) +
ggplot2::geom_line() +
ggplot2::labs(x = "Semanas", y = "Número médio de mortes") +
ggplot2::geom_label(data = last, ggplot2::aes(WEEK, AVG_VALUE, label = round(AVG_VALUE, 0))) +
ggplot2::facet_grid(~YEAR)

})

delta_avg_sa <- reactive({

dplyr::tbl(db2, "AVG_SA_COVID19") %>%
dplyr::as_tibble() %>%
dplyr::filter(WEEK == lubridate::week(input$data), YEAR == lubridate::year(input$data))

})


output$mapa <- leaflet::renderLeaflet({

conpal <-
leaflet::colorNumeric(palette = "Reds",
domain = delta_avg_sa()$DELTA,
na.color = "black")

delta_avg_sa() %>%
leaflet::leaflet() %>%
leaflet::addProviderTiles("CartoDB.DarkMatter") %>%
leaflet::addCircleMarkers(~LONG, ~LAT,
label = paste(delta_avg_sa()$COUNTRY, "|",
round(delta_avg_sa()$DELTA, 1)),
color = ~conpal(delta_avg_sa()$DELTA)) %>%
leaflet::addLegend(position = "bottomleft",
title = "Delta %",
pal = conpal,
values = delta_avg_sa()$DELTA,
opacity = 0.5)

})

output$summarybox <-
renderUI({

sa <- dplyr::tbl(db2, "SA_COVID19") %>%
dplyr::as_tibble() %>%
dplyr::filter(COUNTRY == input$country) %>%
dplyr::slice(dplyr::n())


fluidRow(
summaryBox::summaryBox("Última atualização", sa$DATE, width = 3, icon = "fas fa-calendar", style = "info"),
summaryBox::summaryBox("Número de mortes no último dia", sa$VALUE, width = 3, icon = "fas fa-chart-bar", style = "warning"),
summaryBox::summaryBox("Número total de mortes", sa$CUMULATE, width = 3, icon = "fas fa-chart-bar", style = "danger")
)})

}

shinyApp(ui, server)


13 changes: 13 additions & 0 deletions covid19.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 4
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX
8 changes: 8 additions & 0 deletions exec
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

# get data
wget -q https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv -O time_series_covid19_deaths_global.csv

# run R script
chmod +x read_update_data.R # turn executable
./read_update_data.R # run script
Binary file added jars/db2jcc4.jar
Binary file not shown.
5 changes: 5 additions & 0 deletions manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
applications:
- name: dash-covid
memory: 256M
random-route: true
67 changes: 67 additions & 0 deletions read_update_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env Rscript

# config
options(warn = -1)
Sys.setenv(TZ = "America/Sao_Paulo")

readRenviron(".Renviron")

# packages
require(magrittr, include.only = "%>%", quietly = TRUE)

# read data
covid19 <-
read.csv("time_series_covid19_deaths_global.csv", stringsAsFactors = FALSE)

# prepare data
sa <-
covid19 %>%
dplyr::filter(Country.Region %in% c("Brazil", "Peru", "Bolivia",
"Chile", "Argentina", "Colombia",
"Venezuela", "Ecuador", "Uruguay",
"Paraguay")) %>%
dplyr::select(- Province.State) %>%
tidyr::pivot_longer(!Country.Region:Long,
names_to = "date",
values_to = "cumulate") %>%
dplyr::group_by(Country.Region) %>%
dplyr::mutate(date = gsub("X", "", date) %>%
gsub("\\.", "-", .) %>%
lubridate::mdy(),
value = cumulate - dplyr::lag(cumulate)) %>%
dplyr::rename(country = Country.Region)

avg_sa <-
sa %>%
dplyr::mutate(week = lubridate::week(date),
year = lubridate::year(date)) %>%
dplyr::group_by(country, year, week, Lat, Long) %>%
dplyr::summarise(avg_value = mean(value, na.rm = TRUE), .groups = "drop") %>%
dplyr::group_by(country) %>%
dplyr::mutate(delta = (avg_value - dplyr::lag(avg_value))/dplyr::lag(avg_value) * 100) %>%
dplyr::ungroup() %>%
dplyr::mutate(delta = ifelse(is.finite(delta) & !is.na(delta), delta, 0))

# write data
require(dbplyr, quietly = TRUE)

readRenviron(".Renviron")

drv <-
RJDBC::JDBC("com.ibm.db2.jcc.DB2Driver", "jars/db2jcc4.jar")

db2 <-
DBI::dbConnect(drv,
Sys.getenv("DB2_HOST"),
user = Sys.getenv("DB2_USER"),
password = Sys.getenv("DB2_PASSWORD"))

DBI::dbWriteTable(db2, "AVG_SA_COVID19", value = avg_sa, overwrite = TRUE)
DBI::dbWriteTable(db2, "SA_COVID19", value = sa, overwrite = TRUE)

# test
update <- sa %>% dplyr::pull(date) %>% range() %>% .[2]

# response
jsonlite::stream_out(data.frame(update = update), verbose = FALSE)

23 changes: 23 additions & 0 deletions shiny-server.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Define the user we should use when spawning R Shiny processes
run_as shiny;

# Define a top-level server which will listen on a port
server {
# Instruct this server to listen on port 80. The app at dokku-alt need expose PORT 80, or 500 e etc. See the docs
listen 8080;

# Define the location available at the base URL
location / {

# Run this location in 'site_dir' mode, which hosts the entire directory
# tree at '/srv/shiny-server'
site_dir /srv/shiny-server;

# Define where we should put the log files for this location
log_dir /var/log/shiny-server;

# Should we list the contents of a (non-Shiny-App) directory when the user
# visits the corresponding URL?
directory_index on;
}
}
7 changes: 7 additions & 0 deletions shiny-server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

# Make sure the directory for individual app logs exists
mkdir -p /var/log/shiny-server
chown shiny.shiny /var/log/shiny-server

exec shiny-server >> /var/log/shiny-server.log 2>&1
Loading

0 comments on commit bce3122

Please sign in to comment.