Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想问一下是否有办法查看隐藏在文本中的变量。
如果我跑
k <- eval(expression(v <- 1))
然后我得到 v 等于 1。
但是如果我有它是如何工作的
k <- "v <- 1"
先感谢您
用于parse(text=k)创建表达式,然后对其求值:
parse(text=k)
eval(parse(text=k)) v # [1] 1
如果您想评估代码,请查看evaland命令。evalq
eval
evalq
如果您只想在<-运算符之前找到任何字符串,我想您可以使用以下内容:
<-
regexpr("(.*)[\\s]*<-", "a <- 1", perl=TRUE)
这将返回变量名称的起始索引,如果没有,则返回 -1。substr如果您只想要变量名,可以使用命令提取它。
substr