Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

variables in package function didn't work properly with {{}} #5393

Closed
gaospecial opened this issue Aug 16, 2023 · 2 comments
Closed

variables in package function didn't work properly with {{}} #5393

gaospecial opened this issue Aug 16, 2023 · 2 comments

Comments

@gaospecial
Copy link

I have wrote three functions as according to the official guidelines in Using ggplot2 in packages.

#' @importFrom ggplot2 ggplot geom_boxplot aes
myplot1 = function(data, x = "group", y = "weight"){
  ggplot(data, aes({{ x }}, {{ y }})) +
    geom_boxplot()
}

#' @importFrom ggplot2 ggplot geom_boxplot aes_string
myplot2 = function(data, x = "group", y = "weight"){
  ggplot(data, aes_string(x, y)) +
    geom_boxplot()
}

#' @importFrom ggplot2 ggplot geom_boxplot aes
#' @importFrom rlang .data
myplot3 = function(data, x = "group", y = "weight"){
  ggplot(data, aes(.data[[x]], .data[[y]])) +
    geom_boxplot()
}

All these functions are designed to have identical results. However, they don't.

data("PlantGrowth")
myplot1(PlantGrowth)
myplot2(PlantGrowth)
myplot3(PlantGrowth)

image

I'am wondering whether I am wrong with coding of myplot1(). Please give me some feedback.

@thomasp85
Copy link
Member

In the first case you are passing in strings which makes the call equivalent to

ggplot(data, aes("group", "weight")) +
    geom_boxplot()

which is not correct code

You should not pass in the aesthetics as strings but as unquoted expressions (i.e. how you would write them inside aes())

@gaospecial
Copy link
Author

In the first case you are passing in strings which makes the call equivalent to

ggplot(data, aes("group", "weight")) +
    geom_boxplot()

which is not correct code

You should not pass in the aesthetics as strings but as unquoted expressions (i.e. how you would write them inside aes())

Yes, you are right. myplot1(PlantGrowth, x = group, y = weight) works for me.

Thanks for your immediate response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants