Лично я предпочел бы аккуратный eval. Однако основная проблема заключается в том, что вы помещаете кавычки вокруг ваших переменных country
и col
внутри тела вашего развлечения. Попробуйте следующее:
trends <- function(country, col){
#filtering the specified country of interest and selecting fixed columns, plus optional column to go with them.
my_data = df %>% dplyr::filter(Countries == country) %>%
select(col = col, day, month)
my_plot = ggplot(my_data, aes(x=day, y=col)) + geom_line() + facet_wrap(~month)
print(my_plot)
}
trends('Cameroon', col='CFR')
Используя набор данных gapminder в качестве примера набора данных:
library(ggplot2)
library(dplyr)
df <- gapminder::gapminder %>%
rename(Countries = country)
trends <- function(country, col) {
#filtering the specified country of interest and selecting fixed columns, plus optional column to go with them.
my_data = df %>% dplyr::filter(Countries == country) %>%
select(col = col, year, continent)#, day, month)
#my_plot = ggplot(my_data, aes(x=day, y=col)) + geom_line() + facet_wrap(~month)
my_plot = ggplot(my_data, aes(x=year, y=col)) + geom_line() + facet_wrap(~continent)
print(my_plot)
}
trends('Cameroon', col='lifeExp')
