# NOT RUN {
library(datasets)
library(magrittr)
library(raster)
tmpDir <- file.path(tempdir(), "symlink-test") %>%
normalizePath(winslash = '/', mustWork = FALSE)
dir.create(tmpDir)
f0 <- file.path(tmpDir, "file0.csv")
write.csv(iris, f0)
d1 <- file.path(tmpDir, "dir1")
dir.create(d1)
write.csv(iris, file.path(d1, "file1.csv"))
d2 <- file.path(tmpDir, "dir2")
dir.create(d2)
f2 <- file.path(tmpDir, "file2.csv")
## create link to a file
linkOrCopy(f0, f2)
file.exists(f2) ## TRUE
identical(read.table(f0), read.table(f2)) ## TRUE
## deleting the link shouldn't delete the original file
unlink(f0)
file.exists(f0) ## FALSE
file.exists(f2) ## TRUE
## using rasters and other file-backed objects
f3a <- system.file("external/test.grd", package = "raster")
f3b <- system.file("external/test.gri", package = "raster")
r3a <- raster(f3a)
f4a <- file.path(tmpDir, "raster4.grd")
f4b <- file.path(tmpDir, "raster4.gri")
linkOrCopy(f3a, f4a) ## hardlink
linkOrCopy(f3b, f4b) ## hardlink
r4a <- raster(f4a)
isTRUE(all.equal(r3a, r4a)) # TRUE
## cleanup
unlink(tmpDir, recursive = TRUE)
# }
Run the code above in your browser using DataLab