Search code examples
rvector

Function to build a vector with dynamic substring in items


I'm trying to build a function that allows me to dynamically make a vector called independent_vars. This is my attempt so far:

independent_vars_fcn <- function(.x){

    substring <- enquo(.x) 

    Hmisc::Cs(
        paste0("size_", !! substring, "_s"),
        paste0("color_", !! substring, "_s"),
        paste0("shape_", !! substring, "_s"),
        paste0("softness_", !! substring, "_s"),
        paste0("brightness_", !! substring, "_s"),
        paste0("comfort_", !! substring, "_s"))
    
}

However, when I try to call this function it is not evaluating substring. E.g.

independent_vars_fcn("office")
'paste0("size_", !!substring, "_s")''paste0("color_", !!substring, "_s")''paste0("shape_", !!substring, "_s")''paste0("softness_", !!substring, "_s")''paste0("brightness_", !!substring, "_s")''paste0("comfort_", !!substring, "_s")'

Where I actually want it to look like this:

'size_office_s', 'color_office_s', 'shape_office_s', 'softness_office_s', 'brightness_office_s', 'comfort_office_s'

Note i'm using Hmisc::Cs() instead of regular c() because I want to pass the vector into reformulate to build an equation and that requires each item in the vector is wrapped in quotes. Because I have many substrings it'd be good to be able to make the independent_vars_fcn once and then feed it into reformulate like so:

reformulate(c(independent_vars_fcn("office")), "dependent_var_example_1")
reformulate(c(independent_vars_fcn("kitchen")), "dependent_var_example_2")
etc...

I assume i'm getting the tidy evaluation wrong somehow. Can someone see where i'm going wrong here?

I've also tried this if helpful, but it produces the same output:

independent_vars_fcn <- function(.x){

    Hmisc::Cs(
        paste0("size_", .x, "_s"),
        paste0("color_", .x, "_s"),
        paste0("shape_", .x, "_s"),
        paste0("softness_", .x, "_s"),
        paste0("brightness_", .x, "_s"),
        paste0("comfort_", .x, "_s"))
    
}

Solution

  • You can use Hmisc as @akrun has shown. However, I wanted to respond to your comment about needing to use it. I think not necessary. Just use c() in your function.

    independent_vars_fcn <- function(.x) {
      c(
        paste0("size_", .x, "_s"),
        paste0("color_", .x, "_s"),
        paste0("shape_", .x, "_s"),
        paste0("softness_", .x, "_s"),
        paste0("brightness_", .x, "_s"),
        paste0("comfort_", .x, "_s")
      )
    }
    
    reformulate(independent_vars_fcn("office"),"dependent_var_example_1")
    

    Output:

    dependent_var_example_1 ~ size_office_s + color_office_s + shape_office_s + 
        softness_office_s + brightness_office_s + comfort_office_s
    

    Its probably better to put a parameter in the function so that you can more easily change the independent variables, like this:

    independent_vars_fcn <- function(.x, keys = c("size", "color", "shape", "softness", "brightness", "comfort")) {
      sapply(keys, \(k) paste0(k,"_", .x, "_s"))
    }
    
    reformulate(independent_vars_fcn("office"),"dependent_var_example_1")
    

    Output:

    dependent_var_example_1 ~ size_office_s + color_office_s + shape_office_s + 
        softness_office_s + brightness_office_s + comfort_office_s