文件x不存在,但是if(file.exists(x))readRDS(x)尝试读取x

时间:2019-03-12 01:19:27

标签: r

我想使用一个具有指定命名约定的循环(例如File_a_1.RdsFile_a_2.RdsFile_b_1.Rds等)使用循环从目录中读取一堆文件。文件名中可能存在空格(例如,File_a_2.Rds可能丢失),但是空格本身不是问题。为了弥补差距,我尝试使用file.exists()if (file.exists(fname)) readRDS(fname) else NULL,但随后我收到一条错误消息,指出即使readRDS(fname)并没有,R也尝试了fname存在:Error in readRDS(fname) : error reading from connection。这是怎么回事?

测试此if语句的其他方法表明,当我不尝试使用readRDS()时,它可以工作。例如,if (file.exists(fname)) "apple" else "banana"在我期望的位置给了我苹果和香蕉,所以奇怪的是,没有对if (file.exists(fname)) readRDS(fname) else NULL应用相同的逻辑。同样,使用ifTRUE()也无济于事,相反an earlier post

循环的类型无关紧要,但是在这种情况下,我使用mclapply()对操作进行向量化。通常,我只会做类似dir() %>% do.call(readRDS)的操作,但是文件顺序错误,并且没有我要的嵌套结构。

会话信息:

Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.6 LTS

Matrix products: default
BLAS: /usr/lib/atlas-base/atlas/libblas.so.3.0
LAPACK: /usr/lib/atlas-base/atlas/liblapack.so.3.0

locale:
 [1] LC_CTYPE=en_NZ.UTF-8       LC_NUMERIC=C               LC_TIME=en_NZ.UTF-8        LC_COLLATE=en_NZ.UTF-8    
 [5] LC_MONETARY=en_NZ.UTF-8    LC_MESSAGES=en_NZ.UTF-8    LC_PAPER=en_NZ.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_NZ.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] grid      parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] DMwR_0.4.1      ROCR_1.0-7      gplots_3.0.1.1  caret_6.0-81    lattice_0.20-38 ranger_0.11.1  
 [7] lmerTest_3.1-0  lme4_1.1-20     Matrix_1.2-15   magrittr_1.5    forcats_0.4.0   stringr_1.4.0  
[13] dplyr_0.8.0.1   purrr_0.3.0     readr_1.3.1     tidyr_0.8.2     tibble_2.0.1    ggplot2_3.1.0  
[19] tidyverse_1.2.1

loaded via a namespace (and not attached):
 [1] nlme_3.1-137       bitops_1.0-6       xts_0.11-2         lubridate_1.7.4    httr_1.4.0        
 [6] numDeriv_2016.8-1  tools_3.5.2        backports_1.1.3    R6_2.4.0           rpart_4.1-13      
[11] KernSmooth_2.23-15 lazyeval_0.2.1     colorspace_1.4-0   nnet_7.3-12        withr_2.1.2       
[16] tidyselect_0.2.5   curl_3.3           compiler_3.5.2     cli_1.0.1          rvest_0.3.2       
[21] xml2_1.2.0         caTools_1.17.1.1   scales_1.0.0       digest_0.6.18      minqa_1.2.4       
[26] rmarkdown_1.11     base64enc_0.1-3    pkgconfig_2.0.2    htmltools_0.3.6    TTR_0.23-4        
[31] rlang_0.3.1        readxl_1.3.0       rstudioapi_0.9.0   quantmod_0.4-13    generics_0.0.2    
[36] zoo_1.8-4          jsonlite_1.6       gtools_3.8.1       ModelMetrics_1.2.2 Rcpp_1.0.0        
[41] munsell_0.5.0      abind_1.4-5        stringi_1.3.1      yaml_2.2.0         MASS_7.3-51.1     
[46] plyr_1.8.4         recipes_0.1.4      gdata_2.18.0       crayon_1.3.4       haven_2.1.0       
[51] splines_3.5.2      hms_0.4.2          knitr_1.21         pillar_1.3.1       reshape2_1.4.3    
[56] codetools_0.2-16   stats4_3.5.2       glue_1.3.0         evaluate_0.13      data.table_1.12.0 
[61] modelr_0.1.4       nloptr_1.2.1       foreach_1.4.4      cellranger_1.1.0   gtable_0.2.0      
[66] assertthat_0.2.0   xfun_0.5           gower_0.1.2        prodlim_2018.04.18 broom_0.5.1       
[71] class_7.3-15       survival_2.43-3    timeDate_3043.102  iterators_1.0.10   lava_1.6.5        
[76] ipred_0.9-8       ```

0 个答案:

没有答案
相关问题