无法重现涉及sp :: writeOGR的测试警告:“强制引入的NA”

时间:2019-06-17 10:03:21

标签: r tidyverse testthat

我有一个带有将某些空间对象写入临时文件的功能的软件包。这会导致我的测试产生警告:NAs introduced by coercion我无法手动复制。

奇怪的是,警告发生在直线或多边形上,而不是在点上。

这是一个最小的可重现示例:

  1. 创建一个虚拟包:usethis::create_package("dummypkg")

  2. 创建一个具有以下内容的函数: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)
}
  1. 创建测试: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)
})
  1. 检查调用函数是否按预期工作,没有任何警告,但在测试中,它会针对“线”类型产生警告:
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

我想了解为什么会产生此警告以对其进行修复。

0 个答案:

没有答案