Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange output and content after as.data.frame() coercion #275

Closed
fweber144 opened this issue Mar 2, 2023 · 5 comments · Fixed by #276 or #295
Closed

Strange output and content after as.data.frame() coercion #275

fweber144 opened this issue Mar 2, 2023 · 5 comments · Fixed by #276 or #295

Comments

@fweber144
Copy link

With posterior 1.4.0, I get strange results when printing an object (e.g., a summary) that I applied as.data.frame() to:

library(posterior)
draws_mat <- as_draws_matrix(example_draws())
q_tails <- function(x) quantile(x, probs = c(0.025, 0.975))
smmry <- summarize_draws(draws_mat, "median", "mad", "q_tails")
smmry_dataframe <- as.data.frame(smmry)
print(smmry_dataframe)

gives

   variable median  mad                                                    2.5% 97.5%
1        mu   4.16 3.57  -\033[31m2\033[39m\033[31m.\033[39m\033[31m16\033[39m   10.2
2       tau   3.07 2.89                                                   0.174  14.6
3  theta[1]   5.97 4.87  -\033[31m2\033[39m\033[31m.\033[39m\033[31m81\033[39m   22.7
4  theta[2]   5.13 4.25  -\033[31m3\033[39m\033[31m.\033[39m\033[31m24\033[39m   14.9
5  theta[3]   3.99 4.94 -\033[31m15\033[39m\033[31m.\033[39m\033[31m0\033[39m    13.2
6  theta[4]   4.99 4.51  -\033[31m4\033[39m\033[31m.\033[39m\033[31m64\033[39m   14.8
7  theta[5]   3.72 4.38  -\033[31m8\033[39m\033[31m.\033[39m\033[31m68\033[39m   12.3
8  theta[6]   4.14 4.81  -\033[31m6\033[39m\033[31m.\033[39m\033[31m80\033[39m   12.8
9  theta[7]   5.90 4.54  -\033[31m2\033[39m\033[31m.\033[39m\033[31m57\033[39m   18.1
10 theta[8]   4.64 4.89  -\033[31m6\033[39m\033[31m.\033[39m\033[31m53\033[39m   14.8

I don't know if this is actually posterior-related or if it might be related to a recent update in tidyverse / tibble / pillar or anything else. I just encountered it in the context of posterior because I don't use tibbles anywhere else.

@mjskay
Copy link
Collaborator

mjskay commented Mar 2, 2023

Looks like control characters from color formatting. My guess is this is because of this:

for (i in seq_cols(out)) {
if (is.numeric(out[[i]])) {
out[[i]] <- do.call(tibble::num, c(list(out[[i]]), .num_args))
}
}

Probably the "right" thing to do is to instead return a data frame with a subclass here, like c("draws_summary", "data.frame"), and move the formatting step into the print method of that subclass.

@paul-buerkner
Copy link
Collaborator

Yes, that could make sense. @mjskay would you have time to do that or shall I?

@mjskay
Copy link
Collaborator

mjskay commented Mar 3, 2023

I miiigght have time this weekend, but most likely not due to various bits of travel. But if you don't get to it before next week I could do it next week.

@fweber144
Copy link
Author

Thanks! I forgot to mention that not only the print() output is strange, but that it's already the content of the data.frame which seems to be broken. This can be seen from typing str(smmry_dataframe$`2.5%`) after the reprex posted above.

@fweber144 fweber144 changed the title Strange output when printing after as.data.frame() coercion Strange output and content after as.data.frame() coercion Mar 3, 2023
@mjskay
Copy link
Collaborator

mjskay commented Mar 3, 2023

Yeah, that tracks with my reading of the problem - basically the formatting is being applied to the returned data frame instead of being applied at print time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants