Skip to contents
  • file_extension() Get the extension of a file.

  • file_name() Get the name of a file.

  • file_dir() Get or directory of a file

  • manipulate_files() Manipulate files in a directory with options to rename (insert prefix or suffix) and save the new files to the same or other provided directory.

  • pliman_indexes() Get the indexes available in pliman.

  • pliman_indexes_eq() Get the equation of the indexes available in pliman.

Usage

file_extension(file)

file_name(file)

file_dir(file)

manipulate_files(
  pattern,
  dir = NULL,
  prefix = NULL,
  name = NULL,
  suffix = NULL,
  extension = NULL,
  sep = "",
  save_to = NULL,
  overwrite = FALSE,
  remove_original = FALSE,
  verbose = TRUE
)

Arguments

file

The file name.

pattern

A file name pattern.

dir

The working directory containing the files to be manipulated. Defaults to the current working directory.

prefix, suffix

A prefix or suffix to be added in the new file names. Defaults to NULL (no prefix or suffix).

name

The name of the new files. Defaults to NULL (original names). name can be either a single value or a character vector of the same length as the number of files manipulated. If one value is informed, a sequential vector of names will be created as "name_1", "name_2", and so on.

extension

The new extension of the file. If not declared (default), the original extensions will be used.

sep

An optional separator. Defaults to "".

save_to

The directory to save the new files. Defaults to the current working directory. If the file name of a file is not changed, nothing will occur. If save_to refers to a subfolder in the current working directory, the files will be saved to the given folder. In case of the folder doesn't exist, it will be created. By default, the files will not be overwritten. Set overwrite = TRUE to overwrite the files.

overwrite

Overwrite the files? Defaults to FALSE.

remove_original

Remove original files after manipulation? defaults to FALSE. If TRUE the files in pattern will be removed.

verbose

If FALSE, the code is run silently.

Value

  • file_extension(), file_name(), and file_dir() return a character string.

  • manipulate_files() No return value. If verbose == TRUE, a message is printed indicating which operation succeeded (or not) for each of the files attempted.

Examples

if (interactive() && requireNamespace("EBImage")) {
library(pliman)
# get file name, directory and extension
file <- "E:/my_folder/my_subfolder/image1.png"
file_dir(file)
file_name(file)
file_extension(file)

# manipulate files
dir <- tempdir()
list.files(dir)
file.create(paste0(dir, "/test.txt"))
list.files(dir)
manipulate_files("test",
                 dir = paste0(dir, "\\"),
                prefix = "chang_",
                save_to = paste0(dir, "\\"),
                overwrite = TRUE)
list.files(dir)
}
#> Warning: cannot create file 'C:\Users\tiago\AppData\Local\Temp\Rtmpu0MoqV\chang_test.c("tif", "aux", "xml")', reason 'Invalid argument'
#>  [1] "Rf62f4667779c2"                                                          
#>  [2] "chang_test.tif"                                                          
#>  [3] "chang_test.txt"                                                          
#>  [4] "data_stars62f419fb2489_fb8c50"                                           
#>  [5] "data_stars62f41e677f14_2f64b0"                                           
#>  [6] "data_stars62f4241374d2_6382d5"                                           
#>  [7] "data_stars62f42d307fe7_9fc3a4"                                           
#>  [8] "data_stars62f441123240_753db8"                                           
#>  [9] "downlit"                                                                 
#> [10] "downloaded_packages"                                                     
#> [11] "file62f4444c27b1"                                                        
#> [12] "file62f44d09386f"                                                        
#> [13] "file62f46c885600"                                                        
#> [14] "file62f478e54998"                                                        
#> [15] "file62f47d029f3"                                                         
#> [16] "foo.dll"                                                                 
#> [17] "foo.o"                                                                   
#> [18] "git-commit-message-62f432e01ac5.txt"                                     
#> [19] "libloc_181_c7e167d9.rds"                                                 
#> [20] "libloc_185_fc06ca9c.rds"                                                 
#> [21] "libloc_197_7764dce8.rds"                                                 
#> [22] "libloc_197_ac7c3ef3.rds"                                                 
#> [23] "pkgload62f436ff257"                                                      
#> [24] "pkgload62f4471366d"                                                      
#> [25] "pkgload62f465781156"                                                     
#> [26] "pkgload62f47ec92f92"                                                     
#> [27] "proc_sev_leaf.jpg"                                                       
#> [28] "proc_sev_leaf_nb.jpg"                                                    
#> [29] "repos_https%3A%2F%2Fcran.rstudio.com%2Fbin%2Fwindows%2Fcontrib%2F4.4.rds"
#> [30] "repos_https%3A%2F%2Fcran.rstudio.com%2Fsrc%2Fcontrib.rds"                
#> [31] "rs-graphics-3c45e660-992d-4263-a5b0-143a4bcc9f28"                        
#> [32] "temp_libpath62f46be42b72"                                                
#> [33] "test.tif"                                                                
#> [34] "test.tif.aux.xml"                                                        
#> [35] "test.txt"                                                                
#> [36] "viewhtml62f420e7764e"                                                    
#> [37] "viewhtml62f4f023804"