R 用相应的单位打印单位编号

问题描述 投票:0回答:1

我有一个使用带有单位的数字参数的脚本。当向日志/用户打印消息时,我希望打印添加相应的单位。 我期望 R 能够足够聪明地这样做:

library(units)

min_size = set_units(1200, "m^2")
print(paste0("Must be at least ", min_size))

# Prints: "Must be at least 1200"
# I want: "Must be at least 1200 m^2")

# ideally without explicitly adding " m^2", to avoid code inconsistency when changing units
print(paste0("Must be at least ", min_size, " m^2"))
r units-of-measurement
1个回答
0
投票

简单的答案是使用

units(min_size)
获取单位的可打印表示。

print(paste0("Must be at least ", min_size, " ", units(min_size)))
© www.soinside.com 2019 - 2024. All rights reserved.