You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
A public way to redefine a database symbol or name after #449 #451
Thanks for the quick and thorough rework in #449. The atomic install and the alias handling fix everything I reported in #448.
One thing it took away that I was relying on: redefining an id that the database already uses. #449 notes the change ("remove_unit() on a symbol/name from the database used to remove it; now it warns and does nothing"), and I can see why that is the safe default. But it was the only way to give a database id a different meaning for a session, and I can't find another public route now.
Example
In the database gal is a name of the galileo. In my field it always means the US gallon.
library(units)
remove_unit(name="gal")
#> Warning: not installed by install_unit(), left unchanged: 'gal'
install_unit(name="gal", def="1 gallon")
#> Error: name "gal" already maps to existing but different unit
install_unit("gal", "1 gallon") # as a symbol: accepted, but the name still wins
set_units(set_units(1, "gal"), "L")
#> Error: cannot convert gal into L
The same goes for any other database symbol or name a project wants to use differently.
Request
Would you consider a public way to override a database id on purpose? For example an argument such as install_unit(..., override = TRUE) (or remove_unit(..., force = TRUE)), recorded in the new registry so that remove_unit() puts the database's own mapping back afterwards. That would keep the new default, where nothing from the database is touched by accident, and make the deliberate case explicit and reversible.
If there is an intended route I have missed, a pointer would be just as welcome. load_units_xml() with an edited database works, but it replaces the whole unit system and drops everything installed so far, which is heavy for one id.
library(units)
install_unit("gal", "1 gallon", override=TRUE)
#> redefined symbol "gal" (was 0.01 meter·second⁻²)
set_units(set_units(1, gal), L)
#> 3.785412 [L]
remove_unit("gal") # puts the database's own "gal" back
set_units(set_units(1, gal), cm/s^2)
#> 1 [cm/s^2]
How it behaves:
The database mappings that give the id another meaning are set aside, and remove_unit() restores them. It works both as a symbol and as a name (install_unit(name = "gal", def = "1 gallon", override = TRUE)).
Prefixed forms follow the new meaning (e.g., kgal). Existing units objects that use the id are read with the new meaning too, so it's best done at the start of a session.
Two things are still refused, even with override = TRUE:
other ids a new name would shadow: names match regardless of case, so a name KG would also take over kg;
ids already installed with install_unit(): remove them first.
While working on this I also found that install_unit() was silently redefining database units through the other kind of id or a prefix (e.g., a name "m", or a symbol "km"). #452 now refuses those unless you pass override = TRUE.
It'd be great if you could try it on your real use cases, especially any edge cases you have in mind, for example:
several overrides in one session, or in one call;
ids that are a database unit's own symbol or name, not only aliases like gal;
removing and re-installing;
code that worked with the old remove_unit() + install_unit() idiom.
Anything that's refused when you'd expect it to work, or the other way round, would be very useful to hear about.
Thanks, I tried the branch (80ad1b2) on the cases you listed and on my own. What works: several overrides in one session, database symbols and names as ids, install/remove cycles, redefining a unit in terms of its old meaning, and objects created earlier following the new meaning. The old remove_unit() + install_unit() idiom is refused with a clear pointer to override = TRUE.
One bug. When the same id is overridden as both a symbol and a name, removing the symbol first (or both in one call) leaves the database's gal unreadable for the rest of the session:
Removing the name first, or overriding only the symbol or only the name, restores correctly. The same happens with install_unit(c("gal","galn"), "1 gallon", name = c("gal","galname"), override = TRUE) followed by remove_unit(c("gal","galn"), c("gal","galname")).
Cause, as far as I can tell: set_aside() sets aside both the database symbol and the database name for the id and files both under the symbol's entry, so the later name call finds nothing left to record. Removing the symbol first then tries to put the database name back while the new name still holds it (ut_map_name_to_unit() fails and the return value is ignored); removing the name afterwards unmaps the new name, and the database name is lost.
#453 is a fix against fix-install-shadowing: after the set_aside() calls in ud_install(), each set-aside mapping is handed to the installed id of its own kind when there is one, so the name entry restores the name in any removal order. It adds a test for both removal orders, one call, the vector case and the single-kind cases; the suite passes with it here (858 passed).
Two smaller observations, neither a defect in the restore logic. After a symbol-only override of gal, Gal and GAL are unreadable until removal, because they are only reached through the case-insensitive name that is set aside; that looks intended but may deserve a line in the docs. And remove_unit("gal", "gal") after a symbol-only override restores everything but warns "left unchanged" for the name half, which is harmless but a little misleading.
Thanks for merging #452, and for folding in the fix from #453.
I've tested install_unit(..., override = TRUE) against main (d01bffe). Redefining gal, mph, nmi, pH, Ma and ka and removing them again all works. I also re-ran the cases from #448, #451 and #453. Refused installs leave nothing mapped, and degree and ohm keep their symbols. The database gal comes back in every removal order, including when the symbol and the name are overridden in separate calls.
One small thing I noticed: an alias of a unit that has no symbol becomes that unit's symbol while it is installed, so the original prints differently:
Thanks. This is not new: gallon is a name, there is no symbol. If you install a symbol, the symbol wins unless you set the option auto_convert_names_to_symbols=FALSE.
Activity
Enchufa2 commentedon Nov 13, 2026
I see. I'm not a fan of redefining the default units, but let me think about it.
Enchufa2 commentedon Nov 14, 2026
@vorpalvorpal, #452 adds an explicit way to do this:
install_unit(..., override = TRUE). Could you give it a try?With your example:
How it behaves:
remove_unit()restores them. It works both as a symbol and as a name (install_unit(name = "gal", def = "1 gallon", override = TRUE)).kgal). Existingunitsobjects that use the id are read with the new meaning too, so it's best done at the start of a session.override = TRUE:KGwould also take overkg;install_unit(): remove them first.While working on this I also found that
install_unit()was silently redefining database units through the other kind of id or a prefix (e.g., a name"m", or a symbol"km"). #452 now refuses those unless you passoverride = TRUE.It'd be great if you could try it on your real use cases, especially any edge cases you have in mind, for example:
gal;remove_unit()+install_unit()idiom.Anything that's refused when you'd expect it to work, or the other way round, would be very useful to hear about.
vorpalvorpal commentedon Nov 14, 2026
Thanks, I tried the branch (80ad1b2) on the cases you listed and on my own. What works: several overrides in one session, database symbols and names as ids, install/remove cycles, redefining a unit in terms of its old meaning, and objects created earlier following the new meaning. The old
remove_unit()+install_unit()idiom is refused with a clear pointer tooverride = TRUE.One bug. When the same id is overridden as both a symbol and a name, removing the symbol first (or both in one call) leaves the database's
galunreadable for the rest of the session:Removing the name first, or overriding only the symbol or only the name, restores correctly. The same happens with
install_unit(c("gal","galn"), "1 gallon", name = c("gal","galname"), override = TRUE)followed byremove_unit(c("gal","galn"), c("gal","galname")).Cause, as far as I can tell:
set_aside()sets aside both the database symbol and the database name for the id and files both under the symbol's entry, so the later name call finds nothing left to record. Removing the symbol first then tries to put the database name back while the new name still holds it (ut_map_name_to_unit()fails and the return value is ignored); removing the name afterwards unmaps the new name, and the database name is lost.#453 is a fix against
fix-install-shadowing: after theset_aside()calls inud_install(), each set-aside mapping is handed to the installed id of its own kind when there is one, so the name entry restores the name in any removal order. It adds a test for both removal orders, one call, the vector case and the single-kind cases; the suite passes with it here (858 passed).Two smaller observations, neither a defect in the restore logic. After a symbol-only override of
gal,GalandGALare unreadable until removal, because they are only reached through the case-insensitive name that is set aside; that looks intended but may deserve a line in the docs. Andremove_unit("gal", "gal")after a symbol-only override restores everything but warns "left unchanged" for the name half, which is harmless but a little misleading.Share what an override set aside between both kinds of the same id
Enchufa2 commentedon Nov 16, 2026
Take another go, please, and I'll merge it if everything looks good.
Refuse shadowing ids in install_unit(), and add override to redefine …
Enchufa2 commentedon Nov 18, 2026
Merging, thanks again. Please let us know if you find anything else.
vorpalvorpal commentedon Nov 25, 2026
Thanks for merging #452, and for folding in the fix from #453.
I've tested
install_unit(..., override = TRUE)againstmain(d01bffe). Redefininggal,mph,nmi,pH,Maandkaand removing them again all works. I also re-ran the cases from #448, #451 and #453. Refused installs leave nothing mapped, anddegreeandohmkeep their symbols. The databasegalcomes back in every removal order, including when the symbol and the name are overridden in separate calls.One small thing I noticed: an alias of a unit that has no symbol becomes that unit's symbol while it is installed, so the original prints differently:
A unit that has a symbol (
degree,s) keeps it. If this is intended, a line ininstall_unit()'s docs might help.Enchufa2 commentedon Nov 25, 2026
Thanks. This is not new:
gallonis a name, there is no symbol. If you install a symbol, the symbol wins unless you set the optionauto_convert_names_to_symbols=FALSE.