gpt4 book ai didi

R错误覆盖不存在的文件

转载 作者:行者123 更新时间:2023-12-05 06:23:24 25 4
gpt4 key购买 nike

我正在尝试使用 openxlsx 编写文件:

saveWorkbook(wb, file=filename, overwrite=T)

没有错误但没有文件出现,而

> saveWorkbook(wb, file=filename, overwrite=F)
Error in saveWorkbook(wb, file = filename, overwrite = F) :
File already exists!

我可以在 R 中很清楚地看到 t6his:

> filename
[1] "/home/balter/miseq-239/analysis/beta_diversity//tables/miseq-239_beta_diversity_Genus_clust_unifrac_permanova.xlsx"
> filedir = gsub('\\/[^/]+$', '', filename)
> filedir
[1] "/home/balter/miseq-239/analysis/beta_diversity//tables"
> file.exists(filename)
[1] TRUE
> list.files(path=filedir)
character(0)

这是怎么回事?

最佳答案

我似乎无法通过模拟重现这个问题。您如何确定该文件未创建(例如,您可以在 saveWorkbook 之后立即运行 file.exists 吗?)。我会更新 openxlsx 并查看您是否可以在新 session 中复制此示例。

library(openxlsx)
wb <- createWorkbook("Creator of workbook")
addWorksheet(wb, sheetName = "My first worksheet")

tmp <- tempfile(fileext = ".xlsx")

# Create when file does not exist [overwrite = TRUE]
file.exists(tmp)
#> [1] FALSE
saveWorkbook(wb, file = tmp, overwrite = TRUE)
#> Note: zip::zip() is deprecated, please use zip::zipr() instead
file.exists(tmp)
#> [1] TRUE

# Create when file does not exist [overwrite = FALSE]
file.remove(tmp)
#> [1] TRUE
file.exists(tmp)
#> [1] FALSE
saveWorkbook(wb, file = tmp, overwrite = FALSE)
file.exists(tmp)
#> [1] TRUE

# Create when file exists [overwrite = TRUE]
file.exists(tmp)
#> [1] TRUE
saveWorkbook(wb, file = tmp, overwrite = TRUE)
file.exists(tmp)
#> [1] TRUE

# Create when file exists [overwrite = FALSE]
file.exists(tmp)
#> [1] TRUE
saveWorkbook(wb, file = tmp, overwrite = FALSE)
#> Error in saveWorkbook(wb, file = tmp, overwrite = FALSE): File already exists!
file.exists(tmp)
#> [1] TRUE


sessionInfo()
#> R version 3.6.1 (2019-07-05)
#> Platform: x86_64-apple-darwin18.6.0 (64-bit)
#> Running under: macOS Mojave 10.14.6
#>
#> Matrix products: default
#> BLAS/LAPACK: /usr/local/Cellar/openblas/0.3.7/lib/libopenblasp-r0.3.7.dylib
#>
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] openxlsx_4.1.0.1
#>
#> loaded via a namespace (and not attached):
#> [1] compiler_3.6.1 magrittr_1.5 tools_3.6.1 htmltools_0.4.0
#> [5] yaml_2.2.0 Rcpp_1.0.2 stringi_1.4.3 rmarkdown_1.16
#> [9] highr_0.8 knitr_1.25 stringr_1.4.0 xfun_0.10
#> [13] digest_0.6.21 zip_2.0.4 rlang_0.4.0 evaluate_0.14

reprex package 创建于 2019-10-15 (v0.3.0)

关于R错误覆盖不存在的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58401681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com