在R和knitr中,我可以抑制readOGR的消息吗?

rad*_*dek 12 r ogr knitr

我正在使用R&knitr构建小型报告,将输出发送到pdf.

我在分析中使用了几个形状文件,每当我使用包的readOGR功能时,rgdal我都会获得有关正在读取的内容的信息,例如:

OGR data source with driver: ESRI Shapefile 
Source: "__PATH_HERE__", layer: "__NAME__OF__LAYER__HERE__"
with 148 features and 5 fields
Feature type: wkbPolygon with 2 dimensions
Run Code Online (Sandbox Code Playgroud)

通常,它是有用的...但不幸的是它也打印出我的pdf输出.

我尝试设置knitr的块选项,echo=FALSE, message=FALSE但不幸的是它没有帮助.

有什么更好的解决方案吗?

Sim*_*lon 25

您是否尝试设置verbose = FALSEreadOGR功能本身?

例如

> dsn <- system.file("vectors", package = "rgdal")[1]
> cities <- readOGR(dsn=dsn, layer="cities")
OGR data source with driver: ESRI Shapefile 
Source: "C:/Users/sohanlon/Dropbox/R/R64_Win_Libs/rgdal/vectors", layer: "cities"
with 606 features and 4 fields
Feature type: wkbPoint with 2 dimensions
# Set verbose = FALSE
> cities <- readOGR(dsn=dsn, layer="cities" , verbose = FALSE)
Run Code Online (Sandbox Code Playgroud)

那么相关的knitr chunk可能是:

```{r, echo=FALSE, message=FALSE}
library(rgdal)
dsn <- system.file("vectors", package = "rgdal")[1]
cities <- readOGR(dsn=dsn, layer="cities", verbose=FALSE)
```
Run Code Online (Sandbox Code Playgroud)

  • 作为参考,在 `sf` 中使用 `quiet = TRUE` (2认同)

A5C*_*2T1 9

"knitr"这样做的方法就是使用results = 'hide'.借用@ SimonO101的示例数据,尝试:

```{r, results='hide', echo=FALSE, message=FALSE}
library(rgdal)
dsn <- system.file("vectors", package = "rgdal")[1]
cities <- readOGR(dsn=dsn, layer="cities")
```
Run Code Online (Sandbox Code Playgroud)