How to reference a data file not in a working directory - r

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.

Related

How to read an Excel file from a folder without specifying the filename?

Is there a way to read an Excel file into R directly from a folder without having to specify the filename?
I'm using the readxl library and I have only 1 file in the folder, but the file's name changes monthly.
Thanks!
The computer need to have the path anyway BUT you can get it without giving if you are absolutly sure that this is the only one file in your folder
see https://stat.ethz.ch/R-manual/R-devel/library/base/html/list.files.html
to learn more about how to open a directory and getting filename inside.
If this isn't the only file but this is the only excel file you while have to get the extension of each file and do some parsing to take a decision of wich file you want to open
As noted in other answers, this can be solved using the dir function.
Assuming the variable path contains the path of the directory in which the XLSX file is located, then the following will give you the full path to the file:
file_path = dir(path, pattern = '\\.xlsx$', full.names = TRUE)
Note that pattern uses regular expressions rather than glob format! Using the glob pattern *.xlsx might appear to work but it’s incorrect, only works by accident, and will also match other file names.
Suppose your file is located inside a folder called myFolder located in disk E:\\
library(readxl)
folderPath <- "E://myFolder"
fileName <- grep(".xlsx", dir(folderPath), value = TRUE)
filePath <- paste(folderPath, fileName, sep = "//")
df <- read_xlsx(filePath)
This code will get the name of your xlsx file inside folderPath each time you run it and then you can import it with readxl::read_xlsx. I assume there is only one xlsx file inside the folder.

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.

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

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.

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