Skip to content
This repository has been archived by the owner on May 29, 2020. It is now read-only.

Add backticks to attributes that have characters that are illegal in R names (such as dashes) #2

Open
daattali opened this issue Feb 5, 2018 · 1 comment

Comments

@daattali
Copy link

daattali commented Feb 5, 2018

For example:

div("test", data-foo = "bar")

Will fail because the named argument has an illegal R name (containing a dash). I always run into this with data- attributes, but anything similar would cause that error. The fix is to wrap the attribute name in backticks

@DivadNojnarg
Copy link

As Dean mentioned I use the following to handle non-standard attributes:

#' @import XML
#' @importFrom stringr str_detect
makeAttrs <- function(node) {
  attrs <- xmlAttrs(node)
  names(attrs) %>%
    Map(function (name) {
      val <- attrs[[name]]
      if (str_detect(string = name, pattern = "-")) {
        name <- paste0("`", name, "`")
      }
      paste0(name, ' = ', if (val == "") "NA" else paste0('"', val, '"'))
    }, .)
}

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

No branches or pull requests

2 participants