r-quantities / units

Measurement units for R
https://r-quantities.github.io/units
175 stars 28 forks source link

Display unit information in RStudio environment pane #227

Closed lewinfox closed 4 years ago

lewinfox commented 4 years ago

Hi, I was working with this package (which is great, by the way - thank you) and I wanted to make unit information visible in the RStudio environment pane rather than the default "object of class units".

Per the discussion here I've solved my issue by modifying str.units() in misc.R to incorporate some code from the print.units() method:

str.units <- function(object) {
  gr <- units_options("group")
  unit_string <- paste0(gr[1], as.character(attr(object, "units")), gr[2])
  str_default <- capture.output(str(unclass(object)))
  cat(paste(unit_string, str_default[1], "\n"))
}

Couple of screenshots of RStudio with the new function: Screenshot from 2020-03-02 21-53-50

Screenshot from 2020-03-02 21-55-57

Is this something you'd be interested in including in the package? If so I can submit a PR.

I appreciate not everyone uses RStudio and some may have uses for str() that this overwrites, but I found it useful. Let me know what you think - if you think it's worth exploring I'm happy to tweak it if necessary.

Thanks, Lewin

Enchufa2 commented 4 years ago

Thanks for looking into this. I think that's a very nice feature. Let's see what @edzer thinks.

Enchufa2 commented 4 years ago

Some comments on the implementation:

lewinfox commented 4 years ago

Good feedback, thanks. I'll put something together and see what you think 👍

On Tue, 3 Mar 2020, 02:52 Iñaki Ucar, notifications@github.com wrote:

Some comments on the implementation:

  • The generic is defined as str(object, ...), with dots, so we need to respect that.
  • Use NextMethod() instead of str(unclass(object)).
  • I would add "Units: " to the beginning, as print.units does.
  • A similar method should be implemented for mixed_units.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/r-quantities/units/issues/227?email_source=notifications&email_token=AGC6L2RH65IQ5PEPESUYUBDRFO22HA5CNFSM4K7QUZZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOENPMB4Q#issuecomment-593412338, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGC6L2RM4SBID5SCTMJHX3TRFO22HANCNFSM4K7QUZZA .

lewinfox commented 4 years ago

@Enchufa2, I've made the suggested changes with the exception of replacing str(unclass(object)) with NextMethod().

The rationale here is NextMethod() doesn't allow you to use capture.output(), meaning you can't trim the output (although this SO post gives a workaround). The output of NextMethod() is verbose so I think it would be good to limit it, but I haven't done a performance comparison of the two approaches. Is this something you'd like me to look at?

Below is a screenshot of the new functions at work. The only visual issues I can see are:

Screenshot from 2020-03-03 21-40-21

Here are the changes.

Let me know what you think.

Enchufa2 commented 4 years ago

Thanks, comments inline:

@Enchufa2, I've made the suggested changes with the exception of replacing str(unclass(object)) with NextMethod().

The rationale here is NextMethod() doesn't allow you to use capture.output(), meaning you can't trim the output (although this SO post gives a workaround). The output of NextMethod() is verbose so I think it would be good to limit it, but I haven't done a performance comparison of the two approaches. Is this something you'd like me to look at?

The solution in the SO post is not really a workaround, but how capture.output works: by default, it opens a textConnection, then redirects the output using sink. I believe we should use that mechanism here to be able to call NextMethod, because units should play well with errors and quantities, and avoiding NextMethod makes everything else harder.

Below is a screenshot of the new functions at work. The only visual issues I can see are:

  • The output of str.mixed_units() is clipped in the RStudio viewer - the summary line is removed. Compare the terminal output with the environment pane in the screenshot below to see what I mean.

What happens if you strip out the "List of x" line from the default?

  • There is no whitespace between the colon and the start of the word "Units" in the list and data.frame views. Not sure why this is the case.

That's because str by default inserts a whitespace (try, e.g., str(1:10)). We should do the same.

lewinfox commented 4 years ago

Thanks, that's really useful. I hope to look at this over the weekend.

lewinfox commented 4 years ago

So I've modified things to use NextMethod() as you suggested. I think we're nearly there, but I can't get a standalone mixed_units object to display properly in the environment pane. See screenshot below - it looks great in the console but Rstudio is stil inserting List of n in the environment pane. I've asked here and will see if they can shed any light as I'm stumped!

Screenshot from 2020-03-07 20-16-06

I assume it's something to do with this:

library(units)
mix <- units::mixed_units(1:5, c("s", "m", "cd", "kg", "lb"))

class(mix)
## "mixed_units"

inherits(mix, "list")
## FALSE

is.list(mix)
## TRUE

Current code is here.

Enchufa2 commented 4 years ago

Looks very good, thanks for all the work! You mean the "List of 5" on the right side? I think that's fine. It really is a list of 5, so no problem I'd say. I believe we are ready for a PR. :)