Create a tar from a folder in R while keeping the relative path only - r

Following Jeff's reply here: https://stackoverflow.com/a/4627044/429846
I would like to tar a folder, but keep the path relative to the location of the folder (and not include the drive name and so on).
Sample code:
tarfile <- 'newfile.tgz'
tar(tarfile,file.path(getwd(), "folder_name"),compression='gzip')
Is it possible? (either in "tar" or another function name)
Thanks.

Related

How to import an external dataset into in a Moodle question?

I would like to import an external dataset using read.table() (or any other function for reading files) and then randomize or sample over it. The file is stored in a subfolder within the parent folder that contains the exercises *.rmd. I am working within a RStudio project. I tried placing the dataset in different levels of the folder structure. Using relative path did not work, but absolute paths did.
My folder structure is:
$home/project_name/exercises # It contains the RMD files
$home/project_name/exercises/data # It contains data files that I want to process
$home/project_name/datasets # this folder could eventually contain the dataset I want to process
To make this code more portable, I would like to know o the manage relative paths within *.Rmd for the knitting process.
The exercises are copied to a temporary directory and processed there. Hence, the easiest option is to copy these files to the temporary directory using include_supplement("file.csv"). By default this assumes that the file.csv is in the same directory that the exercise itself resides in. If it is in a subdirectory you can use include_supplement("file.csv", recursive = TRUE) and then subdirectories are searched recursively for file.csv.
After using include_supplement(), the copied file is available locally and can be processed with read.table() or also included in the exercise as a supplementary file. See http://www.R-exams.org/templates/Rlogo/ for a worked example. However, note that the Rlogo template explicitly specifies the directory from which the file should be copied. This is not necessary if that directory is the same as the exercise or a subdirectory.

How to reference a data file not in a working directory

I'm using R and I know how to define a working directory:
setwd("C:/temp/")
I have a data file that is not in this folder. How can I access it? In SAS, it would be this:
libname x1 "C:\temp";
libname x2 "C:\temp\data folder";
There are several ways to solve this problem.
One is to save the current directory in a variable, temporarily change dir, then return to the current directory.
old_dir <- getwd()
setwd("C:/temp/data folder")
#code
setwd(old_dir)
Another is to use function file.path to make a fully qualified file name.
path <- "C:/temp/data folder"
filename <- "datastuff.csv"
fullname <- file.path(path, filename)
fullname
#[1] "C:/temp/data folder/datastuff.csv"
one way is just with another line,
setwd("C:/temp/newfolder")
read_csv(...)
and then again your original to get you back
setwd("C:/temp/")
In my opinion, library(here) is a nice one for easily setting relative paths and working up and down directories.
You can use the absolute file path or the relative file path in the same way that you would use the file name if the file were in your current working directory.
For a file in your current working directory:
source("myFile.R")
For a file in a directory that is a peer to your current working directory:
source("../folder/myFile.R")
In this relative path, the ../ indicates "up one directory". Relative file paths are relative to your current working directory.
For a file anywhere on your system:
source("C:/full/path/to/myFile.R")
Absolute paths start with C: on Windows, or / ("root") for *nix systems.

How can I set different directory to the current directory in R?

I'm trying to open a file that does not exist in my current directory. This file is named testFile.r existing in the data folder in my current directory. I tried file.path("./data") and then wanted to show the file with this command file.show("testFile.r") but it gives this error:
Error: File testFile.r does not exist.
And the command getwd() gives the previous current directory. So any thoughts on this?
You change your current directory using the command setwd(new_directory)
(replacing new_directory with the path to the directory you want).
If you'd rather stay in your current directory, just do
file.show("./data/testFile.r")
To keep track of multiple paths, you can save each as a variable and use the paste function to reference the file:
path1 <- "./data/"
path2 <- "./second_folder_name/"
file.show(paste0(path1, "testFile.R"))
file.show(paste0(path2, "testFile.R"))

Can I work with multiple working directories in R?

Can I work with parallel working directories in R, or can I change the working directory in a loop to access the files from different folders?
I find it easier to have a single working directory. You find out what that is using the
getwd()
function. Typically, my working directory is something like:
~/colin/project1/R
You can change your working directory using
setwd()
You can easily access other files using the full path. In particular, I find
##List files in current directory
list.files()
##Give full path
list.files(full.names=TRUE)
##list files in the species1 directory
list.files("species1/", full.names=TRUE)
very handy.
Don't change the working directory in a loop, loop over the directories and use file.path to get to the file you want. Something like:
for(path in c("data1","data2","data3")){
for(file in c("file1.txt","file2.txt")){
fullPath = file.path(path,file)
doSomethingWith(fullPath)
}
}
That will loop over data1/file1.txt, data1/file2.txt and so on. Note it will also handle differences between path separators in different operating systems - don't try and paste file path components together with paste because you'll get it wrong.

Determining current file's location in R to include file from same directory?

I want to be able to source() a file which includes a different file in its same directory, but I don't want to have to set the working directory from the R-prompt before running this file:
> getwd()
[1] "/Users/myser"
> source("/Users/myuser/workspace/myproject/myfile.r")
Inside /Users/myuser/workspace/myproject, there would be myfile.r and my-utils.r. myfile.r calls source('my-utils.r') from within it.
Other programming languages can determine the current file's path. Does R have something similar? Example:
cur_dir <- sys.get_current_file_path()
source(file.path(cur_dir, "my-utils.r"))
source("/Users/myuser/workspace/myproject/my-utils.r", chdir=TRUE)
When chdir option is set to true and the source file parameter is a full path, the directory of file will be used as the working directory while sourcing the file.
NOTE: cur_dir <- sys.get_current_file_path() doesn't make much sense because pathnames are not unique.

Resources