Go to the RMD, R, PDF, or HTML version of this file. Go back to fan’s REconTools Package, R Code Examples Repository (bookdown site), or Intro Stats with R Repository (bookdown site).
Take different string-based actions if some path exists on a computer.
# Check if either of the path exists on the computer
spt_root_computer_a <- 'G:/repos/R4Econ/'
spt_root_computer_b <- 'C:/Users/fan/R4Econ/'
# Checking if directory exists
if (dir.exists(spt_root_computer_a)) {
print(paste(spt_root_computer_a, 'exists'))
} else if (dir.exists(spt_root_computer_b)) {
print(paste(spt_root_computer_b, 'exists', spt_root_computer_a, 'does not exist'))
} else {
print(paste(spt_root_computer_b, spt_root_computer_a, 'both do does not exist'))
}
## [1] "C:/Users/fan/R4Econ/ exists G:/repos/R4Econ/ does not exist"
Now, we check if a particular file exists.
# Check if either of the files exists on the computer
spn_root_computer_a <- 'G:/repos/R4Econ/index.Rmd'
spn_root_computer_b <- 'C:/Users/fan/R4Econ/index.Rmd'
# Checking if file exists
if (file.exists(spn_root_computer_a)) {
print(paste(spn_root_computer_a, 'exists'))
} else if (file.exists(spn_root_computer_b)) {
print(paste(spn_root_computer_b, 'exists', spn_root_computer_a, 'does not exist'))
} else {
print(paste(spn_root_computer_b, spn_root_computer_a, 'both do does not exist'))
}
## [1] "C:/Users/fan/R4Econ/index.Rmd exists G:/repos/R4Econ/index.Rmd does not exist"
File Path might contain information related to the file, decompose the file path, keep the final N folder names, to be possibled stored as a variable in the datafile stored inside.
# Compose together a path
print(paste0('.Platform$file.sep=', .Platform$file.sep))
## [1] ".Platform$file.sep=/"
spn_file_path = file.path("C:", "Users", "fan", "R4Econ", "amto", "tibble",
"fs_tib_basics.Rmd",
fsep = .Platform$file.sep)
# print
print(spn_file_path)
## [1] "C:/Users/fan/R4Econ/amto/tibble/fs_tib_basics.Rmd"
From path, get file name without suffix.
st_example <- 'C:/Users/fan/R4Econ/amto/tibble/fs_tib_basics.Rmd'
st_file_wth_suffix_s <- tail(strsplit(st_example, "/")[[1]],n=1)
st_file_wno_suffix_s <- tools::file_path_sans_ext(basename(st_example))
st_fullpath_nosufx_s <- sub('\\.Rmd$', '', st_example)
st_fullpath_noname_s <- dirname(st_example)
print(paste0('st_file_wth_suffix_s:', st_file_wth_suffix_s))
## [1] "st_file_wth_suffix_s:fs_tib_basics.Rmd"
print(paste0('st_file_wno_suffix_s:', st_file_wno_suffix_s))
## [1] "st_file_wno_suffix_s:fs_tib_basics"
print(paste0('st_fullpath_nosufx_s:', st_fullpath_nosufx_s))
## [1] "st_fullpath_nosufx_s:C:/Users/fan/R4Econ/amto/tibble/fs_tib_basics"
print(paste0('st_fullpath_noname_s:', st_fullpath_noname_s))
## [1] "st_fullpath_noname_s:C:/Users/fan/R4Econ/amto/tibble"
File Path might contain information related to the file, decompose the file path, keep the final N folder names, to be possibled stored as a variable in the datafile stored inside.
# Compose together a path
spn_example <- 'C:/Users/fan/R4Econ/amto/tibble/fs_tib_basics.Rmd'
# Replace default system slash, assume spn was generated by system.
ls_srt_folders_file <- strsplit(st_example, .Platform$file.sep)[[1]]
# Keep the last N layers
it_folders_names_to_keep = 2
snm_file_name <- tail(strsplit(st_example, "/")[[1]],n=1)
ls_srt_folders_keep <- head(tail(ls_srt_folders_file, it_folders_names_to_keep+1),
it_folders_names_to_keep)
# Show folder names
print(paste0('snm_file_name:', snm_file_name))
## [1] "snm_file_name:fs_tib_basics.Rmd"
print(paste0('last ', it_folders_names_to_keep, ' folders:'))
## [1] "last 2 folders:"
print(ls_srt_folders_keep)
## [1] "amto" "tibble"
Shorter lines, to make copying easier.
# inputs
it_folders_names_to_keep = 2
spn_example <- 'C:/Users/fan/R4Econ/amto/tibble/fs_tib_basics.Rmd'
# copy these
ls_srt_folders_name_keep <- tail(strsplit(st_example, "/")[[1]], n=it_folders_names_to_keep+1)
snm_file_name <- tail(ls_srt_folders_name_keep, 1)
ls_srt_folders_keep <- head(ls_srt_folders_name_keep, it_folders_names_to_keep)
# print
print(paste0('snm_file_name:', snm_file_name))
## [1] "snm_file_name:fs_tib_basics.Rmd"
print(paste0('last ', it_folders_names_to_keep, ' folders:'))
## [1] "last 2 folders:"
print(ls_srt_folders_keep)
## [1] "amto" "tibble"