R里面有很多工具包定欧,接收的數(shù)據(jù)不能直接處理字符串
像ggplot2猴伶, ggplot(data, aes(x=AAA))
,
這個的AAA
必須是data
中的表頭霉咨。
如果你自己提前定義quote='AAA'
小槐,
然后再ggplot(data, aes(x=quote))
就會報錯拇派。
還有dplyr、subset都是如此
這種情況凿跳,如果自己希望去寫一個靈活的函數(shù)時件豌,就很麻煩。
提供一個解決方案
library(ggplot2)
library(rlang)
mine_func = function(data, x){
quo_x = sym(x)
ggplot(diamonds, aes(x= !! quo_x))+
geom_bar()
}
quote='cut'
mine_func(diamonds, quote)