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

CRAN Warning when Non-breaking space in default function argument value #1342

Closed
zeehio opened this issue May 2, 2022 · 1 comment · Fixed by #1383
Closed

CRAN Warning when Non-breaking space in default function argument value #1342

zeehio opened this issue May 2, 2022 · 1 comment · Fixed by #1383
Labels
bug an unexpected problem or unintended behavior rd ✍️
Milestone

Comments

@zeehio
Copy link

zeehio commented May 2, 2022

Hi,

Thanks for your time and work on the roxygen2 package. It makes developing R packages much more convenient to me.

I have a function that takes a string with a default value that contains non-breaking spaces. When I use devtools::document() and afterwards devtools::check() I get a WARNING pointing out a mismatch between my code and the help page.

A minimal function with roxygen documentation that gives the WARNING:

#' A dummy function
#'
#' @param str_with_nbsp A string, potentially with non-breaking spaces
#'
#' @return Not relevant
#' @export
#'
myfun <- function(str_with_nbsp = "x\u00a0=\u00a04") {print(str_with_nbsp)}

You can find a minimal package that includes this function, with github actions set up here: https://github.com/zeehio/testnbsp

The CRAN WARNING (not that helpful to read given that the regular space and the non-breaking space look the same...)

   Codoc mismatches from documentation object 'myfun':
   myfun
     Code: function(str_with_nbsp = "x = 4")
     Docs: function(str_with_nbsp = "x = 4")
     Mismatches in argument default values:
       Name: 'str_with_nbsp' Code: "x = 4" Docs: "x = 4"

Here is some reproducible example that could be easily converted into a unit test for this bug:

myfun <- function(str_with_nbsp = "x\u00a0=\u00a04") {print(str_with_nbsp)}
# The wrap_usage function replaces all my nbsp characters by regular characters
fun_usage <- roxygen2:::wrap_usage("myfun", identity, formals = formals(myfun))
fun_usage
#> <rd> myfun(str_with_nbsp = "x = 4")
# The nbsp 00a0 character was replaced by regular spaces:
grepl(pattern = "\u00a0", as.character(fun_usage)) # I expected this to be TRUE
#> [1] FALSE

Created on 2022-05-02 by the reprex package (v2.0.1)

After looking at the code, I have been able to see that the replacement happens here:

out <- gsub("\u{A0}", " ", out, useBytes = TRUE)

It feels like first roxygen2 introduces: \u{A0}=\u{A0} (in args_string). Afterwards it replaces all nbsp with regular spaces. A simple solution would be change the final search and replace so instead of out <- gsub("\u{A0}", " ", out, useBytes = TRUE) it becomes out <- gsub("\u{A0}=\u{A0}", "=", out, useBytes = TRUE), so it would replace the non breaking spaces that roxygen adds, but not the ones that were present before.

However I don't know if \u{A0} and other UTF-8 characters are still problematic on some Windows locales or not... maybe with the recent changes in Windows support many of these workarounds can now be simplified...

Any help will be very much appreciated 🙏

@zeehio
Copy link
Author

zeehio commented May 2, 2022

I think I understood the code and provided a fix on #1343 that includes a unit test. I'll be very happy to get feedback on it.

@hadley hadley added bug an unexpected problem or unintended behavior rd ✍️ labels Jun 24, 2022
@hadley hadley added this to the 7.2.1 milestone Jun 24, 2022
hadley added a commit that referenced this issue Jul 7, 2022
hadley added a commit that referenced this issue Jul 7, 2022
hadley added a commit that referenced this issue Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior rd ✍️
Projects
None yet
2 participants