RamiKrispin / coronavirus_dashboard

The Coronavirus Dashboard
https://ramikrispin.github.io/coronavirus_dashboard/
108 stars 133 forks source link

Error in validateCssUnit(sizeInfo$width) : #21

Closed stepminer closed 3 years ago

stepminer commented 3 years ago

Greetings,

I am using your codes modified by Antoine Soetewey to create a New Dashboard for Haiti as this country lacks a good dashboard https://statsandr.com/blog/how-to-create-a-simple-coronavirus-dashboard-specific-to-your-country-in-r/

When knitting to flexdashboard I am getting the following error:

Quitting from lines 156-185 (Haiti-Coronavirus-Dashboard.Rmd) Error in validateCssUnit(sizeInfo$width) : CSS units must be a single-element numeric or character vector Calls: ... -> need_screenshot -> toHTML -> validateCssUnit In addition: Warning message: package 'flexdashboard' was built under R version 4.0.3 Execution halted

I am using RStudio Version 1.3.1093. Do you have an idea why I am getting this error message and how to fix it. I would be very grateful to you.

The codes are below:

Best,

Patrick


title: "Coronavirus in Haiti" author: "Patrick Stephenson" output: flexdashboard::flex_dashboard: orientation: rows

social: ["facebook", "twitter", "linkedin"]

source_code: embed
vertical_layout: fill

#------------------ Packages ------------------
library(flexdashboard)
# install.packages("devtools")
# devtools::install_github("RamiKrispin/coronavirus", force = TRUE)
library(coronavirus)
data(coronavirus)
# View(coronavirus)
# max(coronavirus$date)
`%>%` <- magrittr::`%>%`
#------------------ Parameters ------------------
# Set colors
# https://www.w3.org/TR/css-color-3/#svg-color
confirmed_color <- "purple"
active_color <- "#1f77b4"
recovered_color <- "forestgreen"
death_color <- "red"
#------------------ Data ------------------
df <- coronavirus %>%
  # dplyr::filter(date == max(date)) %>%
  dplyr::filter(country == "Haiti") %>%
  dplyr::group_by(country, type) %>%
  dplyr::summarise(total = sum(cases)) %>%
  tidyr::pivot_wider(
    names_from = type,
    values_from = total
  ) %>%
  # dplyr::mutate(unrecovered = confirmed - ifelse(is.na(recovered), 0, recovered) - ifelse(is.na(death), 0, death)) %>%
  dplyr::mutate(unrecovered = confirmed - ifelse(is.na(death), 0, death)) %>%
  dplyr::arrange(-confirmed) %>%
  dplyr::ungroup() %>%
  dplyr::mutate(country = dplyr::if_else(country == "United Arab Emirates", "UAE", country)) %>%
  dplyr::mutate(country = dplyr::if_else(country == "Mainland China", "China", country)) %>%
  dplyr::mutate(country = dplyr::if_else(country == "North Macedonia", "N.Macedonia", country)) %>%
  dplyr::mutate(country = trimws(country)) %>%
  dplyr::mutate(country = factor(country, levels = country))
df_daily <- coronavirus %>%
  dplyr::filter(country == "Haiti") %>%
  dplyr::group_by(date, type) %>%
  dplyr::summarise(total = sum(cases, na.rm = TRUE)) %>%
  tidyr::pivot_wider(
    names_from = type,
    values_from = total
  ) %>%
  dplyr::arrange(date) %>%
  dplyr::ungroup() %>%
  #dplyr::mutate(active = confirmed - death - recovered) %>%
  dplyr::mutate(active = confirmed - death) %>%
  dplyr::mutate(
    confirmed_cum = cumsum(confirmed),
    death_cum = cumsum(death),
    # recovered_cum = cumsum(recovered),
    active_cum = cumsum(active)
  )
df1 <- coronavirus %>% dplyr::filter(date == max(date))

Summary

Row {data-width=400}

confirmed {.value-box}

valueBox(
  value = paste(format(sum(df$confirmed), big.mark = ","), "", sep = " "),
  caption = "Total confirmed cases",
  icon = "fas fa-user-md",
  color = confirmed_color
)

death {.value-box}

valueBox(
  value = paste(format(sum(df$death, na.rm = TRUE), big.mark = ","), " (",
    round(100 * sum(df$death, na.rm = TRUE) / sum(df$confirmed), 1),
    "%)",
    sep = ""
  ),
  caption = "Death cases (death rate)",
  icon = "fas fa-heart-broken",
  color = death_color
)

Row

Daily cumulative cases by type (Haiti only)

plotly::plot_ly(data = df_daily) %>%
  plotly::add_trace(
    x = ~date,
    # y = ~active_cum,
    y = ~confirmed_cum,
    type = "scatter",
    mode = "lines+markers",
    # name = "Active",
    name = "Confirmed",
    line = list(color = active_color),
    marker = list(color = active_color)
  ) %>%

  # plotly::add_annotations(
  #   x = as.Date("2020-02-04"),
  #   y = 1,
  #   text = paste("First case"),
  #   xref = "x",
  #   yref = "y",
  #   arrowhead = 5,
  #   arrowhead = 3,
  #   arrowsize = 1,
  #   showarrow = TRUE,
  #   ax = -10,
  #   ay = -90
  # ) %>%

  plotly::layout(
    title = "",
    yaxis = list(title = "Cumulative number of cases"),
    xaxis = list(title = "Date"),
    legend = list(x = 0.1, y = 0.9),
    hovermode = "compare"
  )

Comparison

Column {data-width=400}

Daily new confirmed cases

daily_confirmed <- coronavirus %>%
  dplyr::filter(type == "confirmed") %>%
  dplyr::filter(date >= "2020-02-29") %>%
  dplyr::mutate(country = country) %>%
  dplyr::group_by(date, country) %>%
  dplyr::summarise(total = sum(cases)) %>%
  dplyr::ungroup() %>%
  tidyr::pivot_wider(names_from = country, values_from = total)
#----------------------------------------
# Plotting the data
daily_confirmed %>%
  plotly::plot_ly() %>%
  plotly::add_trace(
    x = ~date,
    y = ~Haiti,
    type = "scatter",
    mode = "lines+markers",
    name = "Haiti"
  ) %>%
  # plotly::add_trace(
  #   x = ~date,
  #   y = ~France,
  #   type = "scatter",
  #   mode = "lines+markers",
  #   name = "France"
  # ) %>%
  # plotly::add_trace(
  #   x = ~date,
  #   y = ~Spain,
  #   type = "scatter",
  #   mode = "lines+markers",
  #   name = "Spain"
  # ) %>%
  plotly::add_trace(
    x = ~date,
    y = ~`Dominican Republic`,
    type = "scatter",
    mode = "lines+markers",
    name = "Dominican Republic"
  ) %>%
  plotly::add_trace(
    x = ~date,
    y = ~Jamaica,
    type = "scatter",
    mode = "lines+markers",
    name = "Jamaica"
  ) %>%
  plotly::add_trace(
    x = ~date,
    y = ~Cuba,
    type = "scatter",
    mode = "lines+markers",
    name = "Cuba"
  ) %>%
  plotly::layout(
    title = "",
    legend = list(x = 0.7, y = 0.9),
    yaxis = list(title = "New confirmed cases"),
    xaxis = list(title = "Date"),
    # paper_bgcolor = "black",
    # plot_bgcolor = "black",
    # font = list(color = 'white'),
    hovermode = "compare",
    margin = list(
      # l = 60,
      # r = 40,
      b = 10,
      t = 10,
      pad = 2
    )
  )

Cases distribution by type

df_EU <- coronavirus %>%
  # dplyr::filter(date == max(date)) %>%
  dplyr::filter(country == "Haiti" |
    country == "Dominican Republic " |
    country == "Jamaica" |
    country == "Cuba") %>%
  dplyr::group_by(country, type) %>%
  dplyr::summarise(total = sum(cases)) %>%
  tidyr::pivot_wider(
    names_from = type,
    values_from = total
  ) %>%
  # dplyr::mutate(unrecovered = confirmed - ifelse(is.na(recovered), 0, recovered) - ifelse(is.na(death), 0, death)) %>%
  dplyr::mutate(unrecovered = confirmed - ifelse(is.na(death), 0, death)) %>%
  dplyr::arrange(confirmed) %>%
  dplyr::ungroup() %>%
  dplyr::mutate(country = dplyr::if_else(country == "United Arab Emirates", "UAE", country)) %>%
  dplyr::mutate(country = dplyr::if_else(country == "Mainland China", "China", country)) %>%
  dplyr::mutate(country = dplyr::if_else(country == "North Macedonia", "N.Macedonia", country)) %>%
  dplyr::mutate(country = trimws(country)) %>%
  dplyr::mutate(country = factor(country, levels = country))
plotly::plot_ly(
  data = df_EU,
  x = ~country,
  # y = ~unrecovered,
  y = ~ confirmed,
  # text =  ~ confirmed,
  # textposition = 'auto',
  type = "bar",
  name = "Confirmed",
  marker = list(color = active_color)
) %>%
  plotly::add_trace(
    y = ~death,
    # text =  ~ death,
    # textposition = 'auto',
    name = "Death",
    marker = list(color = death_color)
  ) %>%
  plotly::layout(
    barmode = "stack",
    yaxis = list(title = "Total cases"),
    xaxis = list(title = ""),
    hovermode = "compare",
    margin = list(
      # l = 60,
      # r = 40,
      b = 10,
      t = 10,
      pad = 2
    )
  )

Map

World map of cases (use + and - icons to zoom in/out)

# map tab added by Art Steinmetz
library(leaflet)
library(leafpop)
library(purrr)
cv_data_for_plot <- coronavirus %>%
  # dplyr::filter(country == "Haiti") %>%
  dplyr::filter(cases > 0) %>%
  dplyr::group_by(country, province, lat, long, type) %>%
  dplyr::summarise(cases = sum(cases)) %>%
  dplyr::mutate(log_cases = 2 * log(cases)) %>%
  dplyr::ungroup()
cv_data_for_plot.split <- cv_data_for_plot %>% split(cv_data_for_plot$type)
pal <- colorFactor(c("orange", "red", "green"), domain = c("confirmed", "death", "recovered"))
map_object <- leaflet() %>% addProviderTiles(providers$Stamen.Toner)
names(cv_data_for_plot.split) %>%
  purrr::walk(function(df) {
    map_object <<- map_object %>%
      addCircleMarkers(
        data = cv_data_for_plot.split[[df]],
        lng = ~long, lat = ~lat,
        #                 label=~as.character(cases),
        color = ~ pal(type),
        stroke = FALSE,
        fillOpacity = 0.8,
        radius = ~log_cases,
        popup = leafpop::popupTable(cv_data_for_plot.split[[df]],
          feature.id = FALSE,
          row.numbers = FALSE,
          zcol = c("type", "cases", "country", "province")
        ),
        group = df,
        #                 clusterOptions = markerClusterOptions(removeOutsideVisibleBounds = F),
        labelOptions = labelOptions(
          noHide = F,
          direction = "auto"
        )
      )
  })
map_object %>%
  addLayersControl(
    overlayGroups = names(cv_data_for_plot.split),
    options = layersControlOptions(collapsed = FALSE)
  )

About

The Coronavirus Dashboard: the case of Haiti

This Coronavirus dashboard: the case of Haiti provides an overview of the 2019 Novel Coronavirus COVID-19 (2019-nCoV) epidemic for Haiti. This dashboard is built with R using the R Makrdown framework and was adapted from this dashboard{target="_blank"} by Rami Krispin.

Code

The code behind this dashboard is available on GitHub{target="_blank"}.

Data

The input data for this dashboard is the dataset available from the {coronavirus}{target="_blank"} R package. Make sure to download the development version of the package to have the latest data:

install.packages("devtools")
devtools::install_github("RamiKrispin/coronavirus")

The raw data is pulled from the Johns Hopkins University Center for Systems Science and Engineering (JHU CCSE) Coronavirus repository{target="_blank"}.

Information

More information about this dashboard (and how to replicate it for your own country) can be found in this article.

Update

The data is as of r format(max(coronavirus$date), "%A %B %d, %Y") and the dashboard has been updated on r format(Sys.time(), "%A %B %d, %Y").


Go back to statsandr.com (blog) or antoinesoetewey.com (personal website).

stepminer commented 3 years ago

I have circumvented part of the problem for the first plot, in Daily cumulative cases by type (Haiti only) I have replaced

plotly::plot_ly(data = df_daily) %>%

with: plotly::plot_ly(data = df_daily, height= 400, width=800 ) %>%

But this strategy does not work for the other plots

RamiKrispin commented 3 years ago

Hi @stepminer, could you please send me a link to the code on github?

stepminer commented 3 years ago

Greetings Rami,

Thanks for the great codes in the coronavirus dashboard. Just for your information, in case you are asked the same question: The issue was with the old htmlwidgets package in RStudio. After a reinstall of the newest fixed version, everything is running smoothly (no error codes).

Thanks again for your time and codes. The dashboard is now public on Rpubs and I will be updating it almost daily (when you refresh the data). Have a look and please leave a comment, that will be appreciated.

https://rpubs.com/stepminer/709695

Best,

Pat

On Sat, Jan 9, 2021 at 12:27 AM Rami Krispin notifications@github.com wrote:

Hi @stepminer https://github.com/stepminer, could you please send me a link to the code on github?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/RamiKrispin/coronavirus_dashboard/issues/21#issuecomment-757099730, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQ3O6KUQ72ZAIDL6NPSF6QDSY7SMJANCNFSM4VPLGXCQ .

RamiKrispin commented 3 years ago

Thanks Pat,

Happy to see that you were able to solve and deploy the dashboard. Nice dashboard!

Stay safe, Rami

stepminer commented 3 years ago

Hello Rami,

Hope you are doing well. Sorry to bother you again, I am trying to add a new panel to the Haiti dashboard for Active cases. I am getting a logic error using your codes or an alternative one:

The added code after line 139 is below is as per your codes template:

active {.value-box}

