在我继续寻求避免在一些简单命令中使用括号时,我编写了以下运算符来创建一个新的图形窗口.我的问题是:除了明显无法在我的变量"newdev"上执行"not"函数之外,我是否有可能"破坏"R中的任何内容?
# function to overload "!" for one purpose only
#this is adapted from the sos package code for "???", credited to Duncan Murdoch.
# Example of how to create a specialized unary operator that doesn't require
# parentheses for its argument. So far as I can tell,
#the only way to do this is to overload an existing function or
# operator which doesn't require parentheses. "?" and "!" meet this requirement.
`!` <- function (e1, e2) {
call …Run Code Online (Sandbox Code Playgroud) 如何在R中实现F#的前向管道运算符?操作员可以轻松地链接一系列计算.例如,当你有一个输入data,想调用函数foo和bar顺序,你可以这样写:
data |> foo |> bar
Run Code Online (Sandbox Code Playgroud)
而不是写作bar(foo(data)).好处是您可以避免使用某些括号,并且计算的执行顺序与执行顺序相同(从左到右).在F#中,运算符定义如下:
let (|>) a f = f a
Run Code Online (Sandbox Code Playgroud)
看起来%...%可以用于二元运算符,但是它如何工作?
有没有办法在R中编写流水线函数,其中一个函数的结果立即传递到下一个函数?我来自F#并且非常欣赏这种能力,但还没有找到如何在R中做到这一点.它应该很简单,但我找不到如何.在F#中它看起来像这样:
let complexFunction x =
x |> square
|> add 5
|> toString
Run Code Online (Sandbox Code Playgroud)
在这种情况下,输入将被平方,然后添加5,然后转换为字符串.我希望能够在R中做类似的事情,但不知道如何做.我已经搜索了如何做这样的事情,但没有遇到任何问题.我想要导入数据,因为我通常必须导入它然后过滤.现在我在多个步骤中执行此操作,并且非常希望能够以F#管道方式执行某些操作.