Skip to content

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

Closed
@zeehio

Description

@zeehio

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 🙏

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorrd ✍️

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions