我认为诀窍是使用open = "w"
打开进行写入。这似乎对我来说非常有效:
file.opened <- function(path) {
suppressWarnings(
"try-error" %in% class(
try(file(path,
open = "w"),
silent = TRUE
)
)
)
}
Run Code Online (Sandbox Code Playgroud)
打开 Excel 文件后:
file.opened("C:\\Folder\\tbl1.xlsx")
# TRUE
Run Code Online (Sandbox Code Playgroud)
当我关闭它时:
file.opened("C:\\Folder\\tbl1.xlsx")
# FALSE
Run Code Online (Sandbox Code Playgroud)