iich
iich

Reputation: 155

Is it possible to nest a tibble passed to rlang::dots_list?

I have a 2x2 tibble x that I want to send to a function f. I use rlang::dots_list() within f to extract all named parameters sent to f as ellipsis (...). The result from rlang::dots_list() is coerced into a tibble data structure.

# MRE
x <- tibble::tibble(
    a = base::sample(base::letters, size = 2),
    b = base::sample(base::letters, size = 2)
  )

f <- function(...){
  rlang::dots_list(...) %>%
    tibble::as_tibble()
}

f(parameter = '1', x_name = x)
# A tibble: 2 x 2
  parameter x_name$a $b   
  <chr>     <chr>    <chr>
1 1         c        r    
2 1         v        g    
# Expected behaviour
f(parameter = '1', x_name = x)
# A tibble: 1 x 2
  parameter x_name$data     
  <chr>     <list>          
1 1         <tibble [2 x 2]>

I reach the expected block above by resetting x to x <- tidyr::nest(x, data = tidyr::everything()).

# Trials
f <- function(...){
  rlang::dots_splice(...) %>%
    tibble::as_tibble()
}

f <- function(...){
  as_tibble(
    !!!rlang::dots_list(...)
  )
}

f <- function(...){
  as_tibble(...)
}

f <- function(...){
  as_tibble(
    !!rlang::dots_list(...)
  )
}

Upvotes: 0

Views: 37

Answers (1)

Abdessabour Mtk
Abdessabour Mtk

Reputation: 3888

Adding to @Edo's comment you could implement his solution using purrr::map_if and check if the object is a data.frame then if it is apply list to it:

f <- function(...){
  rlang::dots_list(...) %>% purrr::map_if(is.data.frame, list) %>% tibble::as_tibble 
}
f(parameter = '1', x_name = x)
# A tibble: 1 x 2
  parameter x_name          
  <chr>     <list>          
1 1         <tibble [2 × 2]>

Upvotes: 1

Related Questions