Skip to content

Commit 812a765

Browse files
committed
[pugi] provide as.character.pugi_xml
1 parent 92c94ef commit 812a765

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
S3method("+",fmt_txt)
44
S3method("[",wb_data)
55
S3method(as.character,fmt_txt)
6+
S3method(as.character,pugi_xml)
67
S3method(print,fmt_txt)
78
S3method(print,pugi_xml)
89
S3method(xl_open,default)

R/pugixml.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,24 @@ print.pugi_xml <- function(x, indent = " ", raw = FALSE, attr_indent = FALSE, ..
208208
if (raw) cat("\n")
209209
}
210210

211+
#' as.character pugi_xml
212+
#' @method as.character pugi_xml
213+
#' @param x a class pugi_xml object
214+
#' @param ... additional arguments see [print.pugi_xml()]
215+
#' @examples
216+
#' # a pointer
217+
#' x <- read_xml("<a><b/></a>")
218+
#' as.character(x)
219+
#' as.character(x, raw = FALSE)
220+
#' @export
221+
as.character.pugi_xml <- function(x, ...) {
222+
args <- list(...)
223+
indent <- args$indent %||% " "
224+
raw <- args$raw %||% TRUE
225+
attr_indent <- args$attr_indent %||% FALSE
226+
printXPtr(x, indent = indent, raw = raw, attr_indent = attr_indent)
227+
}
228+
211229
#' loads character string to pugixml and returns an externalptr
212230
#' @details
213231
#' might be useful for larger documents where single nodes are shortened

man/as.character.pugi_xml.Rd

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-pugixml.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11

22
test_that("read_xml", {
33

4+
xml <- "<a><b/></a>"
5+
expect_equal(as.character(read_xml(xml)), xml)
6+
47
exp <- "<a/>"
58
got <- read_xml("<a/>", empty_tags = FALSE, pointer = FALSE)
69
expect_equal(got, exp)

0 commit comments

Comments
 (0)