[英]R equivalent of python star unpacking?
I have a routine in R that takes a variable number of arguments, and I need to call this routine passing an unpacked list.我在 R 中有一个带有可变数量参数的例程,我需要调用这个例程并传递一个解压缩列表。 In python I would do
在python中我会做
lst = [1,2,3]
my_func(*lst)
What is the equivalent syntax in R? R中的等效语法是什么?
To be more specific, I am using shiny and I am trying to create a dynamic list of entries更具体地说,我正在使用闪亮,我正在尝试创建一个动态的条目列表
This is the code.这是代码。
server <- function(input, output) {
applist <- c(
a(href="#", class="list-group-item",
h4(class="list-group-item-heading", "Entry 1"),
p(class="list-group-item-text", "Description")
),
a(href="#", class="list-group-item",
h4(class="list-group-item-heading", "Entry 2"),
p(class="list-group-item-text", "Description")
))
output$applist = renderUI(
div(class="list-group", ... applist)
)
}
Posted by OP.由 OP 发布。
Here is how I solved.这是我解决的方法。 using
do.call
does the trick, but you have to be aware that lists in python is not the same as lists in R. A list in R is more similar to a mix of a dictionary and a list.使用
do.call
可以解决问题,但您必须注意,python 中的列表与 R 中的列表不同。R 中的列表更类似于字典和列表的混合。
applist <- list(
class="list-group",
a(href="#", class="list-group-item"),
a(href="#", class="list-group-item")
)
output$applist = renderUI(do.call(div, applist))
Note how the applist contains both a named entry class
and non-named entries.请注意 applist 如何同时包含命名条目
class
和非命名条目。 As I said, hybrid.正如我所说,混合动力车。 The
do.call
then does the "unpacking", but the content of applist is then distributed as named or non-named argument.然后
do.call
执行“解包”,但 applist 的内容随后作为命名或非命名参数分发。 It works differently from python, where named and non-named are always separated.它与 python 的工作方式不同,其中命名和非命名总是分开的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.