r-quantities / units

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

Variables names with rare characters are mishandled by make_unit_label #349

Closed Ralayax closed 8 months ago

Ralayax commented 1 year ago

Variables names with rare characters used in french plots are mishandled by make_unit_label

To reproduce

library(tidyverse)
library(units)

# Traceback
options(error = function() traceback(3)) 

# Variable name with single quote inside
a=tibble(`Volume à l'inclusion` = set_units(1:100, mL)) 

a |>
  ggplot(aes(x = `Volume à l'inclusion`)) +
  geom_density()

Apparently make_unit_label doesn't remove the quote, only whitespaces, maybe this line should be updated to remove single quotes, double quotes and apostrophes (and maybe other characters, but these definetely produce errors).

https://github.com/r-quantities/units/blob/626a41d8443fffe75bf7ab5b8cc83b0786fb43c6/R/plot.R#L33

I'm not quite sure what the function exactly does with the lab string so this might be the wrong course of action.

Have a nice day

Enchufa2 commented 1 year ago

Thanks for the report, we'll take a look.

elipousson commented 8 months ago

I'm unsure if this is a related issue or a separate issue (or the same issue as #289) but it looks like units allows you to install a unit with an emoji as the unit symbol but won't support conversion to that new unit regardless of whether you use the unit symbol or name.

library(units)
#> udunits database from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/units/share/udunits/udunits2.xml

kilometer <- as_units(1, "km")

install_unit(
  "SL",
  "305 feet",
  "StatueOfLiberty"
)

set_units(
  kilometer,
  "StatueOfLiberty",
  mode = "standard"
)
#> 10.75685 [SL]

remove_unit(
  symbol = "SL",
  name = "StatueOfLiberty"
)

install_unit(
  symbol = "🗽",
  def = "305 feet",
  name = "StatueOfLiberty"
)

set_units(
  kilometer,
  "🗽",
  mode = "standard"
)
#> Error: In '🗽', '🗽' is not recognized by udunits.
#> 
#> See a table of valid unit symbols and names with valid_udunits().
#> Custom user-defined units can be added with install_unit().
#> 
#> See a table of valid unit prefixes with valid_udunits_prefixes().
#> Prefixes will automatically work with any user-defined unit.

set_units(
  kilometer,
  "StatueOfLiberty",
  mode = "standard"
)
#> Error: cannot convert km into 🗽

Created on 2023-10-20 with reprex v2.0.2

Enchufa2 commented 8 months ago

@elipousson This is the same as #289, different from this one.

Enchufa2 commented 8 months ago

@Ralayax Did you test this with ggplot2 only? I see:

library(ggplot2)

data.frame(`Volume à l'inclusion` = 1:100)  |>
  ggplot(aes(x = `Volume à l'inclusion`)) +
  geom_density()
#> Error in `geom_density()`:
#> ! Problem while computing aesthetics.
#> ℹ Error occurred in the 1st layer.
#> Caused by error:
#> ! object 'Volume à l'inclusion' not found
Enchufa2 commented 8 months ago

Closing here, please feel free to reopen if the issue persists when this is fixed in ggplot2.