RL_Pug
RL_Pug

Reputation: 877

How to wrap R code that has quotes (both "" & '') to be text?

I have the following R code which has some markdown embedded.

  tab_header(    
    title = md(paste0(
      "<b>",
      '<span style="font-size: 11pt;">',input$title_num,'</span>',
      "<br>",
      '<span style="font-size: 11pt;">',input$title_name,'</span>',
      "</b>"))
  ) %>%

My goal is have this entire code chunk be converted to text. Whenever I have done this in the past, I would do something like this.

mytext <- "y=mx+b"

#which prints like this below
print(mytext)
[1] "y=mx+b"

But in this case I have html code where I am using both "" and ''.

Simply wrapping "" or '' around the quote block will not work.

What is the best way to approach this?

I am trying to save this code through a rendertext() in my shiny app.

Desired goal:

mytext <- " tab_header(    
    title = md(paste0(
      "<b>",
      '<span style="font-size: 11pt;">',input$title_num,'</span>',
      "<br>",
      '<span style="font-size: 11pt;">',input$title_name,'</span>',
      "</b>"))
  ) %>% "

print(mytext)

Upvotes: 0

Views: 40

Answers (1)

gaut
gaut

Reputation: 5958

As of R 4.0.0, you can use raw text notation:

mytext <- r"[
tab_header(    
  title = md(paste0(
    "<b>",
    '<span style="font-size: 11pt;">',input$title_num,'</span>',
    "<br>",
    '<span style="font-size: 11pt;">',input$title_name,'</span>',
    "</b>"))
) %>% ]"

mytext
[1] "\ntab_header(    \n  title = md(paste0(\n    \"<b>\",\n    '<span style=\"font-size: 11pt;\">',input$title_num,'</span>',\n    \"<br>\",\n    '<span style=\"font-size: 11pt;\">',input$title_name,'</span>',\n    \"</b>\"))\n) %>% "

Raw character constants are also available using a syntax similar to the one used in C++: r"(...)" with ... any character sequence, except that it must not contain the closing sequence )". The delimiter pairs [] and {} can also be used, and R can be used in place of r.

see ?Quotes and this answer

Upvotes: 2

Related Questions