Managing file system is R is not that difficult since there are many built-in as well as third-party library functions available that we can use to perform various file operations as per our requirement.
How to Move Files in R
To move a file in R, use the move_files() function. The move_files() function is not a built-in R function. It is under the filesstrings library. To use the move_files() function, you need first to install the filesstrings library.
You can easily install any new library using package manager in R.
Syntax of move_files in R
move_files(files, destinations, overwrite = FALSE)
files: It is a character vector of files to move (relative or absolute paths).
destinations: It is a character vector of the destination directories into which to move the files.
overwrite: They allow overwriting of files? Default no.
It returns a logical vector with a TRUE for each time the operation succeeded and a FALSE for every failure.
The first step is to import the filesstrings library using the library() function.
The second step is to create a directory inside the current working directory using the dir.create() method.
Then we will create a vector of four files. Using the file.create() function to create all four files.
And then, we will move all these files to a new folder using the move_files() method.
library(filesstrings) dir.create("newdir") files <- c("a.txt", "b.txt", "c.txt", "d.txt") file.create(files) move_files(files, "newdir", overwrite=TRUE)
Loading required package: stringr  TRUE TRUE TRUE TRUE 4 files moved. 0 failed.
That’s it. If you check your current working directory, you can see a folder called newdir, and inside that folder, there should be 4 files that we moved from the current directory to the newdir folder.
If there are \(n\) files, there must be either \(1\) or \(n\) directories. If there is one directory, then all \(n\) files are moved there. If you try to move files to a directory that doesn’t exist, the directory is first created, and then the files are put inside.
That is it for move files in R tutorial.
Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.