英文:
Problems with quasiquotation inside ggplot custom function using ggmosaic::product
问题
I want to create a function that taking a data base, variable_1 y variable_2 builds a mosaic plot!
var_1<-rep(c("A","B","C","B","A","C"),10)
var_2<-c(rep(c("1","0"),10),rep("0",5),rep(c("0","1","0"),10),rep("1",5) )
dat<-data.frame(var_1,var_2)
The function takes 3 arguments as explained:
mosaic_fig<-function(db,var1,var2){
library(ggmosaic)
mosac <- ggplot2::ggplot(data = db) +
geom_mosaic(ggplot2::aes(x = product({{var2}}), fill = {{var1}})) +
ggplot2::labs(y="", x="", title = "")
return(mosac)
}
Now I try to use the function with the data I just provided and this happens:
windows()
mosaic_fig(dat,var_1,var_2)
Warning message:
Computation failed in `stat_mosaic()`
Caused by error in `[.data.frame`:
! undefined columns selected
It is clear I'm having problems with the quasiquotation in the product() function. I have used quasiquotation in other ggplot functions and it worked nicely... why not here?
英文:
I want to create a function that taking a data base, variable_1 y variable_2 builds a mosaic plot!
var_1<-rep(c("A","B","C","B","A","C"),10)
var_2<-c(rep(c("1","0"),10),rep("0",5),rep(c("0","1","0"),10),rep("1",5) )
dat<-data.frame(var_1,var_2)
The function takes 3 arguments as explained:
mosaic_fig<-function(db,var1,var2){
library(ggmosaic)
mosac <- ggplot2::ggplot(data = db) +
geom_mosaic(ggplot2::aes(x = product({{var2}}), fill = {{var1}})) +
ggplot2::labs(y="", x="", title = "")
return(mosac)
}
Now I try to use the function with the data I just provided and this happens:
windows()
mosaic_fig(dat,var_1,var_2)
Warning message:
Computation failed in `stat_mosaic()`
Caused by error in `[.data.frame`:
! undefined columns selected
It is clear I'm having problems with the quasiquotation in the product() function. I have used quasiquotation in other ggplot functions and it worked nicely... why not here?
答案1
得分: 4
你需要使用 rlang::enexprs
替代 rlang::exprs
(ggmosaic::product
调用的函数)。
从 help("topic-defuse")
中:
你可以使用
expr()
来解析你自己的 R 表达式(...)
你可以使用以 en 为前缀的操作符来解析用户提供的表达式(...)
var_1 <- rep(c("A", "B", "C", "B", "A", "C"), 10)
var_2 <- c(rep(c("1", "0"), 10), rep("0", 5), rep(c("0", "1", "0"), 10), rep("1", 5))
dat <- data.frame(var_1, var_2)
## 最好不要在函数内部要求加载库
library(ggmosaic)
#> 加载需要的包: ggplot2
mosaic_fig <- function(db, var1, var2) {
ggplot2::ggplot(data = db) +
ggmosaic::geom_mosaic(ggplot2::aes(x = rlang::enexprs(var2), fill = {{ var1 }})) +
ggplot2::labs(y = "", x = "", title = "")
}
mosaic_fig(dat, var_1, var_2)
#> 警告: `unite_()` 在 tidyr 1.2.0 中已弃用。
#> 请使用 `unite()` 代替。
#> 弃用功能可能在 ggmosaic 包中使用,请在 <https://github.com/haleyjeppson/ggmosaic> 报告问题。
![](https://i.stack.imgur.com/vSb9c.png)<!-- -->
<sup>创建于 2023-04-10,使用 [reprex v2.0.2](https://reprex.tidyverse.org)</sup>
<details>
<summary>英文:</summary>
You will need `rlang::enexprs` instead of `rlang::exprs` (which is called by `ggmosaic::product`).
From `help("topic-defuse")`:
> You can defuse your own R expressions with expr(). (...)
You can defuse the expressions supplied by the user of your function with the en-prefixed operators (...)
``` r
var_1 <- rep(c("A", "B", "C", "B", "A", "C"), 10)
var_2 <- c(rep(c("1", "0"), 10), rep("0", 5), rep(c("0", "1", "0"), 10), rep("1", 5))
dat <- data.frame(var_1, var_2)
## better not to require libraries within a function
library(ggmosaic)
#> Loading required package: ggplot2
mosaic_fig <- function(db, var1, var2) {
ggplot2::ggplot(data = db) +
ggmosaic::geom_mosaic(ggplot2::aes(x = rlang::enexprs(var2), fill = {{ var1 }})) +
ggplot2::labs(y = "", x = "", title = "")
}
mosaic_fig(dat, var_1, var_2)
#> Warning: `unite_()` was deprecated in tidyr 1.2.0.
#> ℹ Please use `unite()` instead.
#> ℹ The deprecated feature was likely used in the ggmosaic package.
#> Please report the issue at <https://github.com/haleyjeppson/ggmosaic>.
<!-- -->
<sup>Created on 2023-04-10 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论