valueBox(
    value = paste(format(sum(df$unrecovered, na.rm = TRUE), big.mark =
","), "
 (",
                  round(100 * sum(df$unrecovered, na.rm = TRUE) /
sum(df$confirmed), 1),"%)",
                  sep = "" ),
    caption = "Active cases (% of total cases)", icon = "fas fa-ambulance",

    color = active_color
)

Alternatively I have tried:

active {.value-box}

valueBox(
    value = paste(format(sum(df$active, na.rm = TRUE), big.mark = ","), "
 (",
                  round(100 * sum(df$active, na.rm = TRUE) /
sum(df$confirmed), 1),"%)",
                  sep = "" ),
    caption = "Active cases (% of total cases)", icon = "fas fa-ambulance",

    color = active_color
)

I am getting either a high number (with your codes) or zero with the alternative codes.

I would be grateful for your advice. I am attaching the .Rmd file as well as a picture of the resulting codes.

Best,

Pat

On Mon, Jan 18, 2021 at 4:21 PM Rami Krispin notifications@github.com wrote:

Closed #21 https://github.com/RamiKrispin/coronavirus_dashboard/issues/21.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/RamiKrispin/coronavirus_dashboard/issues/21#event-4219866230, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQ3O6KUSVXTHKDDTO6STF7TS2SQ4PANCNFSM4VPLGXCQ .

stepminer commented 3 years ago

Forget about it Rami,

I just modified line 47 of Antoine's codes and it works and is accurate

dplyr::mutate(unrecovered = confirmed - recovered - death , ifelse(is.na(death), 0, death)) %>%

Thanks again,

Best,

Pat

https://rpubs.com/stepminer/709695

On Tue, Jan 19, 2021 at 5:37 AM Pat Stephenson stepminer@gmail.com wrote:

Hello Rami,

Hope you are doing well. Sorry to bother you again, I am trying to add a new panel to the Haiti dashboard for Active cases. I am getting a logic error using your codes or an alternative one:

The added code after line 139 is below is as per your codes template:

active {.value-box}

valueBox(
    value = paste(format(sum(df$unrecovered, na.rm = TRUE), big.mark =
","), "
 (",
                  round(100 * sum(df$unrecovered, na.rm = TRUE) /
sum(df$confirmed), 1),"%)",
                  sep = "" ),
    caption = "Active cases (% of total cases)", icon = "fas fa-ambulance",

    color = active_color
)

Alternatively I have tried:

active {.value-box}

valueBox(
    value = paste(format(sum(df$active, na.rm = TRUE), big.mark = ","), "
 (",
                  round(100 * sum(df$active, na.rm = TRUE) /
sum(df$confirmed), 1),"%)",
                  sep = "" ),
    caption = "Active cases (% of total cases)", icon = "fas fa-ambulance",

    color = active_color
)

I am getting either a high number (with your codes) or zero with the alternative codes.

I would be grateful for your advice. I am attaching the .Rmd file as well as a picture of the resulting codes.

Best,

Pat

On Mon, Jan 18, 2021 at 4:21 PM Rami Krispin notifications@github.com wrote:

Closed #21 https://github.com/RamiKrispin/coronavirus_dashboard/issues/21.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/RamiKrispin/coronavirus_dashboard/issues/21#event-4219866230, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQ3O6KUSVXTHKDDTO6STF7TS2SQ4PANCNFSM4VPLGXCQ .

stepminer commented 3 years ago

Hello Rami,

Sorry to bother you again. Did you change something in the coronavirus data structure? I am getting an error at line 215 Error: Column 1 must be named.


daily_confirmed <- coronavirus %>%
  dplyr::filter(type == "confirmed") %>%
  dplyr::filter(date >= "2020-02-29") %>%
  dplyr::mutate(country = country) %>%
  dplyr::group_by(date, country) %>%
  dplyr::summarise(total = sum(cases)) %>%
  dplyr::ungroup() %>%
  tidyr::pivot_wider(names_from = country, values_from = total)

I am attaching a screenshot as well as the Rmd file.

Thanks for your advice.

Pat

On Tue, Jan 19, 2021 at 7:39 AM Pat Stephenson <stepminer@gmail.com> wrote:

> Forget about it Rami,
>
>   I just modified line 47 of Antoine's codes and it works and is accurate
>
>   dplyr::mutate(unrecovered = confirmed - recovered - death , ifelse(is.na(death),
> 0, death)) %>%
>
> Thanks again,
>
> Best,
>
> Pat
>
> https://rpubs.com/stepminer/709695
>
> On Tue, Jan 19, 2021 at 5:37 AM Pat Stephenson <stepminer@gmail.com>
> wrote:
>
>> Hello Rami,
>>
>> Hope you are doing well. Sorry to bother you again, I am trying to add a
>> new panel to the Haiti dashboard for Active cases. I am getting a logic
>> error using your codes or an alternative one:
>>
>>
>> The added code after line 139 is below is as per your codes template:
>>
>> ### active {.value-box}
>>
>> ```{r}
>> valueBox(
>>     value = paste(format(sum(df$unrecovered, na.rm = TRUE), big.mark =
>> ","), "
>>  (",
>>                   round(100 * sum(df$unrecovered, na.rm = TRUE) /
>> sum(df$confirmed), 1),"%)",
>>                   sep = "" ),
>>     caption = "Active cases (% of total cases)", icon = "fas
>> fa-ambulance",
>>
>>     color = active_color
>> )
>> ```
>>
>> Alternatively I have tried:
>>
>> ### active {.value-box}
>>
>> ```{r}
>> valueBox(
>>     value = paste(format(sum(df$active, na.rm = TRUE), big.mark = ","), "
>>  (",
>>                   round(100 * sum(df$active, na.rm = TRUE) /
>> sum(df$confirmed), 1),"%)",
>>                   sep = "" ),
>>     caption = "Active cases (% of total cases)", icon = "fas
>> fa-ambulance",
>>
>>     color = active_color
>> )
>> ```
>> I am getting either a high number (with your codes) or zero with the
>> alternative codes.
>>
>> I would be grateful for your advice. I am attaching the .Rmd file as well
>> as a picture of the resulting codes.
>>
>> Best,
>>
>> Pat
>>
>> On Mon, Jan 18, 2021 at 4:21 PM Rami Krispin <notifications@github.com>
>> wrote:
>>
>>> Closed #21
>>> <https://github.com/RamiKrispin/coronavirus_dashboard/issues/21>.
>>>
>>> —
>>> You are receiving this because you were mentioned.
>>> Reply to this email directly, view it on GitHub
>>> <https://github.com/RamiKrispin/coronavirus_dashboard/issues/21#event-4219866230>,
>>> or unsubscribe
>>> <https://github.com/notifications/unsubscribe-auth/AQ3O6KUSVXTHKDDTO6STF7TS2SQ4PANCNFSM4VPLGXCQ>
>>> .
>>>
>>