0

I need to pass the name of a list as a string to a mutate column as part of a custom function. For e.g.:

a_list = c('a','b','c')

test = function(list_name) {
  map_df(list_name, function(x) {
    benchmark(x = paste(x,'test')) %>% 
      mutate(test = x,
             list_name_as_string = deparse(substitute(list_name)))
  })
}

test(a_list)

gives:

  test replications elapsed relative user.self sys.self user.child sys.child list_name_as_string
1    a          100       0       NA         0        0         NA        NA           list_name
2    b          100       0       NA         0        0         NA        NA           list_name
3    c          100       0       NA         0        0         NA        NA           list_name

I want the last column to be "a_list" repeated.

THanks

2
  • 1
    You call a_list a list, but it is a vector, not a list(). Being careful with that terminology can help avoid headaches later. Commented May 20, 2021 at 18:52
  • Yes thanks. In my real world problem it is a list; I should have been more clear here. Commented May 21, 2021 at 20:19

2 Answers 2

1

Try the evaluation before the assignment:

a_list = c('a','b','c')

test <- function(list_name) {
  j <- deparse(substitute(list_name))
  
  map_df(list_name, ~ benchmark(x = paste(.x, 'test')) %>%
           mutate(test = .x,
                  list_name_as_string = j))
}

test(a_list)

Or use map2_df:

test <- function(list_name){
  map2_df(list_name, deparse(substitute(list_name)), ~
            benchmark(x = paste(.x,'test')) %>%
            mutate(test = .x,
                   list_name_as_string = .y)
  )
}

Output

  test replications elapsed relative user.self sys.self user.child sys.child
1    a          100       0       NA         0        0         NA        NA
2    b          100       0       NA         0        0         NA        NA
3    c          100       0       NA         0        0         NA        NA
  list_name_as_string
1              a_list
2              a_list
3              a_list
Sign up to request clarification or add additional context in comments.

Comments

1

As we are using tidyverse, we can also use the syntax

test <- function(list_name) {
  str1 <- rlang::expr_text(rlang::enexpr(list_name))
 purrr::map_df(list_name, function(x) {
   rbenchmark::benchmark(x = paste(x,'test')) %>% 
     dplyr::mutate(test = x,
            list_name_as_string = str1)
 })
  
}

-testing

test(a_list)
#    test replications elapsed relative user.self sys.self user.child sys.child list_name_as_string
#1    a          100   0.001        1     0.001        0          0         0              a_list
#2    b          100   0.001        1     0.001        0          0         0              a_list
#3    c          100   0.001        1     0.001        0          0         0              a_list

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.