Avoid R function paste generating backslash for quotes -
i trying 2 strings contain quotations ("") combined character/string vector or r function paste can plug result in argument x of writeformula in openxlsx package.
an example this
paste('hyperlink("file)',':///"&path!$c$1&trim(mid(cell("filename",b',sep="")
and hope should produce result this
hyperlink("file:///"&path!$c$1&trim(mid(cell("filename",b
but produces result backslash in front of ":
[1] "hyperlink(\"file):///\"&path!$c$1&trim(mid(cell(\"filename\",b"
i have searched many potential solutions replace paste cat or add noquote function in front of paste output not character vector. functions tostring or as.character convert these results strings backslash comes well.
really appreciate helps this. thanks.
there no backslashes in p
. backslashes see how r displays quote (so know quote part of string , not ending delimiter) not in string itself.
p <- paste0('hyperlink("file)', ':///"&path!$c$1&trim(mid(cell("filename",b') p ## [1] "hyperlink(\"file):///\"&path!$c$1&trim(mid(cell(\"filename\",b" # no backslashes found in p grepl("\\", p, fixed = true) ## [1] false
noquote(p)
, cat(p, "\n")
or writelines(p)
can used display string without backslash escapes:
noquote(p) ## [1] hyperlink("file):///"&path!$c$1&trim(mid(cell("filename",b cat(p, "\n") ## hyperlink("file):///"&path!$c$1&trim(mid(cell("filename",b writelines(p) ## hyperlink("file):///"&path!$c$1&trim(mid(cell("filename",b
one can see individual characters iseparated spaces , there no backslashes:
do.call(cat, c(strsplit(p, ""), "\n")) ## h y p e r l n k ( " f l e ) : / / / " & p t h ! $ c $ 1 & t r m ( m d ( c e l l ( " f l e n m e " , b
as exmaple here p2
contains 1 double quote , has single character in it, not 2:
p2 <- '"' p2 ## [1] "\"" nchar(p2) ## [1] 1
Comments
Post a Comment