You can try:
v <- c("l$`__a`$`__ID`") library(stringi) stri_replace_all_fixed(v, c('$`', '`'), c('[["', '"]]'), vectorize_all = FALSE)
What gives:
#[1] "l[[\"__a\"]][[\"__ID\"]]"
Note: You see \" in the output, because print() avoids quotation marks when propagating them. You can wrap above in noquote() to see the result without \"
noquote( stri_replace_all_fixed(v, c('$`', '`'), c('[["', '"]]'), vectorize_all = FALSE) )
What gives:
#[1] l[["__a"]][["__ID"]]
If you want to apply this to the whole file, you can try:
writeLines(stri_replace_all_fixed(readLines("script.R"), c('$`', '`'), c('[["', '"]]'), vectorize_all = FALSE), file("new_script.R"))
source share