gpt4 book ai didi

r - 为什么 plotly() 和 enquo + !!在冲突?

转载 作者:行者123 更新时间:2023-12-01 12:04:50 26 4
gpt4 key购买 nike

我正在编写一个使用 plot_ly 绘制圆饼图的函数。参数 labels = ~ 中的波浪号 (~) 与反引号运算符 !! 冲突。这个问题有解决方案吗?

pieplotr <- function (df, Property){

Property_Name <- enquo(Property)

Pie <- plot_ly(df,
labels = ~!!Property_Name,
type = "pie")

return(Pie)

}
Toy_dataframe <- data.frame(
Sex = c("Male", "Female", "Female", "Male", "Male","NA"),
Height = c(12,11,7,3,9,NA),
Name = c("John", "Dora", "Dora","Dora", "Anna", "John"),
Last = c("Henry", "Paul", "House", "Houze", "Henry", "Bill"),
Location = c("Chicago", "Chicago", "Portland", "NYC", "NYC", "NYC"),
stringsAsFactors = TRUE)

例如

pieplotr(df = Toy_dataframe,
Property = Name)

我希望返回一个圆饼图,但我收到以下错误消息:

Error in as.list.environment(x, all.names = TRUE) :
object 'Name' not found

最佳答案

我们可以在创建表达式后求值uate

pieplotr <- function (df, Property){

Property_Name <- rlang::enquo(Property)
rlang::eval_tidy(
rlang::quo_squash(quo(
plot_ly(df, labels = ~!!Property_Name, type = "pie")
)))


}




pieplotr(df = Toy_dataframe, Property = Name)

-输出

enter image description here


或者删除~!!

pieplotr <- function (df, Property){

Property_Name <- rlang::enquo(Property)

plot_ly(df, labels = Property_Name, type = "pie")



}

pieplotr(df = Toy_dataframe, Property = Name)

关于r - 为什么 plotly() 和 enquo + !!在冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58754429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com