I am attempting to use gsub to remove characters from the following string:
string <- "function(data, x = !!rlang::sym(\"Time1\"), y = !!rlang::sym(\"YVAR\")), values = c(\"a\", \"b\"))"
The new string should return:
cat(string)
function(data, x = Time1, y = YVAR, values = c("a", "b"))
that is to say, I'd like to remove !!rlang::sym(\", keep Time1, and remove the closing quote and parenthesis after Time1 \") (and I'd also like to remove this for YVAR)
Time1 and YVAR (x and y variable names) are arbitrary and can be named anything in the resulting string, however, the characters !!rlang::sym(\" and the closing quote and parenthesis \")after the arbitrary string that needs to be kept are constant and will not change.
I understand I can simply use
result <- gsub("!!rlang::sym(\"", "", string, fixed = TRUE)
then
result <- gsub("\")", "", result, fixed = TRUE)
to get part of the way there, however, I'd like to find a more elegant regex solution that can combine both of these gsub and also will of course not remove the closing "\")" in values = c(\"a\", \"b\"))"