Skip to content

Commit 5815b26

Browse files
authored
Merge pull request #6 from calejero/master
Selector + log/linear
2 parents df5cecb + c49432d commit 5815b26

File tree

3 files changed

+65
-32
lines changed

3 files changed

+65
-32
lines changed

model/generate_data.R

+39-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
GenerateCountryEvolutionData <- function(cvirus_longer) {
22
res <- cvirus_longer %>%
3-
# filter(provincia_estado!= "Diamond Princess") %>%
3+
# filter(provincia_estado!= 'Diamond Princess') %>%
44
group_by(pais, fecha) %>%
55
arrange(fecha) %>%
66
arrange(-casos) %>%
@@ -15,6 +15,43 @@ GenerateCountryEvolutionData <- function(cvirus_longer) {
1515
lag(casos_nuevos,1),
1616
casos_nuevos),
1717
dia_since_5 = row_number())
18-
18+
1919
return(res)
2020
}
21+
22+
GenerateSummaryCountriesPlot <- function(data.df, limit.c, scale.c, chart.c = 1) {
23+
24+
if (limit.c != 0) {
25+
max.c <- max(data.df$fecha)
26+
aux.df <- data.df[data.df$fecha == max.c, ]
27+
aux.df <- aux.df[order(aux.df$casos, decreasing = TRUE), ]
28+
aux.df <- aux.df[1:limit.c, ]
29+
data.df <- data.df[data.df$pais %in% aux.df$pais, ]
30+
}
31+
32+
if (chart.c == 1) {
33+
fig <- plot_ly(data.df, x = ~dia_since_5, y = ~casos, name = ~pais, type = 'scatter', mode = 'lines', color = ~pais)
34+
} else {
35+
fig <- plot_ly(data.df, x = ~dia_since_5, y = ~casos_nuevos, name = ~pais, type = 'scatter', mode = 'lines', color = ~pais)
36+
}
37+
38+
f <- list(
39+
family = 'Courier New, monospace',
40+
size = 18,
41+
color = '#7f7f7f'
42+
)
43+
44+
x <- list(
45+
title = 'Día desde alcanzar 5 casos',
46+
titlefont = f
47+
)
48+
49+
y <- list(
50+
title = ifelse(chart.c == 1, 'Número de casos', 'Número de casos nuevos'),
51+
titlefont = f,
52+
type = scale.c
53+
)
54+
55+
fig <- fig %>% layout(xaxis = x, yaxis = y)
56+
fig
57+
}

server_dir/tabla_evolutivo_server.R

+8-26
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,13 @@ library(plotly)
44

55
output$tabla_evolutivo_paises <- renderPlotly({
66
data.df <- GenerateCountryEvolutionData(cvirus_longer)
7-
max.c <- max(data.df$fecha)
8-
aux.df <- data.df[data.df$fecha == max.c, ]
9-
aux.df <- aux.df[order(aux.df$casos, decreasing = TRUE), ]
10-
aux.df <- aux.df[1:10, ]
11-
data.df <- data.df[data.df$pais %in% aux.df$pais, ]
12-
#data.df <- data.df[data.df$pais == 'Spain', ]
13-
fig <- plot_ly(data.df, x = ~dia_since_5, y = ~casos, name = ~pais, type = 'scatter', mode = 'lines', color = ~pais
14-
)
15-
16-
f <- list(
17-
family = "Courier New, monospace",
18-
size = 18,
19-
color = "#7f7f7f"
20-
)
21-
22-
x <- list(
23-
title = "Día desde alcanzar 5 casos",
24-
titlefont = f
25-
)
26-
27-
y <- list(
28-
title = "Número de casos",
29-
titlefont = f
30-
)
31-
32-
fig <- fig %>% layout(xaxis = x, yaxis = y)
7+
fig <- GenerateSummaryCountriesPlot(data.df, limit.c = input$limit, scale.c = input$scale, chart.c = 1)
8+
fig
9+
})
10+
11+
12+
output$tabla_evolutivo_paises_crecimiento <- renderPlotly({
13+
data.df <- GenerateCountryEvolutionData(cvirus_longer)
14+
fig <- GenerateSummaryCountriesPlot(data.df, limit.c = input$limit, scale.c = input$scale, chart.c = 2)
3315
fig
3416
})

ui_dir/tabla_evolutivo_ui.R

+18-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,25 @@
22
library(shinydashboard)
33

44
tab_evolutivo <- tabItem(
5-
tabName = "Tabla_evolutivo",
6-
# h1("Evolutivo desde caso 5"),
7-
# h2(textOutput("tabla_evolutivo")),
5+
tabName = 'Tabla_evolutivo',
6+
# h1('Evolutivo desde caso 5'),
7+
# h2(textOutput('tabla_evolutivo')),
88
br(),
99
fluidRow(
10-
plotlyOutput("tabla_evolutivo_paises")
10+
11+
12+
column(4,
13+
wellPanel(
14+
selectInput('scale', 'Scale Y-Axis', choices = c('log', 'linear'), selected = 'log', width = 100),
15+
selectInput('limit', label = h3('Visualización Top N'),
16+
choices = list('Top 5' = 5, 'Top 10' = 10, 'Todos' = 0),
17+
selected = 1)
18+
)
19+
),
20+
21+
column(8,
22+
plotlyOutput('tabla_evolutivo_paises'),
23+
plotlyOutput('tabla_evolutivo_paises_crecimiento')
24+
)
1125
)
1226
)

0 commit comments

Comments
 (0)