将参数传递给使用替换的函数

发布于 2025-01-20 01:55:10 字数 1407 浏览 2 评论 0原文

我想创建一个小的 reprex 包装器,将输入代码传递给 reprex::reprex()。 由于 reprex 在其 x 参数上使用 substitute() ,我需要以某种方式转义它。

MWE

一个最小的工作示例是这样的,其中 internal_foo() 充当 reprex() 的代理。 预期结果是对 internal_foo(...) 的调用以及对 wrapper(...) 的调用都返回相同的输出。

internal_foo <- function(x) {
  res <- substitute(x)
  res
}

# the expected output! ------
internal_foo({
  a <- 1:10
  b <- rnorm(10)
  plot(a, b)
})
#> {
#>   a <- 1:10
#>   b <- rnorm(10)
#>   plot(a, b)
#> }

wrapper <- function(x = NULL) {
  # pass x to internal_foo in such a way that it has the same output as calling internal_foo directly
  internal_foo(deparse(x))
}
wrapper({
  a <- 1:10
  b <- rnorm(10)
  plot(a, b)
})
#> deparse(x)
# clearly not equal to the expected output ----

由于 Rs 惰性求值,x 并未按照我的预期方式传递给 internal_foo()

我尝试过 eval、deparse 和替代,但我找不到正确的组合。

更复杂的 reprex 示例

实际目标是这个 reprex 包装器。

wrapper2 <- function(x, input = NULL) {
  reprex::reprex(x = x, input = input, venue = "r", html_preview = FALSE)
}
wrapper2({
    a <- 1:10
    b <- rnorm(10)
    plot(a, b)
})

wrapper2 渲染 reprex 并将其复制到剪贴板,但显示未找到 eval 对象 x 错误

I want to create a small wrapper to reprex that passes input code to reprex::reprex().
As reprex uses substitute() on its x argument, I need to somehow escape it.

MWE

A minimal working example is this, where internal_foo() acts as a surrogate for reprex().
The expected result is that both the call to internal_foo(...) as well as to the wrapper(...) return the identical output.

internal_foo <- function(x) {
  res <- substitute(x)
  res
}

# the expected output! ------
internal_foo({
  a <- 1:10
  b <- rnorm(10)
  plot(a, b)
})
#> {
#>   a <- 1:10
#>   b <- rnorm(10)
#>   plot(a, b)
#> }

wrapper <- function(x = NULL) {
  # pass x to internal_foo in such a way that it has the same output as calling internal_foo directly
  internal_foo(deparse(x))
}
wrapper({
  a <- 1:10
  b <- rnorm(10)
  plot(a, b)
})
#> deparse(x)
# clearly not equal to the expected output ----

Due to Rs lazy evaluation, x is not handed trough to internal_foo() the way I intend it to.

I have played around with eval, deparse, and substitute but I cannot find the right combination.

The more complicated reprex example

The actual target is this reprex wrapper.

wrapper2 <- function(x, input = NULL) {
  reprex::reprex(x = x, input = input, venue = "r", html_preview = FALSE)
}
wrapper2({
    a <- 1:10
    b <- rnorm(10)
    plot(a, b)
})

wrapper2 renders the reprex and copies it to clipboard but shows Error in eval object x not found.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独岁月 2025-01-27 01:55:10

您可以做

wrapper2 <- function(..., input = NULL) {
  reprex::reprex(..., 
                 input = input, 
                 venue = "r", 
                 html_preview = FALSE)
}

wrapper2({
  a <- 1:10
  b <- rnorm(10)
  plot(a, b)
})

#> i Rendering reprex...
#> √ Reprex output is on the clipboard.

哪些输出:

a <- 1:10
b <- rnorm(10)
plot(a, b)

#' ![](agile-pike_reprex_files/figure-gfm/unnamed-chunk-2-1.png)

You could do

wrapper2 <- function(..., input = NULL) {
  reprex::reprex(..., 
                 input = input, 
                 venue = "r", 
                 html_preview = FALSE)
}

wrapper2({
  a <- 1:10
  b <- rnorm(10)
  plot(a, b)
})

#> i Rendering reprex...
#> √ Reprex output is on the clipboard.

Which outputs:

a <- 1:10
b <- rnorm(10)
plot(a, b)

#' ![](agile-pike_reprex_files/figure-gfm/unnamed-chunk-2-1.png)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文