0

How do you replace a "backslash" by nothing in a string ?

I'm doing this :

A = "plot_maker(df,c(\"var1\", \"var2\"))"
gsub("\\\\","11", A)

it's not working

I would like to have something like this at the end :

plot_maker(df,c("var1", "var2"))
Akram H
  • 33
  • 4
  • 1
    there aren't any backslashes in your example, just escaped apostrophes – Mark Aug 22 '23 at 13:50
  • I think you're looking for `eval(parse(text = A))` – Mark Aug 22 '23 at 13:50
  • 2
    Use `cat(A)` to verify that there are no backslashes in your output. R just has to escape special characters when printing the value. Using `cat()` avoid the special escaping. But this is for visual output only. The string itself contains no slashes, it just contains quotes inside quotes. – MrFlick Aug 22 '23 at 13:51

1 Answers1

0

You're confused by the distinction between a string and the way it is displayed by R. Your string doesn't actually have any backslashes in it, but R is adding them when it displays. You can see this with

print(A, quote = FALSE)
## [1] plot_maker(df,c("var1", "var2"))

or

cat(A, "\n")
## plot_maker(df,c("var1", "var2")) 

(the \n is added to print a newline after the string)

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453