r - 如何在 R 中获取多个文件而不返回值 ($value) 和可见 ($visible) 跟踪
问题描述
假设我有以下.R
文件:
R/01_script.R
cat("You are now in 01_script.R")
R/01_script.R
cat("You are now in 01_script.R")
我会将它们放入一个单独的调用/文件WORKFLOW.R
中,该文件来自这两个文件:
工作流
source(here::here("R/01_script.R"))
source(here::here("R/02_script.R"))
运行这个,我得到以下打印到控制台:
You are now in 01_script.R
You are now in 02_script.R
如果我尝试将其抽象为一个列表,然后运行sapply
、lapply
或purrr::map
,我会收到更详细的输出:
list_of_scripts <- list(
here::here("R/01_script.R"),
here::here("R/02_script.R")
)
lapply(list_of_scripts, source)
# You are now in 01_script.RYou are now in 02_script.R[[1]]
# [[1]]$value
# NULL
#
# [[1]]$visible
# [1] FALSE
#
#
# [[2]]
# [[2]]$value
# NULL
#
# [[2]]$visible
# [1] FALSE
# ...and similar results with either...
sapply(list_of_scripts, source)
# ...or this...
library(tidyverse)
list_of_scripts %>%
map(source)
我不了解文档,source()
也无法删除详细输出。我尝试了 , , 等的各种组合echo = FALSE
,print.eval = FALSE
但withAutoPrint(print. = FALSE)
控制台以列表结构打印$value
和$visible
变量。
有没有办法抽象调用source()
多个文件并保持“干净”的控制台输出?cat()
如果输出可以自动插入换行符,那就更好了\n
。
解决方案
另一种可能的选择:
file_path = "/path/to/dir"
现在列出您想要获取的所有 R 脚本:
r_scritps_source = list.files(file_path, recursive = T, full.names = T, pattern = ".R")
现在:
invisible(lapply(r_scripts_source, source))
或更紧凑:
invisible(lapply(list.files(file_path, recursive = T, full.names = T, pattern = ".R"), source))
推荐阅读
- laravel - 输入vue Js外键
- python - 在更新对象的方法上运行 Django tests.py
- php - 带有自定义查询分页的 WordPress 自定义分类不工作 - 404 错误
- php - 构建数组路径
- python - 如何使用熊猫获取与其计数匹配的单词
- azure - 使用带有 .NET Core 的服务主体列出所有 Azure Blob
- python - 如何使用 Python 沿着带有矩形边界框的斑点/轮廓的主轴裁剪图像
- javascript - React Native 中的注册问题(关于并发)
- javascript - 大家好, 如何使用一个路由文件中的变量到另一个路由文件?
- java - 如何为 BeforeAll 方法动态传递输入和预期文件名 六月 5