Topics Map > Technical Support > Remote Access > Community Servers
Using relative paths on LINCOMM
Hints on how to use relative paths for R, Stata, and Matlab on LINCOMM servers.
- C:\users\jdoe\Documents\myfile.txt (Windows example)
- /users/jdoe/Documents/myfile.txt (Mac/Linux example)
- Starting point: C:\users\jdoe\Documents\myscript.cmd
- Accessing something in the same folder - path: ./file-in-same-folder.txt
- Accessing a file in a subfolder below the script - path: ./subfolder/anotherfolder/thefile.txt
- Accessing a file in the parent folder of the script - path : ../file-in-parent.txt
Relative Paths in Stata, R, MatLab and Python
Relative paths are handled differently by Stata, R, and Matlab. Although some sample code is given below for your benefit, it is not the ONLY way to do relative paths. In fact, there may actually be better ones. But this will get you started.
Stata is the easiest of the 3 software to deal with relative paths. Nothing special must be done. Simply add the relative path to the file name when working with files in your .do file. See the following examples:
import excel subfolder/data.xlsx, sheet("Sheet1")
import excel subfolder/anotherfolder/data.xlsx, sheet("Sheet1")
import excel ../../folderhere/data.xlsx, sheet("Sheet1") this goes up two directories, then into a subfolder "folderhere"
Download Stata sample files
Matlab requires a little work in order to effectively use relative paths (to the executing script). To do this:
- Get the absolute path for the executing script
currentFile = mfilename( 'fullpath' );
- Get the path part of the absolute path (everything except the file itself)
[pathstr,~,~] = fileparts( currentFile );
- Use the pathstrvariable to combine with relative paths
inputfilename = fullfile( pathstr, 'raw/in-data.txt' );
from pathlib import Path path = Path(__file__).parent / "data/transactions.csv"
R, unfortunately, is the most difficult to use with relative paths. This is because everything is relative to the Working Directory. There isn't a reliable cross-platform method to determine the file path of the executing script.One workaround, is to define an absolute path in a variable, set the Working Directory to that path, and then make all subsequent paths in the script relative to it. This single line of code will need to be changed if the file is executed in different locations. However, you can use a method of commenting in order to make it easier to deal with that situation.
# uncomment for mapped drive #rootPath <- "Z:"# uncomment for LINCOMM rootPath <- "/mnt/aae/users/jdoe # uncomment for Mac (typical) #rootPath <- "/volumes/aae/users/jdoe setwd(paste(rootPath, "/MyRfiles", sep="")) dt1 <- read.csv("./import/data.csv")