tmp <- withr::local_tempfile(lines = "c('a', 'b')")
expr_as_xml <- get_source_expressions(tmp)$expressions[[1L]]$xml_parsed_content
writeLines(as.character(expr_as_xml))
get_r_string(expr_as_xml, "expr[2]") # "a"
get_r_string(expr_as_xml, "expr[3]") # "b"
# more importantly, extract strings under R>=4 raw strings
tmp4.0 <- withr::local_tempfile(lines = "c(R'(a\\b)', R'--[a\\\"\'\"\\b]--')")
expr_as_xml4.0 <- get_source_expressions(tmp4.0)$expressions[[1L]]$xml_parsed_content
writeLines(as.character(expr_as_xml4.0))
get_r_string(expr_as_xml4.0, "expr[2]") # "a\b"
get_r_string(expr_as_xml4.0, "expr[3]") # "a\\"'\"\b"
Run the code above in your browser using DataLab