difference result between data.frame with tibble

library(tidyverse)

myfun1 <- function(v1) {
  data.frame(
    ID = deparse(substitute(v1))
  )
}
myfun1(v1 = model)
#>      ID
#> 1 model




myfun2 <- function(v1) {
  tibble(
    ID = deparse(substitute(v1))
  )
}
myfun2(v1 = model)
#> # A tibble: 1 × 1
#>   ID   
#>   <chr>
#> 1 v1

Created on 2023-08-30 with reprex v2.0.2


Why is this happening?

Not sure why, but you can work around.

library(tidyverse)

myfun2 <- function(v1) {
  tibble(
    ID = rlang::as_name(enexpr(v1))
  )
}
myfun2(v1 = model)
#> # A tibble: 1 × 1
#>   ID   
#>   <chr>
#> 1 model

Created on 2023-09-01 with reprex v2.0.2

thanks, It's great

library(tidyverse)
library(palmerpenguins)

penguins <- palmerpenguins::penguins %>% drop_na()


extact_summary <- function(model) {
  
  fit <- lm(model, data = penguins)
  rsquared <- summary(fit)$r.squared

  result <- tibble(
    ID         = rlang::as_name(enexpr(model)),
    chisq      = rsquared
  )

  return(result)
  
}



mod01 <- 'body_mass_g ~ bill_length_mm'
mod02 <- 'body_mass_g ~ bill_length_mm + bill_depth_mm'

list(mod01, mod02) %>% 
  map(extact_summary) %>% 
  list_rbind() %>%  
  flextable::flextable() %>% 
  flextable::autofit()

in fact , I hope it's like this
123

Can you try

lst(mod01, mod02) %>% 
  map(extact_summary) %>% 
  list_rbind(names_to = "model_name") %>%
  ...

?

tibble::lst() produces a named list, equivalent to list(mod01 = mod01, mod02 = mod02), and the names are propagated via map() and then processed by list_rbind() . Your original question is unrelated, it would only apply if you called e.g. extract_summary(mod01) directly.

1 Like

Learn a lot from you

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.