R错误覆盖不存在的文件

问题描述 投票:0回答:1

我正在尝试使用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)

发生了什么事?

r file overwrite openxlsx
1个回答
0
投票

我似乎无法通过仿真来复制此问题。您如何确定未创建文件(例如,是否可以在file.exists之后立即运行saveWorkbook?)。我将更新openxlsx,看看是否可以在新的会话中复制此示例。

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(v0.3.0)在2019-10-15创建

© www.soinside.com 2019 - 2024. All rights reserved.