用R中的奇怪符号重命名文件

时间:2019-10-08 09:01:17

标签: r file

我有一个文件名为:

  

16900-19 odb。 Parcelacija katastar-乌卢卡卡(SluĹľbenovišeuloĹľaka)   145.doc

如何重命名此文件?如果我尝试了多个软件包,但总是说该文件不存在:

# dont run
    list.files()
[1] "16900-19 odb. parcelacija katastar - službeno  više uložaka, čl. 145.doc"

file.rename(list.files(), "test.doc") 
fs::file_move(list.files(), "test.doc")

最后一行返回错误:

Error: [ENOENT] Failed to move '16900-19 odb. parcelacija katastar - službeno  više uložaka, čl. 145.doc'to 'test.doc': no such file or directory

sessionInfo的信息:

R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

Matrix products: default

Random number generation:
 RNG:     Mersenne-Twister 
 Normal:  Inversion 
 Sample:  Rounding 

locale:
[1] LC_COLLATE=Croatian_Croatia.1250  LC_CTYPE=Croatian_Croatia.1250    LC_MONETARY=Croatian_Croatia.1250 LC_NUMERIC=C                     
[5] LC_TIME=Croatian_Croatia.1250    

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

other attached packages:
 [1] zip_2.0.3         readxl_1.3.1.9000 RCurl_1.95-4.12   bitops_1.0-6      docker_0.0.9000   reshape2_1.4.3    reticulate_1.12   ids_1.0.1        
 [9] lubridate_1.7.4   pdftools_2.2      anytime_0.3.4     jsonlite_1.6      httr_1.4.1        mailR_0.4.1       taskscheduleR_1.4 RODBC_1.3-15     
[17] RMySQL_0.10.17    DBI_1.0.0         stringi_1.4.3     forcats_0.4.0     stringr_1.4.0     dplyr_0.8.3       purrr_0.3.2.9000  readr_1.3.1      
[25] tidyr_1.0.0       tibble_2.1.3      ggplot2_3.2.0     tidyverse_1.2.1   rvest_0.3.4       xml2_1.2.2       

loaded via a namespace (and not attached):
 [1] carData_3.0-2      R.utils_2.9.0      modelr_0.1.4       assertthat_0.2.1   askpass_1.1        selectr_0.4-1      memisc_0.99.17.2  
 [8] cellranger_1.1.0   qpdf_1.1           pillar_1.4.2       backports_1.1.4    lattice_0.20-38    glue_1.3.1         uuid_0.1-2        
[15] digest_0.6.21      colorspace_1.4-1   htmltools_0.4.0    Matrix_1.2-17      R.oo_1.22.0        plyr_1.8.4         pkgconfig_2.0.3   
[22] broom_0.5.2        haven_2.1.1        scales_1.0.0       processx_3.4.1     openxlsx_4.1.0.1   rio_0.5.16         openssl_1.4.1     
[29] generics_0.0.2     car_3.0-3          withr_2.1.2        repr_1.0.1         lazyeval_0.2.2     cli_1.1.0          magrittr_1.5      
[36] crayon_1.3.4       ps_1.3.0           R.methodsS3_1.7.1  fs_1.3.1           fansi_0.4.0        nlme_3.1-140       MASS_7.3-51.4     
[43] foreign_0.8-71     tools_3.6.1        data.table_1.12.2  hms_0.5.1          lifecycle_0.1.0    RApiDatetime_0.0.4 munsell_0.5.0     
[50] packrat_0.5.0      compiler_3.6.1     rlang_0.4.0        grid_3.6.1         rstudioapi_0.10    base64enc_0.1-3    gtable_0.3.0      
[57] abind_1.4-5        curl_4.0           R6_2.4.0           utf8_1.1.4         zeallot_0.1.0      rJava_0.9-11       Rcpp_1.0.2        
[64] vctrs_0.2.0        tidyselect_0.2.5  

1 个答案:

答案 0 :(得分:0)

R可能无法识别所有怪异的符号。您可以通过将列表分配给变量并使用索引而不是名称来避免这种情况:

list_names <- list.files
file.rename(list_names[1], "test.doc")

修改

嗯,这行不通。我尝试了几种方法,例如用UTF-8代码替换R不能识别的“ ˇ”字符(它用“?”表示),但文件名仍然未被识别。我还发现了另一个未解决的问题:

Reading accented filenames in R using list.files

但是,这些都没有让我更接近可行的解决方案。