Created
June 23, 2018 12:11
-
-
Save strboul/2c3666c581a1ea24fd1033545996bfbd to your computer and use it in GitHub Desktop.
evaluate expressions (with a keyword)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Convert strings to R objects | |
#' | |
#' @param x a string | |
#' @details It is particularly designed for the YAML strings that can be | |
#' converted into R code. Strings to be evaluated should start with *`'r'`* | |
#' letter and be surrounded with backticks. If this is not the case, it will | |
#' return string as is. It is advised to keep all string in double quotes, | |
#' otherwise unquoted strings will be converted by the yaml reader. | |
#' @noRd | |
eval_string <- function(x) { | |
# beginning pattern: | |
if (grepl("^(`r\\s+)", x)) { | |
# remove backtick, r and space beginning and backtick at the end: | |
eval(parse(text = gsub("(^`r\\s+)|(`$)", "", x))) | |
} else { | |
# return string unevaluated: | |
x | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment