选择&;从函数中的数据框打印可选列,R

选择&;从函数中的数据框打印可选列,R,r,function,ggplot2,R,Function,Ggplot2,我试图用ggplot绘制一个折线图,用一个我称之为趋势的函数。我希望在将df传递到函数wrt的固定列选项中时,可以选择df中的任何列,并选择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 %&g

我试图用ggplot绘制一个折线图,用一个我称之为趋势的函数。我希望在将df传递到函数wrt的固定列选项中时,可以选择df中的任何列,并选择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 = paste0('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')

请帮忙。我尝试了其他类似场景的建议响应,但没有效果。

因为您使用字符输入作为函数参数,所以可以使用
!!sym(col)
需要时。但是,我不太确定“附加的可选列”在哪里。在你的情节中应该在哪里使用它们。你需要提供一个更好的例子来说明你的想法

trends <- function(country, col){
  my_data =  df %>% dplyr::filter(Countries == country) %>% 
    select(!! sym(col), day, month) 
  my_plot = ggplot(my_data, aes(x=day,  y= !! sym(col))) +
    geom_line() +
    facet_wrap(~ month)
  print(my_plot)
}
trends%dplyr::filter(国家==国家)%>%
选择(!!符号(列)、日、月)
my_plot=ggplot(my_数据,aes(x=天,y=!!sym(列)))+
geom_线()+
面包(~月)
打印(我的图)
}

因为您使用字符输入作为函数参数,所以可以使用
!!sym(col)
需要时。但是,我不太确定“附加的可选列”在哪里。在你的情节中应该在哪里使用它们。你需要提供一个更好的例子来说明你的想法

trends <- function(country, col){
  my_data =  df %>% dplyr::filter(Countries == country) %>% 
    select(!! sym(col), day, month) 
  my_plot = ggplot(my_data, aes(x=day,  y= !! sym(col))) +
    geom_line() +
    facet_wrap(~ month)
  print(my_plot)
}
trends%dplyr::filter(国家==国家)%>%
选择(!!符号(列)、日、月)
my_plot=ggplot(my_数据,aes(x=天,y=!!sym(列)))+
geom_线()+
面包(~月)
打印(我的图)
}

就个人而言,我更喜欢整洁的评估。然而,主要的问题是,在你的游戏主体中,你在你的VAR
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')
trends%dplyr::filter(国家==国家)%>%
选择(列=列、日、月)
my_plot=ggplot(my_data,aes(x=day,y=col))+geom_line()+facet_wrap(~month)
打印(我的图)
}
趋势(“喀麦隆”,col='CFR')
使用gapminder数据集作为示例数据集:

库(ggplot2)
图书馆(dplyr)
df%
重命名(国家=国家)
趋势%dplyr::过滤器(国家==国家)%>%
选择(列=列,年,洲)#,日,月)
#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(~大陆)
打印(我的图)
}
趋势(“喀麦隆”,col='lifeExp')

就个人而言,我更喜欢整洁的评估。然而,主要的问题是,在你的游戏主体中,你在你的VAR
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')
trends%dplyr::filter(国家==国家)%>%
选择(列=列、日、月)
my_plot=ggplot(my_data,aes(x=day,y=col))+geom_line()+facet_wrap(~month)
打印(我的图)
}
趋势(“喀麦隆”,col='CFR')
使用gapminder数据集作为示例数据集:

库(ggplot2)
图书馆(dplyr)
df%
重命名(国家=国家)
趋势%dplyr::过滤器(国家==国家)%>%
选择(列=列,年,洲)#,日,月)
#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(~大陆)
打印(我的图)
}
趋势(“喀麦隆”,col='lifeExp')

这是一个评估问题。检查小案例如果您需要更多帮助,请帮助我们帮助您并提供您的数据片段。这是一个评估问题。如果您需要更多帮助,请帮助我们帮助您并提供您的数据片段。非常感谢stefan!成功了。只是我的月份没有按时间顺序排列而不是1-2-3月,我得到了2-1-3月。不管怎样,这是可以管理的。非常感谢stefan!成功了。只是我的月份没有像1-2-3月那样按时间顺序排列,我得到了2-1-3月。不管怎样,它是可以管理的。非常感谢@TimTeaFan,它成功了。我很惊讶,你竟然没有按照我的想法解决所有细节问题。太棒了!。如果你不介意我问我在哪里可以学到更多关于这个!!sym函数。另外,这个月的facet_网格没有按时间顺序排列,而不是1-2-3月,我得到了2-1-3月。请再次帮助。谢谢。@Dezzy7model我想我在“用dplyr编程”vgnette的旧版本中读到了关于
sym
。显然,这个小插曲已经更新了。另一个好的来源是哈德利·威克汉姆的书《高级R》。关于你的
facet\u网格的排序
你可以遵循,它应该会起作用。非常感谢@TimTeaFan,它起作用了。我很惊讶,你竟然没有按照我的想法解决所有细节问题。太棒了!。如果你不介意我问我在哪里可以学到更多关于这个!!sym函数。另外,这个月的facet_网格没有按时间顺序排列,而不是1-2-3月,我得到了2-1-3月。请再次帮助。谢谢。@Dezzy7model我想我在“用dplyr编程”vgnette的旧版本中读到了关于
sym
。显然,这个小插曲已经更新了。另一个好的来源是哈德利·威克汉姆的书《高级R》。关于
facet\u网格的顺序,您可以遵循,它应该会起作用。