Binding arguments for a function call together correctly

Hello,

I have this toy example below which I hope can explain what I want to do. This relates to a problem in shiny where I have a set of arguments captured in a list object and I need to append. It also relates to a do.call with a function across my lists which are the arguments themselves. I think something like the below is happening.

How do I correctly bind e to listb to have the same properties as listc ?

dummy <- function(w,x,y,z,...) {
  w + x / z * y + ... 
  
}


a <- sample(1:100,5,replace = FALSE)
b <- sample(1:100,5,replace = FALSE)
c <- sample(1:100,5,replace = FALSE)
d <- sample(1:100,5,replace = FALSE)
e <- sample(1:100,5,replace = FALSE)


lista <- list(a,b,c,d)

listb <- list(lista,e)

listc <- list(a,b,c,d,e)

dummy(a,b,c,d,e) == do.call(dummy,list(a,b,c,d,e)) 
#> [1] TRUE TRUE TRUE TRUE TRUE


dummy(a,b,c,d,e) == do.call(dummy,listb)
#> Error in (function (w, x, y, z, ...) : argument "z" is missing, with no default

dummy(a,b,c,d,e) == do.call(dummy,listc)
#> [1] TRUE TRUE TRUE TRUE TRUE

Created on 2021-03-03 by the reprex package (v0.3.0)

Hi,

Is this what you are looking for?

dummy <- function(w,x,y,z,...) {
  w + x / z * y + ... 
  
}

a <- sample(1:100,5,replace = FALSE)
b <- sample(1:100,5,replace = FALSE)
c <- sample(1:100,5,replace = FALSE)
d <- sample(1:100,5,replace = FALSE)
e <- sample(1:100,5,replace = FALSE)

lista <- list(a,b,c,d)
listb <- lista
listb[[length(listb) + 1]] = e

dummy(a,b,c,d,e) == do.call(dummy,listb)
[1] TRUE TRUE TRUE TRUE TRUE

Or if you immediately want to append to lista

dummy <- function(w,x,y,z,...) {
  w + x / z * y + ... 
  
}

a <- sample(1:100,5,replace = FALSE)
b <- sample(1:100,5,replace = FALSE)
c <- sample(1:100,5,replace = FALSE)
d <- sample(1:100,5,replace = FALSE)
e <- sample(1:100,5,replace = FALSE)

lista <- list(a,b,c,d)
lista[[length(lista) + 1]] = e

dummy(a,b,c,d,e) == do.call(dummy,lista)
[1] TRUE TRUE TRUE TRUE TRUE

Hope this helps,
PJ

1 Like

Thank you :slight_smile: It is an effective solution. Is there no other prettier way of doing it?

package rlist has function list.append()

library(rlist)
all.equal(target =list.append(lista,e),
          current = listc)
#[1] TRUE
1 Like

Or, you can use just base R:

append(lista, list(e))
2 Likes

This is overall probably the best.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.