r - 如何阻止 lapply 打印到控制台?
问题描述
我正在将一堆 shapefile 读入 R 并lapply
用于在一堆文件中运行它。现在,代码工作正常,但它会在控制台中打印很多信息。我试图摆脱它,以便控制台更清洁。
load_shapefiles <- function(file_name){
#grabs the last two digits of the year for the current file
partial_year <- str_sub(file_name, start = 9, end = 10)
#read in files
st_read(dsn = sprintf("data/%s", file_name), layer = sprintf("Census_sum_%s", partial_year))
}
#apply the loading function across a list of file names
list_data <- lapply(list_filenames, function(x) load_shapefiles(x))
代码运行得非常快,但是对于每个文件都会打印出如下信息:
Reading layer 'Census_sum_17' from data source xxxx using driver ESRI Shapefile
Simple feature collection with xxxxx features and xxxxxx fields
geometry type: POLYGON
dimension: XY
bbox: xmin: xxxx ymin: xxxxxx xmax: xxxxxx ymax: xxxxxxx
epsg (SRID): NA
proj4string: xxxxx
注意:我用“xxxx”代替了实际值。
我想阻止它将此信息打印到控制台中。我已经尝试过这里推荐lapply
的包装和函数。两种方法都不起作用。有任何想法吗?lapply
invisible()
解决方案
使用capture.output(load_shapefiles(x))
在invisible(load_shapefiles(x))
没有工作的地方工作。
感谢 Rui Barradas 的回答
编辑:st_read
有一个调用的参数quiet
,它确定它是否会打印有关它正在读取的文件的信息。设置quiet
为 TRUE 消除了将函数包装在capture.output
or中的需要invisible
。
推荐阅读
- visual-studio-code - vscode 防止从特定节点模块自动导入
- kotlin - Kotlin 中的对象和数据类有什么区别?
- ruby-on-rails - 在 Rails 上创建用户的设计不起作用
- vba - VBA在MSWord中的上标和下标之前和之后插入
- reactjs - react js中API空响应的条件渲染
- spring - Google Cloud Pub/Sub API 和 Spring Boot 应用程序存在问题
- android - 嵌套的 LivaData 实现
- linked-list - 计算此函数的时间复杂度
- format - 如何删除前导零并将数字移到整数变量的最左边
- javascript - 不要在框架 NestJS 中通过 e2e 测试