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.

Using relative paths is a good coding practice regardless of the language or program in which you are coding.  It allows for portability of the code between directory locations on a single machine, or if copied or accessed on multiple machines.   It is essentially a necessity for any code housed in a version control system such as Git, as the paths will almost certainly be different on the various machines that might be working on the code.

It is no different using the LINCOMM servers.   It is a recommended best practice to author your code locally on your machine, especially as you are learning new software.  Once the code has been created and perfected to where you are ready to run it on a larger dataset, etc, copying it to the server and running it will be MUCH easier with relative paths.

An absolute path is one where the entire directory structure is listed.
  • C:\users\jdoe\Documents\myfile.txt   (Windows example)
  • /users/jdoe/Documents/myfile.txt   (Mac/Linux example)
A relative path is one that takes a starting point (usually a code file) and references all other paths relative to it.
  • 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

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

Matlab requires a little work in order to effectively use relative paths (to the executing script).  To do this:

  1. Get the absolute path for the executing script
    currentFile = mfilename( 'fullpath' );

  2. Get the path part of the absolute path (everything except the file itself)
    [pathstr,~,~] = fileparts( currentFile );

  3. Use the pathstr variable to combine with relative paths
    inputfilename = fullfile( pathstr, 'raw/in-data.txt' );

Python

Python works similarly to MatLab, in that you need to specify where the working path is for the Python script, and append that to your file paths

from pathlib import Path
path = Path(__file__).parent / "data/transactions.csv"

Download sample Python files

R

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")

Download R sample files




Keywords:LINCOMM; relative path; command line; scripts   Doc ID:105781
Owner:Eric D.Group:Agricultural & Applied Economics
Created:2020-09-13 19:28 CSTUpdated:2020-09-27 11:22 CST
Sites:Agricultural & Applied Economics
Feedback:  0   0