
91
expect_floor_equal("year", "2008-01-01 00:00:00")
#>
: floor_date(base, unit) not equal to as.POSIXct(time, tz = "UTC")
#> Mean absolute difference: 31622400
このような場合に、標準的なやり方からはやや外れる関数(
http://adv-r.had.co.nz/Computing-
on-the-language.html
)ですが、わかりやすいメッセージを出力する方法があります。そのために
使用するのが、
bquote()
と
eval()
です。以下の
bquote()
を呼び出す際に、
.(x)
を使用している
点に注意してください。
()
の中身が呼び出し時に挿入されます。
expect_floor_equal <- function(unit, time) {
as_time <- function(x) as.POSIXct(x, tz = "UTC")
eval(bquote(expect_equal(floor_date(base, .(unit)), as_time(.(time)))))
}
expect_floor_equal("year", "2008-01-01 00:00:00")
#>
: floor_date(base, "year") not equal to as_time("2008-01-01 ...