rmermaiddiagrammer

Replace double quotation to single quotation in R


I'm trying to use https://mermaid-js.github.io/mermaid/#/ to output some texts to a flowchart. This is the syntax I need to follow, which is a single quotation outside of a double quotation. For example,

library(DiagrammeR)
mermaid('graph TB \n Trial_A["<b>Trial_A </b> 
        <br/> (BGL + PCD vars)
        <br/> (XAL)
        <br/> (RAX)
        <br/> (NOW)"] 
        \n style Trial_A fill:#FCFBFD \n ')

So I'm trying to combine a list of text using paste0, but it's always a double quotation outside:

textn <- c("This is a sentence [<b>]", '"a (panda bear)"')    
textn2 <- paste0(textn, collapse=" \n ")
textn2
"This is a sentence [<b>] \n \"a (panda bear)\""

I want

'This is a sentence [<b>] \n \"a (panda bear)\"'

Are there any ways I can do this? I tried gsub("\"","\'", textn2), it only changed the double quotation inside.


Solution

  • The noquote() is what you are after.

    1. See the input and output:

    Run:
    noquote("Your text")

    You get:
    Your text

    2. Add single quotation marks:

    Run:
    paste0("'", noquote("Your text"), "'")

    You get:
    "'Your text'"

    3. Re-use the noquote function again (Sorry about the nested function - it's not pretty but it works):

    Run:
    noquote(paste0("'", noquote("Your text"), "'"))

    You then get what you want:
    'Your text'