How to maintain defaults in a function when using the pipe in R?

一笑奈何 提交于 2019-12-24 12:03:42

问题


I feel like this should be a really easy task, but I can't seem to find the answer online. I simply want to do something like this:

stringr::str_interp("x <- ${rnorm(1)}") %>% parse(text = .) %>% eval()

But that doesn't work; when I call x it tells me it cannot be found. I know it's a valid series of functions because this works:

eval(parse(text = stringr::str_interp("x <- ${rnorm(1)}")))

Any idea how to achieve this? Thanks!

NOTE: I'm using the github version of stringr, which is where the str_interp function comes from.


回答1:


There are a couple of problems here. First, nested functions get evaluated from the inside out, so it should be

str_interp --> parse --> eval

since parse() goes inside eval(). String it, parse it, evaluate it. Next, you will need to evaluate it in an environment other than the default environment of eval(), which is the parent frame. I chose the global environment.

library(magrittr)
str_interp("x <- ${rnorm(1)}") %>% parse(text = .) %>% eval(globalenv())
x
# [1] 0.1542613

Note: The necessary functions to reproduce this are in the stringr development version, found here: https://github.com/hadley/stringr/blob/master/R/interp.R



来源:https://stackoverflow.com/questions/33161303/how-to-maintain-defaults-in-a-function-when-using-the-pipe-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!