在R中对上一个结果应用函数,最少的键入方式是:

huangapple go评论88阅读模式
英文:

Applying a function to the last result in R with minimal typing

问题

Twitter上,我提出了以下问题:

> 在RStudio中,是否有一种简单的方法将存储在.Last.value中的最后结果传递给一个函数。假设我做了以下操作:
>
> tbl <- CreateTable(x)
>
> 现在我想要执行 View(tbl),但要少打字。只需键入
>
> .View
>
> 或类似的东西。这是否可能? #rstats #rstudio

也许有一种通用的方法来实现这个吗?

英文:

On Twitter I asked the following:

> In RStudio, is there an easy way to pipe the last result stored in
> .Last.value into a function. Say I did:
>
> tbl <- CreateTable(x).
>
> I now want to do View(tbl) but with less typing. Just type
>
> .View
>
> or something. Is that possible? #rstats #rstudio

Is there maybe a general way to do this?

答案1

得分: 4

以下是已翻译的内容:

1 - 为 . 创建一个绑定

. 将成为 .Last.Value 的快捷方式,节省了大部分的输入。
magrittr 的管道和函数序列仍然可以使用,purrr 的 lambda 函数也可以。

makeActiveBinding(".", function() .Last.value, .GlobalEnv)
4
sqrt(.) # 2

如果你的键盘上有 µλ 或其他符合要求的特殊字符,最好使用它。

2 - 修改 print.function

我们可以修改 print.function 使其在最后一个值上运行函数。

在这里,我无法使用 .Last.value,因为它是函数本身的值,所以我需要重新执行前一个调用,所以在某些情况下可能会较慢。

print.function <- function(x){
  tf <- tempfile()
  savehistory(tf)
  last_calls <- parse(text=tail(readLines(tf),2))
  if(as.list(last_calls[[2]])[[1]] == quote(print))
    base:::print.function(x) else print(x(eval.parent(last_calls[[1]])))
}
4
sqrt # 2
4
print(sqrt) # 实际定义

3 - 使用 doubt 包

我们可以构建一个可疑的运算符,它将使用 .Last.value

# remotes::install_github("moodymudskipper/doubt")
library(doubt)
`?+{fun}` <- function(fun){ fun(.Last.value)}
4
?+sqrt # 2

4 - 为你关心的函数赋予一个类

并为一元运算符(+-! 都可以)定义一个方法:

class(sqrt) <- class(summary) <- class(View) <- "magic_function"
`+.magic_function` <- function(fun){ fun(.Last.value)}
4
+sqrt # 2
英文:

Here are a few ways, this is all strange stuff so I hope you'll keep it for interactive use, won't put anything in your RProfile and won't share code using those :).

1 - create a binding for .

. will be a shortcut for .Last.Value, saving most of the typing.
magrittr pipes and functional sequences will still work, and so will purrr lambdas.

makeActiveBinding(&quot;.&quot;, function() .Last.value, .GlobalEnv)
4
sqrt(.) # 2

If you have a &#181;, a λ or another compliant special character easily accessible on your keyboard it might be a good idea to use it instead.

2 - hacking print.function

We can hack print.function so it runs the function on the last value.

Here I couldn't use .Last.value because it's the value of the function itself, so I need to re-execute the previous call, so in some cases it might be slow.

print.function &lt;- function(x){
  tf &lt;- tempfile()
  savehistory(tf)
  last_calls &lt;- parse(text=tail(readLines(tf),2))
  if(as.list(last_calls[[2]])[[1]] == quote(print))
    base:::print.function(x) else print(x(eval.parent(last_calls[[1]])))
}
4
sqrt # 2
4
print(sqrt) # the actual definition

3 - Use package doubt

We can build a dubious operator that will use .Last.value :

# remotes::install_github(&quot;moodymudskipper/doubt&quot;)
library(doubt)
`?+{fun}` &lt;- function(fun){ fun(.Last.value)}
4
?+sqrt # 2

4 - give a class to the functions you care about

And define a method for an unary operator (+, -, and ! will all work) :

class(sqrt) &lt;- class(summary) &lt;- class(View) &lt;- &quot;magic_function&quot;
`+.magic_function` &lt;- function(fun){ fun(.Last.value)}
4
+sqrt # 2

huangapple
  • 本文由 发表于 2020年1月3日 19:32:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定