file.rename 如果文件名格式为 <0-9>.R 则删除文件
file.rename deletes files if filename format is <0-9>.R
描述
此问题似乎适用于重命名多个文件的矢量化和循环方法。下面的 reprex 中使用的向量化方法。该错误似乎仅在文件名不包含有意义的字符串或字母数字字符串时出现。
总结
file.rename之前的现有文件:
"1.R" "2.R" "3.R" "4.R" "5.R".
file.rename 之后的现有文件:
“6.R”。
预期输出:
“2.R” “3.R” “4.R” “5.R” “6.R”
Reprex
library(stringr)
# how many files to create
req_nos <- c(1:5)
# create file names
req_names <- paste(req_nos, "R", sep = ".")
# create the files
invisible(file.create(req_names))
# adjust the numbering plus 1
new_nos <- as.character(req_nos + 1)
# create new names with the new numbering
new_names <- str_replace(req_names, "^[0-9]*", new_nos)
# file.rename loses the files up to the largest value of new_nos
file.rename(from = req_names, to = new_names)
devtools::session_info
─ Session info
***
setting value
version R version 4.0.4 (2021-02-15)
os macOS Catalina 10.15.7
system x86_64, darwin17.0
ui RStudio
language (EN)
collate en_GB.UTF-8
ctype en_GB.UTF-8
tz Europe/London
date 2021-03-20
─ Packages
***
package * version date lib source
assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.0.0)
audio 0.1-7 2020-03-09 [1] CRAN (R 4.0.2)
beepr 1.3 2018-06-04 [1] CRAN (R 4.0.2)
callr 3.5.1 2020-10-13 [1] CRAN (R 4.0.2)
cli 2.3.1 2021-02-23 [1] CRAN (R 4.0.1)
crayon 1.4.1 2021-02-08 [1] CRAN (R 4.0.2)
desc 1.2.0 2018-05-01 [1] CRAN (R 4.0.0)
devtools 2.3.2 2020-09-18 [1] CRAN (R 4.0.2)
digest 0.6.27 2020-10-24 [1] CRAN (R 4.0.2)
ellipsis 0.3.1 2020-05-15 [1] CRAN (R 4.0.0)
fs 1.5.0 2020-07-31 [1] CRAN (R 4.0.2)
glue 1.4.2 2020-08-27 [1] CRAN (R 4.0.2)
log4r 0.3.2 2020-01-18 [1] CRAN (R 4.0.2)
magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.0.2)
memoise 1.1.0 2017-04-21 [1] CRAN (R 4.0.2)
pkgbuild 1.2.0 2020-12-15 [1] CRAN (R 4.0.2)
pkgload 1.1.0 2020-05-29 [1] CRAN (R 4.0.2)
prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.0.0)
processx 3.4.5 2020-11-30 [1] CRAN (R 4.0.2)
ps 1.5.0 2020-12-05 [1] CRAN (R 4.0.2)
R6 2.5.0 2020-10-28 [1] CRAN (R 4.0.2)
remotes 2.2.0 2020-07-21 [1] CRAN (R 4.0.2)
rlang 0.4.10 2020-12-30 [1] CRAN (R 4.0.2)
rprojroot 2.0.2 2020-11-15 [1] CRAN (R 4.0.2)
sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.0.2)
stringi 1.5.3 2020-09-09 [1] CRAN (R 4.0.2)
stringr * 1.4.0 2019-02-10 [1] CRAN (R 4.0.0)
testthat 3.0.2 2021-02-14 [1] CRAN (R 4.0.2)
tinytex 0.23 2020-05-19 [1] CRAN (R 4.0.0)
usethis 1.6.3 2020-09-17 [1] CRAN (R 4.0.2)
withr 2.4.1 2021-01-26 [1] CRAN (R 4.0.2)
xfun 0.21 2021-02-10 [1] CRAN (R 4.0.2)
yaml 2.2.1 2020-02-01 [1] CRAN (R 4.0.0)
6.R实际上是1.R的内容吗?
我认为您看到 1.R 重命名为 2.R 并在此过程中覆盖了 2.R。新的2.R到3.R等等
file.rename(from = req_names, to = new_names, overwrite = F)
以上内容应该可以防止它们被意外重写,但会导致 error/warning
如果您颠倒重命名的顺序在这种情况下它将避免出现问题
或者将它们重命名为中介
1.R -> 2.R-tmp
2.R -> 3.R-tmp
等
然后
2.R-tmp -> 2.R
按照@CALUM Polwart 的建议,重新排序要写入的文件的顺序可以解决问题。
最初的 reprex 问题是将 1.R 重命名为 2.R 到 3.R 等等。
解决方案
library(stringr)
# how many files to create: REVERSE the sequence
req_nos <- order(-c(1:5))
# create file names
req_names <- paste(req_nos, "R", sep = ".")
# create the files
invisible(file.create(req_names))
# adjust the numbering plus 1
new_nos <- as.character(req_nos + 1)
# create new names with the new numbering
new_names <- str_replace(req_names, "^[0-9]*", new_nos)
# file.rename loses the files up to the largest value of new_nos
file.rename(from = req_names, to = new_names)
描述
此问题似乎适用于重命名多个文件的矢量化和循环方法。下面的 reprex 中使用的向量化方法。该错误似乎仅在文件名不包含有意义的字符串或字母数字字符串时出现。
总结
file.rename之前的现有文件:
"1.R" "2.R" "3.R" "4.R" "5.R".
file.rename 之后的现有文件:
“6.R”。
预期输出:
“2.R” “3.R” “4.R” “5.R” “6.R”
Reprex
library(stringr)
# how many files to create
req_nos <- c(1:5)
# create file names
req_names <- paste(req_nos, "R", sep = ".")
# create the files
invisible(file.create(req_names))
# adjust the numbering plus 1
new_nos <- as.character(req_nos + 1)
# create new names with the new numbering
new_names <- str_replace(req_names, "^[0-9]*", new_nos)
# file.rename loses the files up to the largest value of new_nos
file.rename(from = req_names, to = new_names)
devtools::session_info
─ Session info
***
setting value
version R version 4.0.4 (2021-02-15)
os macOS Catalina 10.15.7
system x86_64, darwin17.0
ui RStudio
language (EN)
collate en_GB.UTF-8
ctype en_GB.UTF-8
tz Europe/London
date 2021-03-20
─ Packages
***
package * version date lib source
assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.0.0)
audio 0.1-7 2020-03-09 [1] CRAN (R 4.0.2)
beepr 1.3 2018-06-04 [1] CRAN (R 4.0.2)
callr 3.5.1 2020-10-13 [1] CRAN (R 4.0.2)
cli 2.3.1 2021-02-23 [1] CRAN (R 4.0.1)
crayon 1.4.1 2021-02-08 [1] CRAN (R 4.0.2)
desc 1.2.0 2018-05-01 [1] CRAN (R 4.0.0)
devtools 2.3.2 2020-09-18 [1] CRAN (R 4.0.2)
digest 0.6.27 2020-10-24 [1] CRAN (R 4.0.2)
ellipsis 0.3.1 2020-05-15 [1] CRAN (R 4.0.0)
fs 1.5.0 2020-07-31 [1] CRAN (R 4.0.2)
glue 1.4.2 2020-08-27 [1] CRAN (R 4.0.2)
log4r 0.3.2 2020-01-18 [1] CRAN (R 4.0.2)
magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.0.2)
memoise 1.1.0 2017-04-21 [1] CRAN (R 4.0.2)
pkgbuild 1.2.0 2020-12-15 [1] CRAN (R 4.0.2)
pkgload 1.1.0 2020-05-29 [1] CRAN (R 4.0.2)
prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.0.0)
processx 3.4.5 2020-11-30 [1] CRAN (R 4.0.2)
ps 1.5.0 2020-12-05 [1] CRAN (R 4.0.2)
R6 2.5.0 2020-10-28 [1] CRAN (R 4.0.2)
remotes 2.2.0 2020-07-21 [1] CRAN (R 4.0.2)
rlang 0.4.10 2020-12-30 [1] CRAN (R 4.0.2)
rprojroot 2.0.2 2020-11-15 [1] CRAN (R 4.0.2)
sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.0.2)
stringi 1.5.3 2020-09-09 [1] CRAN (R 4.0.2)
stringr * 1.4.0 2019-02-10 [1] CRAN (R 4.0.0)
testthat 3.0.2 2021-02-14 [1] CRAN (R 4.0.2)
tinytex 0.23 2020-05-19 [1] CRAN (R 4.0.0)
usethis 1.6.3 2020-09-17 [1] CRAN (R 4.0.2)
withr 2.4.1 2021-01-26 [1] CRAN (R 4.0.2)
xfun 0.21 2021-02-10 [1] CRAN (R 4.0.2)
yaml 2.2.1 2020-02-01 [1] CRAN (R 4.0.0)
6.R实际上是1.R的内容吗?
我认为您看到 1.R 重命名为 2.R 并在此过程中覆盖了 2.R。新的2.R到3.R等等
file.rename(from = req_names, to = new_names, overwrite = F)
以上内容应该可以防止它们被意外重写,但会导致 error/warning
如果您颠倒重命名的顺序在这种情况下它将避免出现问题
或者将它们重命名为中介
1.R -> 2.R-tmp 2.R -> 3.R-tmp 等
然后
2.R-tmp -> 2.R
按照@CALUM Polwart 的建议,重新排序要写入的文件的顺序可以解决问题。 最初的 reprex 问题是将 1.R 重命名为 2.R 到 3.R 等等。
解决方案
library(stringr)
# how many files to create: REVERSE the sequence
req_nos <- order(-c(1:5))
# create file names
req_names <- paste(req_nos, "R", sep = ".")
# create the files
invisible(file.create(req_names))
# adjust the numbering plus 1
new_nos <- as.character(req_nos + 1)
# create new names with the new numbering
new_names <- str_replace(req_names, "^[0-9]*", new_nos)
# file.rename loses the files up to the largest value of new_nos
file.rename(from = req_names, to = new_names)