Home > Software engineering >  Forcing R to apply a function or call a variable inside an argument of set_args function
Forcing R to apply a function or call a variable inside an argument of set_args function

Time:12-17

While I am trying to use indexes for extracting an element of a vector or calling a function inside the set_args function of the parsnip library;

library(parsnip)
tree_numbers  = c(500, 1000)
boost_tree() %>% set_args(tree_depth = tree_numbers[1])

I am facing such a situation and the result of the above code is;

Boosted Tree Model Specification (unknown)

Main Arguments:
  tree_depth = tree_numbers[1]

Instead of calling the result of tree_numbers[1] which is 500, the function directly takes tree_depth = tree_numbers[1] as a string. For any other functions' argument with the same logic, I can call or extract the 500 value. It seems like whatever it is typed inside set_args, it is directly taken as strings and the function decodes it afterward.

What is the solution to such a situation to get 500 or 1000 which are the elements of tree_numbers vector inside the set_args function?

CodePudding user response:

You can splice the argument using the bang bang operator !! :

library(parsnip)

tree_numbers  = c(500, 1000)
boost_tree() %>% set_args(tree_depth = !! tree_numbers[1])

#> Boosted Tree Model Specification (unknown)
#> 
#> Main Arguments:
#>   tree_depth = 500
#> 
#> Computational engine: xgboost

Created on 2021-12-17 by the reprex package (v2.0.1)

  • Related