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

Show colour in glimpse() #658

Closed
olivermagnanimous opened this issue Mar 21, 2024 · 3 comments · Fixed by #734
Closed

Show colour in glimpse() #658

olivermagnanimous opened this issue Mar 21, 2024 · 3 comments · Fixed by #734
Labels
help wanted ❤️ we'd love your help!

Comments

@olivermagnanimous
Copy link

I'd love for glimpse() to show colour the same way that tibbles do, to easily notice NAs etc. Currently it can be a bit hard to take in as an undifferentiated wall of text. Perhaps colouring the commas that separate the values in the same dim colour used for the column type would help too.

@krlmlr
Copy link
Member

krlmlr commented Mar 22, 2024

Good idea. Would you like to contribute a PR? What kind of guidance would you need?

@ryanzomorrodi
Copy link
Contributor

ryanzomorrodi commented Jun 22, 2024

I also think it would be a nice feature to have NAs have more color for tables. I can send a PR if you'd like. The coloring of the commas seems like some more intensive work.

My thoughts

format_glimpse.default <- function(x, ...) {
  dims <- dim(x)

  if (!is.null(dims)) {
    dims_out <- paste0(dims, collapse = " x ")
    paste0("<", class(x)[[1]], "[", dims_out, "]>")
  } else {
    out <- format(x, trim = TRUE, justify = "none")
    out[is.na(x)] <- crayon_red(NA)

    out
  }
}

format_glimpse.character <- function(x, ...) {
  out <- encodeString(as.character(x), quote = '"')
  out[is.na(x)] <- crayon_red(NA)

  out
}

format_glimpse.factor <- function(x, ...) {
  if (any(grepl(",", levels(x), fixed = TRUE))) {
    out <- encodeString(as.character(x), quote = '"')
  } else {
    out <- format(x, trim = TRUE, justify = "none")
  }
  out[is.na(x)] <- crayon_red(NA)

  out
}

@moodymudskipper
Copy link

The glimpse() function is used to show conveniently the head of the data, similar to str() on data frames.

The displayed value are separated with comas, for more clarity we wish to color these comas, using the same color used to format the column types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted ❤️ we'd love your help!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants