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

WIP: vignette about using ggplot2 in a package #3344

Merged
merged 8 commits into from
Jun 4, 2019

Conversation

paleolimbot
Copy link
Member

This is a first draft of a vignette that gives guidance on best-practices related to ggplot2 use in packages (fixes #3319). I am sure that there are are things I expand on too much, things that I forgot to cover, or things that I got wrong! I wrote it based on seeing what code failed in the latest round of revdep checks (and what problems that code was trying to solve).

@paleolimbot paleolimbot requested a review from hadley May 30, 2019 22:55
@hadley hadley requested a review from lionel- May 31, 2019 21:02
Copy link
Member

@hadley hadley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking great so far!

vignettes/ggplot2-in-packages.Rmd Outdated Show resolved Hide resolved
vignettes/ggplot2-in-packages.Rmd Outdated Show resolved Hide resolved
vignettes/ggplot2-in-packages.Rmd Show resolved Hide resolved
vignettes/ggplot2-in-packages.Rmd Outdated Show resolved Hide resolved
vignettes/ggplot2-in-packages.Rmd Outdated Show resolved Hide resolved
col_summary(mpg, "drv")
```

To use the same kind of non-standard evaluation used by `aes()` and `vars()`, use `{{ col }}` to pass the unevaluated expression the user typed in `col` to `aes()`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps:

If the column name is supplied by the user, you can also interpolate it with {{ col }}. This tidy eval operator captures the expression supplied by the user and forwards it to another quoting function such as aes() or vars().

}
```

It is considered bad practice to implement an S3 generic like `plot()`, or `autoplot()` if you don't own the S3 object, as it makes it hard for the package developer who does have control over the S3 to implement the method themselves. This shouldn't stop you from creating your own functions to visualize these objects!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
It is considered bad practice to implement an S3 generic like `plot()`, or `autoplot()` if you don't own the S3 object, as it makes it hard for the package developer who does have control over the S3 to implement the method themselves. This shouldn't stop you from creating your own functions to visualize these objects!
It is considered bad practice to implement an S3 generic like `plot()`, or `autoplot()` if you don't own the S3 class, as it makes it hard for the package developer who does have control over the class to implement the method themselves. This shouldn't stop you from creating your own functions to visualize these objects!

mpg_drv_summary()
```

Even if you use many ggplot2 functions in your package, it is unwise to use ggplot2 in `Depends` or import the entire package into your `NAMESPACE`. Using ggplot2 in `Depends` will attach ggplot2 when your package is attached, which includes when your package is tested. This makes it difficult to ensure that others can use the functions in your package without attaching it (i.e., using `::`). Similarly, importing all 450 of ggplot2's exported objects into your namespace makes it difficult to separate the responsibility of your package and the responsibility of ggplot2, in addition to making it difficult for readers of your code to figure out where functions are coming from!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Even if you use many ggplot2 functions in your package, it is unwise to use ggplot2 in `Depends` or import the entire package into your `NAMESPACE`. Using ggplot2 in `Depends` will attach ggplot2 when your package is attached, which includes when your package is tested. This makes it difficult to ensure that others can use the functions in your package without attaching it (i.e., using `::`). Similarly, importing all 450 of ggplot2's exported objects into your namespace makes it difficult to separate the responsibility of your package and the responsibility of ggplot2, in addition to making it difficult for readers of your code to figure out where functions are coming from!
Even if you use many ggplot2 functions in your package, it is unwise to use ggplot2 in `Depends` or import the entire package into your `NAMESPACE` (e.g. with `@import ggplot2`). Using ggplot2 in `Depends` will attach ggplot2 when your package is attached, which includes when your package is tested. This makes it difficult to ensure that others can use the functions in your package without attaching it (i.e., using `::`). Similarly, importing all 450 of ggplot2's exported objects into your namespace makes it difficult to separate the responsibility of your package and the responsibility of ggplot2, in addition to making it difficult for readers of your code to figure out where functions are coming from!

vignettes/ggplot2-in-packages.Rmd Show resolved Hide resolved
vignettes/ggplot2-in-packages.Rmd Outdated Show resolved Hide resolved
vignettes/ggplot2-in-packages.Rmd Show resolved Hide resolved
vignettes/ggplot2-in-packages.Rmd Outdated Show resolved Hide resolved
vignettes/ggplot2-in-packages.Rmd Outdated Show resolved Hide resolved
…ebsites if they exist, clarify usage of .data; improve overall consistency
@paleolimbot paleolimbot merged commit d6581b0 into tidyverse:master Jun 4, 2019
@paleolimbot paleolimbot deleted the issue-3319-pkg-vignette branch June 4, 2019 18:29
@lock
Copy link

lock bot commented Dec 1, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Dec 1, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Vignette on using ggplot2 within a package
3 participants