英文:
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(".", function() .Last.value, .GlobalEnv)
4
sqrt(.) # 2
If you have a µ
, 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 <- 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) # the actual definition
3 - Use package doubt
We can build a dubious operator that will use .Last.value
:
# remotes::install_github("moodymudskipper/doubt")
library(doubt)
`?+{fun}` <- 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) <- class(summary) <- class(View) <- "magic_function"
`+.magic_function` <- function(fun){ fun(.Last.value)}
4
+sqrt # 2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论