I am wondering why only in some instances R produces the output of the data frame/tibble on the console after piping it into a function. Below is a reproducible example.
I first define a custom function called make_stars()
because I would like to see stars associated with significance in my regression output.
library(data.table)
library(dplyr)
library(broom)
library(fixest)
# Define custom function to create stars within the broom output
make_stars <- function(tidy_DT) {
data.table::setDT(tidy_DT)
tidy_DT <- tidy_DT %>%
.[p.value <= 0.01, stars := "***"] %>%
.[p.value > 0.01 & p.value <= 0.05, stars := "**"] %>%
.[p.value >0.05 & p.value <= 0.10, stars := "*"]
return(tidy_DT)
}
Now, to show exactly what I mean, notice that in the first case, I pipe the OLS_model
object only into tidy
and the result appears on the Console and after the RMarkdown chunk. However, this does not happen if I pipe OLS_model
into both tidy
and make_stars
.
DT <- iris
OLS_model <-
DT %>% # Pipes in a dataset into a function
feols(
Sepal.Length ~ Sepal.Width + Petal.Length,
data = . # Puts the . in here to indicate where the data.table is going
)
# Output is displayed
OLS_model %>% tidy
# Output is NOT displayed
OLS_model %>% tidy %>% make_stars
Note that I don't care that my output is not a tibble. I prefer using a data.table anyways.
Thanks!