在r中破坏对象,用于编写软件包;像python' **或javascript'
我一直在寻找一种破坏对象的方法。这是为了在开发软件包时编写清洁代码。
我经常有一个复杂的功能系统来称呼他人等,我想让这些参数在更高级别的功能上提供给用户。
在开发r
的软件包时,我最终经常写下调用其他功能的功能,最终必须使用...
运算符(只能使用一次或一次。为了像这样手动重新分配对象:
someFunction1 <- function(arg1, arg2) { print(stringr::str_interp('Do something with ${arg1}, and with ${arg2}')) }
someFunction2 <- function(arg3, arg4) { print(stringr::str_interp('Do something with ${arg1}, and with ${arg2}')) }
# solution with R's ...
someHigherFunction <- function(arg5, arg6, ...) {
# do something with arg5 and 6
someFunction1(...)
}
# otherwise I start having to do this; and the more nested the functions get the harder it is to manage
someHigherFunction <- function(arg5, arg6, arg1, arg2, arg3, arg4) {
# do something with arg5 and 6
someFunction1(arg1 = arg1, arg2 = arg2)
someFunction1(arg3 = arg3, arg4 = arg4)
}
# there is a solution using do.call but it is rather annoying
someHigherFunction <- function(arg5, arg6, arg1_2 = list(arg1 = "something", arg2 = "something else")) {
# do something with arg5 and 6
# not ideal but it works
do.call(someFunction1, arg1_2)
}
在javascript
我们可以使用对象破坏(容易适用于位置参数),Python也可以使用它自己的**
double Star Operator :
function foo(a, b) {
console.log(a - b);
}
let args = [10, 7];
foo(...args);
还有其他用途:
let a, b, rest;
[a, b] = [10, 20];
console.log(a);
// expected output: 10
console.log(b);
// expected output: 20
[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(rest);
// expected output: Array [30,40,50]
有人在R中有任何建议吗?
I have been looking for a way to do object destructuring. This in an effort to write cleaner code when developing packages.
I often have a complex system of functions that call others etc and I want to make those arguments available at the higher level functions to the user.
When developing packages for R
I end up often writing functions that call other functions and end up having to either use the ...
operator (which can only be used once. Or to manually re-assign the object like so:
someFunction1 <- function(arg1, arg2) { print(stringr::str_interp('Do something with ${arg1}, and with ${arg2}')) }
someFunction2 <- function(arg3, arg4) { print(stringr::str_interp('Do something with ${arg1}, and with ${arg2}')) }
# solution with R's ...
someHigherFunction <- function(arg5, arg6, ...) {
# do something with arg5 and 6
someFunction1(...)
}
# otherwise I start having to do this; and the more nested the functions get the harder it is to manage
someHigherFunction <- function(arg5, arg6, arg1, arg2, arg3, arg4) {
# do something with arg5 and 6
someFunction1(arg1 = arg1, arg2 = arg2)
someFunction1(arg3 = arg3, arg4 = arg4)
}
# there is a solution using do.call but it is rather annoying
someHigherFunction <- function(arg5, arg6, arg1_2 = list(arg1 = "something", arg2 = "something else")) {
# do something with arg5 and 6
# not ideal but it works
do.call(someFunction1, arg1_2)
}
In JavaScript
we can use object destructuring (easy for positional arguments), python has this too with it's own **
double star operator:
function foo(a, b) {
console.log(a - b);
}
let args = [10, 7];
foo(...args);
There are other uses for this as well:
let a, b, rest;
[a, b] = [10, 20];
console.log(a);
// expected output: 10
console.log(b);
// expected output: 20
[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(rest);
// expected output: Array [30,40,50]
Does anyone have any suggestions on how to accomplish this in R? Did I miss something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在上面扩展,这种模式是否有用/可推广?
Expanding on above, is this pattern useful/generalizable?