Description
This issue appears to apply to vectorised and loop approaches to renaming several files. Vectorised approach used in reprex below. The bug only seems to present when filenames do not contain meaningful or alphanumeric strings.
Summary
Existing files prior to file.rename:
"1.R" "2.R" "3.R" "4.R" "5.R".
Existing files following file.rename:
"6.R".
Expected output:
"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)
As in @CALUM Polwart 's suggestion, reordering the sequence of files to write resolves the issue. The initial reprex issue was renaming 1.R to 2.R to 3.R and so on.
Solution
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)