If you want to get into the guts of it, you can , but formatting numbers consistently is a trickier puzzle than you might think. here is an adhoc example where I show we can do arbitrary things to how tibbles print
library(tidyverse)
fun <- function(slice, keys) {
broom::tidy(lm(Petal.Length ~ Sepal.Length, data = slice))
}
x <- iris %>%
group_by(Species) %>%
group_modify(fun)
options(pillar.sigfig = 4)
tb <- as_tibble(x)
library(pillar)
format.pillar_shaft_decimal <- function (x, width, ...)
{
if (length(x$dec$num) == 0L)
return(character())
if (width < pillar:::get_min_width(x)) {
stop("Need at least width ", pillar:::get_min_width(x),
", requested ", width, ".", call. = FALSE)
}
if (width >= pillar:::get_width(x$dec)) {
row <- pillar:::assemble_decimal(x$dec)
row <- stringr::str_replace_all(row," ","X") # why not turn spaces to X's
}
else {
row <- pillar:::assemble_decimal(x$sci)
}
used_width <- pillar:::get_max_extent(row)
row <- paste0(strrep(" ", width - used_width), row)
pillar:::new_ornament(row, width = width, align = "right")
}
assignInNamespace("format.pillar_shaft_decimal", format.pillar_shaft_decimal, ns="pillar")
#throws an error but seems to work anyway
tb %>% mutate_if(is.numeric,~round(.,digits=4))
# A tibble: 6 x 6
Species term estimate std.error statistic p.value
<fct> <chr> <dbl> <dbl> <dbl> <dbl>
1 setosa (Intercept) 0.8031 0.3439 2.335 0.0238
2 setosa Sepal.Length 0.1316 0.0685 1.921 0.0607
3 versicolor (Intercept) 0.1851 0.5142 0.36X 0.7204
4 versicolor Sepal.Length 0.6865 0.0863 7.954 0XXXXX
5 virginica (Intercept) 0.6105 0.4171 1.464 0.1498
6 virginica Sepal.Length 0.7501 0.063X 11.90X 0XXXXX