I have a user defined function that calculates daily covid cases for a given country and gives out a data frame:
fn_daily_cases <- function(Country_Name = India)
{
Country_Name <- rlang::quo_name(rlang::enquo(Country_Name))
# above line will insert quote to the value in variable Country_Name
df_gather %>% filter(Country.Region == Country_Name) %>%
mutate(daily_cases = abs(Cases_Count - lag(Cases_Count, default = 0))) %>%
# at beginning it will give 0 instead of NA
arrange(desc(Date))
}
fn_daily_cases(Spain)
####### output ########
Country.Region Date Cases_Count daily_cases
<chr> <date> <int> <dbl>
Spain 2020-12-11 1730575 10519
Spain 2020-12-10 1720056 7955
Spain 2020-12-09 1712101 9773
Spain 2020-12-08 1702328 0
Spain 2020-12-07 1702328 17681
Spain 2020-12-06 1684647 0
Spain 2020-12-05 1684647 0
Spain 2020-12-04 1684647 8745
Spain 2020-12-03 1675902 10127
Spain 2020-12-02 1665775 9331
Issue: When I try to plot this dataframe using another function then I am unable to display max( Cases_Count)
in subtitle
library(glue)
library(tidyverse)
fn_daily_cases_plot <- function(country_selected = India) {
fn_daily_cases({{country_selected}}) %>%
ggplot(aes(Date, y = daily_cases)) +
geom_line(col = "midnightblue") +
labs(title = glue("{quo_name(enquo(country_selected))} Daily Cases") ,
subtitle = glue("Total cases so far: {max(Cases_Count)}" )
) +
theme_light()
}
fn_daily_cases_plot(Spain)
##### output ######
Error in eval(parse(text = text, keep.source = FALSE), envir) : object 'Cases_Count' not found
Try to first save the data in a separate tibble
x
and then to call max(x$Cases_Count)
.
fn_daily_cases_plot <- function(country_selected = India) {
x <- fn_daily_cases({{country_selected}})
ggplot(x, aes(Date, y = daily_cases)) +
geom_line(col = "midnightblue") +
labs(title = glue("{quo_name(enquo(country_selected))} Daily Cases") ,
subtitle = glue("Total cases so far: {max(x$Cases_Count)}" )) +
theme_light()
}
Anyways, I don't know why you prefer glue
over the base alternative paste
(see also this question ).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.