I'm building a package in RStudio and using %+replace% in my code. During runtime this is no problem. However during build and test (testthat) an error is raised: R could not find function "%+replace%".
Unlying are the following messages: no visible global function definition for ‘%+replace%’
and Undefined global functions or variables:
%+replace%
I use the %+replace% while creating a new ggplot theme based on theme_bw(). Reprex:
my_theme <-
function(base_size = 11,
base_family = "Helvetica",
base_line_size = base_size / 170,
base_rect_size = base_size / 170)
{
ggplot2::theme_bw(
base_size = base_size,
base_family = base_family,
base_line_size = base_line_size
) %+replace%
ggplot2::theme(
# Set style for chart title.
plot.title = ggplot2::element_text(
size = ggplot2::rel(1.5),
colour = "dodgerblue3",
face = "bold"
),
complete = FALSE
}
which is then used in creating plots:
ggplot(iris, eas(x = Sepal.Length, y = Petal.Length, colour = Species)) +
geom_jitter() + my_theme() + labs(title = "IRIS plot with my theme")
I guess I have to prefix %+replace% with a package name but is that true, and if so what should be the package name? TIA.
The function %replace% is a function in the ggplot2 package. To use it in your package functions, you have multiple options:
Prefix it with ggplot2, i.e. ggplot2::%+replace%
If you're using roxygen2 for documentation, you can add the line #' @importFrom ggplot2 '%+replace%' and rebuild the documentation. This will add the line importFrom(ggplot2 ,'%+replace%') to the file NAMESPACE, which makes the function %+replace% available in your package's namespace (and thus you don't have to prefix with ggplot2::)
If you aren't using roxygen2, you can manually add importFrom(ggplot2 ,'%+replace%') to NAMESPACE
For more on this and other issues that may arise when using ggplot2 in package code, check out the very nice vignette Using ggplot2 in packages.
Yes I thought it was part of ggplot2, however I never could get it to work. I've tried the solutions you provide. The culprit is in the quote-ing I found out. I used to use ` but that results in a syntax error.
prefix with ggplot2::%+replace% results in an error. ) ggplot2
^
Calls: ... withr_with_dir -> force -> source_many -> source_one -> parse
Execution halted
I already imported ggplot2 into the NAMESPACE: did not work.
In the indeed very nice vignette I found the solution that is working for me. It's near at the bottom of the vignette: my_theme <-
function(base_size = 11,
base_family = "Helvetica",
base_line_size = base_size / 170,
base_rect_size = base_size / 170)
{
'%+replace%' <- ggplot2::'%+replace%' # nolint
ggplot2::theme_bw(
base_size = base_size,
base_family = base_family,
base_line_size = base_line_size
) %+replace%
ggplot2::theme(
plot.title = ggplot2::element_text(
size = ggplot2::rel(1.5),
colour = "dodgerblue3",
face = "bold"
),
complete = FALSE
}.
As shown, I had to use straight quotes. Italic quotes (what's their name?) resulted in the above error. Then funny thing happend, the lintr complained about the variable being wrong naming style (not camel case and the like), the # nolint comment fixed that.
Now it's working!
Many thanks for setting me in right direction.
I'm glad you figured out a solution that worked for your package! I tested your solution and confirmed it worked for me as well.
For the benefit of future readers of this thread, I also explicitly tried my 3 suggestions:
Sorry about this one. I forgot this was a strangely named function, so ggplot2::%+replace won't work. I tried using backticks (ggplot2::`%+replace` ), which works in the R console, but this surprisingly failed. I'm not sure what is going on.
I confirmed that @importFrom ggplot2 %+replace% works as expected. This is verbatim what the vignette recommends in this section. However, it only works if ggplot2 is listed in Imports, but not Suggests. Do you have ggplot2 listed as a suggested dependency for your package?
Same behavior as above since manually editing NAMESPACE achieves the same thing as using roxygen2.
It was absolutely not my intention to suggest you gave a wrong answer. My thought was that my system might be not-standard. I appologise for giving another impression. Having said this, I can confirm that the solution with importFrom ggplot2 %+replace% also works. I didn't use either backticks or quotes here.
The nice thing with this solution is that lintr doesn't complain anymore, so I can remove the # nolint.