Skip to content

Instantly share code, notes, and snippets.

@strboul
Created June 23, 2018 12:11
Show Gist options
  • Save strboul/2c3666c581a1ea24fd1033545996bfbd to your computer and use it in GitHub Desktop.
Save strboul/2c3666c581a1ea24fd1033545996bfbd to your computer and use it in GitHub Desktop.
evaluate expressions (with a keyword)
#' 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