我有一个带有将某些空间对象写入临时文件的功能的软件包。这会导致我的测试产生警告:NAs introduced by coercion
我无法手动复制。
奇怪的是,警告发生在直线或多边形上,而不是在点上。
这是一个最小的可重现示例:
创建一个虚拟包:usethis::create_package("dummypkg")
创建一个具有以下内容的函数:usethis::use_r("writetmp.R")
:
writetmp <- function(type) {
if (type == "line") {
x <- sp::SpatialLines(
list(sp::Lines(sp::Line(cbind(
c(1, 9), c(8, 2)
)), "Line")), proj4string = sp::CRS("+proj=longlat")
)
} else {
x <- sp::SpatialPoints(
cbind(c(3, 7), c(3, 3)), proj4string = sp::CRS("+proj=longlat")
)
}
x$dummy <- seq.int(length(x))
t_x <- tempfile(file = ".gpkg")
rgdal::writeOGR(
x,
t_x,
layer = "x",
driver = "GPKG"
)
return(t_x)
}
usethis::use_test("writetmp")
,其中包含以下内容:test_that("writetmp writes a line to a temporary file", {
ans <- expect_error(writetmp("line"), NA)
ans <- expect_error(writetmp("point"), NA)
})
library(sp)
library(testthat)
devtools::load_all()
writetmp("line") # OK
writetmp("point") # OK
test()
## Loading dummypkg
## Testing dummypkg
## ✔ | OK F W S | Context
## ✔ | 2 1 | writetmp
## ──────────────────────────────────────────────────────────────────────────
## test-writetmp.R:3: warning: writetmp writes a line to a temporary file
## NAs introduced by coercion
## ────────────────────────────────────────────────────────────────────────────
## ══ Results
## ════════════════════════════════════════════════════════════════════════════## OK: 2
## Failed: 0
## Warnings: 1
## Skipped: 0
我想了解为什么会产生此警告以对其进行修复。