How to include a script that's outside the current folder in Julia? - julia

how can I include a script that's outside the current folder?
If the file is in the directory, I can simple run include(filename.jl), but if the file is outside the current working directory, I don't know how to pass an absolute path to include()

You can use the absolute path like below:
include("/path/to/filename.jl")
or use joinpath() to construct the absolute path
include(joinpath("path", "to", "filename.jl"))

Related

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.

here::here Pulls from Incorrect Directory and Ignores .here File in R

I am having trouble loading my .csv files with here::here.
The here package is setting its default directory as:
'Z:/PSE/Analysis/One'
My working directory is:
Z:/PSE and I have put a .here file in that directory. Here is the message I get after using dr_here():
Here() starts at Z:/PSE/Analysis/One, because it contains a file matching [.]Rproj$ with contents matching ^Version: in the first line
I thought the here function started at the current working directory, which is Z:/PSE, in which it should recognize the .here file and follow the path that I specify in the remainder of the here function?

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

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