0

I'm trying to pass a set of modified arguments from a larger function to arguments in a nested function. This is an argument supplied from the larger function:

time_dep_covariates_list = c(therapy_start = "Start of Therapy",
                             therapy_end = "End of Therapy")

I have these sets of constant arguments:

    tmerge_args_1 <- alist(data1 = analytic_dataset, 
                     data2 = analytic_dataset, 
                     id = patientid, 
                     tstop = adv_dx_to_event, 
                     death_censor = event(adv_dx_to_event))

And I want to append these modified arguments to that argument list:

   tmerge_args_2 <- lapply(1:length(time_dep_covariates_list), function(x){
            tmerge_args <<- c(tmerge_args, alist('var' = tdc(var)) )
            paste0(names(time_dep_covariates_list[x])," = 
            tdc(",names(time_dep_covariates_list[x]), ")")
            })
 > tdc_args
 [[1]]
 [1] "therapy_start = tdc(therapy_start)"

 [[2]]
 [1] "therapy_end = tdc(therapy_end)"

I want to create a do.call that handles the arguments like so:

count_process_form <- do.call(tmerge, args = c(tmerge_args_1,
                                              tmerge_args_2)

That would be identical to the following:

tmerge(data1 = analytic_dataset, data2 = analytic_dataset,
       id = patientid, tstop = adv_dx_to_event, 
       therapy_start = tdc(therapy_start), therapy_end = tdc(therapy_end)

It works fine with tmerge_args_1 by itself, but as the args_2 are character and not language elements, I get this error:

Error in (function (data1, data2, id, ..., tstart, tstop, options)  : 
all additional argments [sic] must have a name:

How can I modify the list I'm creating for args_2 so they're stored as arguments that do.call can understand? Or am I approaching this all wrong?

Thanks!

Here is a reproducible example:

analytic_dataset= data_frame(patientid = sample(1:1000,5),
                         adv_dx_to_event = sample(100:200, 5),
                         death_censor = sample(0:1,5, replace = T),
                         therapy_start = sample(1:20,5),
                         therapy_stop = sample(40:100,5))

The below would be passed in from a function:

time_dep_covariates_list = c(therapy_start = "Start of Therapy",
                         therapy_end = "End of Therapy")

tmerge_args_1 <- alist(data1 = analytic_dataset, 
                   data2 = analytic_dataset, 
                   id = patientid, 
                   tstop = adv_dx_to_event, 
                   death_censor = event(adv_dx_to_event))
do.call(tmerge,tmerge_args_1) #this works 

tmerge_args_2 <- lapply(1:length(time_dep_covariates_list), function(x){
 tmerge_args <<- c(tmerge_args, alist('var' = tdc(var)) )
  paste0(names(time_dep_covariates_list[x])," = tdc(",names(time_dep_covariates_list[x]), ")")
})

do.call(tmerge,tmerge_args_1,tmerge_args_2) # this doesn't```
  • You're getting into some dicey territory here by using `alist` and quoting as character to stop code from executing. There are ways to operate on the language successfully as you're trying to do, but it's not simple. First of all, step back: What are you trying to do, i.e. what will `do.call` accomplish here? What is your desired result? Can you present it as a runnable (at least to reproduce the error) [minimal, complete, reproducible example](https://stackoverflow.com/a/5963610/4497050)? – alistaire Jun 03 '18 at 23:06
  • 3
    This is an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You are asking for help with a *Y* solution that you think will help your *X* problem but you do not tell us the *X* problem. Please step back and provide a fuller background explaining your overall need of concatenating arguments. Please provide data sample and desired results. – Parfait Jun 03 '18 at 23:42
  • My basic problem is that I need to take an argument from a function I'm creating, do some string changes to each of those elements of the list e.g., from `varname `to `varname2 = tdc(varname)`, and pass that modified list to a set of arguments in another function (tmerge). I'm having trouble identifying a straightforward way to do that... – risfun2018 Jun 04 '18 at 00:01
  • Code should essentially never be strings; it's a sign you're working too hard. You can do this with expressions or quosures, but those are advanced topics. There's almost certainly an easier approach, if you tell us what you're trying to do (*X*) instead of how you're trying to solve *X* (*Y*). – alistaire Jun 04 '18 at 00:05
  • Yeah, it seems like a simple problem that I feel like I'm overthinking - from a vector, `time_dep_covariates_list`, take the variable names, change each element of the vector into the argument that paste0 creates above (e.g. `therapy_start = tdc(therapy_start)`), and pass each of those as arguments in tmerge (in addition to the ones that are in the `alist`) to create a new dataframe. – risfun2018 Jun 04 '18 at 00:14
  • What is `tmerge_args`? – Onyambu Jun 04 '18 at 02:10
  • can you give how `tmerge_args_2` should look like? – Onyambu Jun 04 '18 at 02:11
  • `tmerge_args` are `data1 = analytic_dataset, data2 = analytic_dataset, id = patientid, tstop = adv_dx_to_event, death_censor = event(adv_dx_to_event)` – risfun2018 Jun 04 '18 at 02:18
  • the whole expression should look like `tmerge(data1 = analytic_dataset, data2 = analytic_dataset, id = patientid, tstop = adv_dx_to_event, death_censor = event(adv_dx_to_event), therapy_start = tdc(therapy_start), therapy_end = tdc(therapy_end))` – risfun2018 Jun 04 '18 at 02:19
  • Let me know if my example above makes sense and if you have any other thoughts! – risfun2018 Jun 06 '18 at 02:26

0 Answers0