Relative paths in R: how to avoid my computer being set on fire?
up vote
1
down vote
favorite
A while back I was reading an article about improving project workflow. The advice was not to use setwd
or my computer would burn:
If the first line of your R script is
setwd("C:UsersjennypaththatonlyIhave")
I will come into your office and SET YOUR COMPUTER ON FIRE 🔥.
I started using the here
package and it worked great until I started to schedule scripts using cronR
. After asking this question my laptop was again threatened with arson:
If the first line of your #rstats script is wd <- here(), I will come
into your lab and SET YOUR COMPUTER ON FIRE.
Fearing for my laptop's safety I started using the method suggested in the answer to get relative file paths:
wd <- Sys.getenv("HOME")
wd <- file.path(wd, "projects", "my_proj")
Which worked for me but not people I was working with who didn't have the same projects
directory. So now I'm confused. What is the safest / best way get relative file paths so that a project can be portable?
There are quite a few options: 1, 2. My requirements are to source functions/scripts and read/write csv files. Perhaps the rprojroot
package is the best bet?
r
add a comment |
up vote
1
down vote
favorite
A while back I was reading an article about improving project workflow. The advice was not to use setwd
or my computer would burn:
If the first line of your R script is
setwd("C:UsersjennypaththatonlyIhave")
I will come into your office and SET YOUR COMPUTER ON FIRE 🔥.
I started using the here
package and it worked great until I started to schedule scripts using cronR
. After asking this question my laptop was again threatened with arson:
If the first line of your #rstats script is wd <- here(), I will come
into your lab and SET YOUR COMPUTER ON FIRE.
Fearing for my laptop's safety I started using the method suggested in the answer to get relative file paths:
wd <- Sys.getenv("HOME")
wd <- file.path(wd, "projects", "my_proj")
Which worked for me but not people I was working with who didn't have the same projects
directory. So now I'm confused. What is the safest / best way get relative file paths so that a project can be portable?
There are quite a few options: 1, 2. My requirements are to source functions/scripts and read/write csv files. Perhaps the rprojroot
package is the best bet?
r
2
I didn't visit links 1 or 2 butrprojroot
orhere
are my go-to options for projects (provided you've got one of the "markers" in the project directory (and you use projects like you should :-) With regard to scripts, the environment variable approach is pretty common for bash, python, etc scripts and is a fine option for R. For some automation tasks I have a "jobs" folder where the R scripts are and a "conf" folder where Renviron-like files sit and IreadRenviron
the associated one right at the top of the script. But for sharing projects, I see nothing wrong withhere::here()
– hrbrmstr
Nov 22 at 15:27
Create an environment variable for the root folder of each project you want to share in other ways than merely an interactive rstudio project.
– jwijffels
Nov 25 at 18:30
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
A while back I was reading an article about improving project workflow. The advice was not to use setwd
or my computer would burn:
If the first line of your R script is
setwd("C:UsersjennypaththatonlyIhave")
I will come into your office and SET YOUR COMPUTER ON FIRE 🔥.
I started using the here
package and it worked great until I started to schedule scripts using cronR
. After asking this question my laptop was again threatened with arson:
If the first line of your #rstats script is wd <- here(), I will come
into your lab and SET YOUR COMPUTER ON FIRE.
Fearing for my laptop's safety I started using the method suggested in the answer to get relative file paths:
wd <- Sys.getenv("HOME")
wd <- file.path(wd, "projects", "my_proj")
Which worked for me but not people I was working with who didn't have the same projects
directory. So now I'm confused. What is the safest / best way get relative file paths so that a project can be portable?
There are quite a few options: 1, 2. My requirements are to source functions/scripts and read/write csv files. Perhaps the rprojroot
package is the best bet?
r
A while back I was reading an article about improving project workflow. The advice was not to use setwd
or my computer would burn:
If the first line of your R script is
setwd("C:UsersjennypaththatonlyIhave")
I will come into your office and SET YOUR COMPUTER ON FIRE 🔥.
I started using the here
package and it worked great until I started to schedule scripts using cronR
. After asking this question my laptop was again threatened with arson:
If the first line of your #rstats script is wd <- here(), I will come
into your lab and SET YOUR COMPUTER ON FIRE.
Fearing for my laptop's safety I started using the method suggested in the answer to get relative file paths:
wd <- Sys.getenv("HOME")
wd <- file.path(wd, "projects", "my_proj")
Which worked for me but not people I was working with who didn't have the same projects
directory. So now I'm confused. What is the safest / best way get relative file paths so that a project can be portable?
There are quite a few options: 1, 2. My requirements are to source functions/scripts and read/write csv files. Perhaps the rprojroot
package is the best bet?
r
r
asked Nov 22 at 15:18
Pete900
7611824
7611824
2
I didn't visit links 1 or 2 butrprojroot
orhere
are my go-to options for projects (provided you've got one of the "markers" in the project directory (and you use projects like you should :-) With regard to scripts, the environment variable approach is pretty common for bash, python, etc scripts and is a fine option for R. For some automation tasks I have a "jobs" folder where the R scripts are and a "conf" folder where Renviron-like files sit and IreadRenviron
the associated one right at the top of the script. But for sharing projects, I see nothing wrong withhere::here()
– hrbrmstr
Nov 22 at 15:27
Create an environment variable for the root folder of each project you want to share in other ways than merely an interactive rstudio project.
– jwijffels
Nov 25 at 18:30
add a comment |
2
I didn't visit links 1 or 2 butrprojroot
orhere
are my go-to options for projects (provided you've got one of the "markers" in the project directory (and you use projects like you should :-) With regard to scripts, the environment variable approach is pretty common for bash, python, etc scripts and is a fine option for R. For some automation tasks I have a "jobs" folder where the R scripts are and a "conf" folder where Renviron-like files sit and IreadRenviron
the associated one right at the top of the script. But for sharing projects, I see nothing wrong withhere::here()
– hrbrmstr
Nov 22 at 15:27
Create an environment variable for the root folder of each project you want to share in other ways than merely an interactive rstudio project.
– jwijffels
Nov 25 at 18:30
2
2
I didn't visit links 1 or 2 but
rprojroot
or here
are my go-to options for projects (provided you've got one of the "markers" in the project directory (and you use projects like you should :-) With regard to scripts, the environment variable approach is pretty common for bash, python, etc scripts and is a fine option for R. For some automation tasks I have a "jobs" folder where the R scripts are and a "conf" folder where Renviron-like files sit and I readRenviron
the associated one right at the top of the script. But for sharing projects, I see nothing wrong with here::here()
– hrbrmstr
Nov 22 at 15:27
I didn't visit links 1 or 2 but
rprojroot
or here
are my go-to options for projects (provided you've got one of the "markers" in the project directory (and you use projects like you should :-) With regard to scripts, the environment variable approach is pretty common for bash, python, etc scripts and is a fine option for R. For some automation tasks I have a "jobs" folder where the R scripts are and a "conf" folder where Renviron-like files sit and I readRenviron
the associated one right at the top of the script. But for sharing projects, I see nothing wrong with here::here()
– hrbrmstr
Nov 22 at 15:27
Create an environment variable for the root folder of each project you want to share in other ways than merely an interactive rstudio project.
– jwijffels
Nov 25 at 18:30
Create an environment variable for the root folder of each project you want to share in other ways than merely an interactive rstudio project.
– jwijffels
Nov 25 at 18:30
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
There are many ways to organize code and data for use with R. Given that the "arsonist" described in the OP has rejected at least two approaches for locating the project files in an R script, the best next step is to ask the arsonist how s/he performs this function, and adjust your code and file structures accordingly.
UPDATE: Since the "arsonists" appear to be someone who writes on Tidyverse.org (see Tidyverse article in OP) and an answer on SO (see additional links in OP), your computer appears to be relatively safe.
If you are sharing code or executing it with batch processes where the "user" is someone other than you, a useful approach is to place the code, data, and configuration under version control, and develop a runbook to explain how others can retrieve the components and execute them on another computer.
As noted in the comments to the OP, there's nothing wrong with here::here()
if its use can be made reliable through documentation in a runbook.
I structure all of my R code into Projects within RStudio, which are organized into a gitrepositories
directory. All of the projects can be accessed as subdirectories from the gitrepositories
directory. If I need to share a project, I make the project accessible to other users on GitHub.
In my R code I reference external files as subdirectories from the project root directory, such as ./data/gen01.csv
.
Thanks. Is a "runbook" something specific or just a README equivalent?
– Pete900
Nov 22 at 16:51
1
You're welcome, @Pete900. A runbook is a set of procedures that is typically carried out by a technology Operations department. A README can be used as the place where a runbook is documented. Here is a very simple example that I wrote a few years ago for the Johns Hopkins Getting and Cleaning Data final assignment.
– Len Greski
Nov 22 at 16:57
add a comment |
up vote
2
down vote
Create an RStudio project and then reference all files with relative paths from the project's root folder. That way, all users will open the project and automatically have the correct working directory.
What is an "R project"?
– Ista
Nov 22 at 16:19
@Ista - see R projects in RStudio for details.
– Len Greski
Nov 22 at 16:48
@LenGreski the article you linked to never uses the phrase "R project" because "R project" is not a thing. "Rstudio project" is a thing, but Rstudio is not R, and "Rstudio project" is not "R project".
– Ista
Nov 22 at 17:01
@ista - I didn't intend to start an ontological debate here, I just explained what I thought Felipe intended by his post. I understand that projects are an RStudio feature, since the URL I posted refers to the RStudio website.
– Len Greski
Nov 22 at 20:02
1
Thank you @LenGreski, that is indeed what I meant to say. I edited my answer to reflect this.
– Felipe Gerard
Nov 26 at 19:00
|
show 1 more comment
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
There are many ways to organize code and data for use with R. Given that the "arsonist" described in the OP has rejected at least two approaches for locating the project files in an R script, the best next step is to ask the arsonist how s/he performs this function, and adjust your code and file structures accordingly.
UPDATE: Since the "arsonists" appear to be someone who writes on Tidyverse.org (see Tidyverse article in OP) and an answer on SO (see additional links in OP), your computer appears to be relatively safe.
If you are sharing code or executing it with batch processes where the "user" is someone other than you, a useful approach is to place the code, data, and configuration under version control, and develop a runbook to explain how others can retrieve the components and execute them on another computer.
As noted in the comments to the OP, there's nothing wrong with here::here()
if its use can be made reliable through documentation in a runbook.
I structure all of my R code into Projects within RStudio, which are organized into a gitrepositories
directory. All of the projects can be accessed as subdirectories from the gitrepositories
directory. If I need to share a project, I make the project accessible to other users on GitHub.
In my R code I reference external files as subdirectories from the project root directory, such as ./data/gen01.csv
.
Thanks. Is a "runbook" something specific or just a README equivalent?
– Pete900
Nov 22 at 16:51
1
You're welcome, @Pete900. A runbook is a set of procedures that is typically carried out by a technology Operations department. A README can be used as the place where a runbook is documented. Here is a very simple example that I wrote a few years ago for the Johns Hopkins Getting and Cleaning Data final assignment.
– Len Greski
Nov 22 at 16:57
add a comment |
up vote
2
down vote
accepted
There are many ways to organize code and data for use with R. Given that the "arsonist" described in the OP has rejected at least two approaches for locating the project files in an R script, the best next step is to ask the arsonist how s/he performs this function, and adjust your code and file structures accordingly.
UPDATE: Since the "arsonists" appear to be someone who writes on Tidyverse.org (see Tidyverse article in OP) and an answer on SO (see additional links in OP), your computer appears to be relatively safe.
If you are sharing code or executing it with batch processes where the "user" is someone other than you, a useful approach is to place the code, data, and configuration under version control, and develop a runbook to explain how others can retrieve the components and execute them on another computer.
As noted in the comments to the OP, there's nothing wrong with here::here()
if its use can be made reliable through documentation in a runbook.
I structure all of my R code into Projects within RStudio, which are organized into a gitrepositories
directory. All of the projects can be accessed as subdirectories from the gitrepositories
directory. If I need to share a project, I make the project accessible to other users on GitHub.
In my R code I reference external files as subdirectories from the project root directory, such as ./data/gen01.csv
.
Thanks. Is a "runbook" something specific or just a README equivalent?
– Pete900
Nov 22 at 16:51
1
You're welcome, @Pete900. A runbook is a set of procedures that is typically carried out by a technology Operations department. A README can be used as the place where a runbook is documented. Here is a very simple example that I wrote a few years ago for the Johns Hopkins Getting and Cleaning Data final assignment.
– Len Greski
Nov 22 at 16:57
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
There are many ways to organize code and data for use with R. Given that the "arsonist" described in the OP has rejected at least two approaches for locating the project files in an R script, the best next step is to ask the arsonist how s/he performs this function, and adjust your code and file structures accordingly.
UPDATE: Since the "arsonists" appear to be someone who writes on Tidyverse.org (see Tidyverse article in OP) and an answer on SO (see additional links in OP), your computer appears to be relatively safe.
If you are sharing code or executing it with batch processes where the "user" is someone other than you, a useful approach is to place the code, data, and configuration under version control, and develop a runbook to explain how others can retrieve the components and execute them on another computer.
As noted in the comments to the OP, there's nothing wrong with here::here()
if its use can be made reliable through documentation in a runbook.
I structure all of my R code into Projects within RStudio, which are organized into a gitrepositories
directory. All of the projects can be accessed as subdirectories from the gitrepositories
directory. If I need to share a project, I make the project accessible to other users on GitHub.
In my R code I reference external files as subdirectories from the project root directory, such as ./data/gen01.csv
.
There are many ways to organize code and data for use with R. Given that the "arsonist" described in the OP has rejected at least two approaches for locating the project files in an R script, the best next step is to ask the arsonist how s/he performs this function, and adjust your code and file structures accordingly.
UPDATE: Since the "arsonists" appear to be someone who writes on Tidyverse.org (see Tidyverse article in OP) and an answer on SO (see additional links in OP), your computer appears to be relatively safe.
If you are sharing code or executing it with batch processes where the "user" is someone other than you, a useful approach is to place the code, data, and configuration under version control, and develop a runbook to explain how others can retrieve the components and execute them on another computer.
As noted in the comments to the OP, there's nothing wrong with here::here()
if its use can be made reliable through documentation in a runbook.
I structure all of my R code into Projects within RStudio, which are organized into a gitrepositories
directory. All of the projects can be accessed as subdirectories from the gitrepositories
directory. If I need to share a project, I make the project accessible to other users on GitHub.
In my R code I reference external files as subdirectories from the project root directory, such as ./data/gen01.csv
.
edited Nov 22 at 17:03
answered Nov 22 at 15:48
Len Greski
2,9321420
2,9321420
Thanks. Is a "runbook" something specific or just a README equivalent?
– Pete900
Nov 22 at 16:51
1
You're welcome, @Pete900. A runbook is a set of procedures that is typically carried out by a technology Operations department. A README can be used as the place where a runbook is documented. Here is a very simple example that I wrote a few years ago for the Johns Hopkins Getting and Cleaning Data final assignment.
– Len Greski
Nov 22 at 16:57
add a comment |
Thanks. Is a "runbook" something specific or just a README equivalent?
– Pete900
Nov 22 at 16:51
1
You're welcome, @Pete900. A runbook is a set of procedures that is typically carried out by a technology Operations department. A README can be used as the place where a runbook is documented. Here is a very simple example that I wrote a few years ago for the Johns Hopkins Getting and Cleaning Data final assignment.
– Len Greski
Nov 22 at 16:57
Thanks. Is a "runbook" something specific or just a README equivalent?
– Pete900
Nov 22 at 16:51
Thanks. Is a "runbook" something specific or just a README equivalent?
– Pete900
Nov 22 at 16:51
1
1
You're welcome, @Pete900. A runbook is a set of procedures that is typically carried out by a technology Operations department. A README can be used as the place where a runbook is documented. Here is a very simple example that I wrote a few years ago for the Johns Hopkins Getting and Cleaning Data final assignment.
– Len Greski
Nov 22 at 16:57
You're welcome, @Pete900. A runbook is a set of procedures that is typically carried out by a technology Operations department. A README can be used as the place where a runbook is documented. Here is a very simple example that I wrote a few years ago for the Johns Hopkins Getting and Cleaning Data final assignment.
– Len Greski
Nov 22 at 16:57
add a comment |
up vote
2
down vote
Create an RStudio project and then reference all files with relative paths from the project's root folder. That way, all users will open the project and automatically have the correct working directory.
What is an "R project"?
– Ista
Nov 22 at 16:19
@Ista - see R projects in RStudio for details.
– Len Greski
Nov 22 at 16:48
@LenGreski the article you linked to never uses the phrase "R project" because "R project" is not a thing. "Rstudio project" is a thing, but Rstudio is not R, and "Rstudio project" is not "R project".
– Ista
Nov 22 at 17:01
@ista - I didn't intend to start an ontological debate here, I just explained what I thought Felipe intended by his post. I understand that projects are an RStudio feature, since the URL I posted refers to the RStudio website.
– Len Greski
Nov 22 at 20:02
1
Thank you @LenGreski, that is indeed what I meant to say. I edited my answer to reflect this.
– Felipe Gerard
Nov 26 at 19:00
|
show 1 more comment
up vote
2
down vote
Create an RStudio project and then reference all files with relative paths from the project's root folder. That way, all users will open the project and automatically have the correct working directory.
What is an "R project"?
– Ista
Nov 22 at 16:19
@Ista - see R projects in RStudio for details.
– Len Greski
Nov 22 at 16:48
@LenGreski the article you linked to never uses the phrase "R project" because "R project" is not a thing. "Rstudio project" is a thing, but Rstudio is not R, and "Rstudio project" is not "R project".
– Ista
Nov 22 at 17:01
@ista - I didn't intend to start an ontological debate here, I just explained what I thought Felipe intended by his post. I understand that projects are an RStudio feature, since the URL I posted refers to the RStudio website.
– Len Greski
Nov 22 at 20:02
1
Thank you @LenGreski, that is indeed what I meant to say. I edited my answer to reflect this.
– Felipe Gerard
Nov 26 at 19:00
|
show 1 more comment
up vote
2
down vote
up vote
2
down vote
Create an RStudio project and then reference all files with relative paths from the project's root folder. That way, all users will open the project and automatically have the correct working directory.
Create an RStudio project and then reference all files with relative paths from the project's root folder. That way, all users will open the project and automatically have the correct working directory.
edited Nov 26 at 18:58
answered Nov 22 at 16:01
Felipe Gerard
751418
751418
What is an "R project"?
– Ista
Nov 22 at 16:19
@Ista - see R projects in RStudio for details.
– Len Greski
Nov 22 at 16:48
@LenGreski the article you linked to never uses the phrase "R project" because "R project" is not a thing. "Rstudio project" is a thing, but Rstudio is not R, and "Rstudio project" is not "R project".
– Ista
Nov 22 at 17:01
@ista - I didn't intend to start an ontological debate here, I just explained what I thought Felipe intended by his post. I understand that projects are an RStudio feature, since the URL I posted refers to the RStudio website.
– Len Greski
Nov 22 at 20:02
1
Thank you @LenGreski, that is indeed what I meant to say. I edited my answer to reflect this.
– Felipe Gerard
Nov 26 at 19:00
|
show 1 more comment
What is an "R project"?
– Ista
Nov 22 at 16:19
@Ista - see R projects in RStudio for details.
– Len Greski
Nov 22 at 16:48
@LenGreski the article you linked to never uses the phrase "R project" because "R project" is not a thing. "Rstudio project" is a thing, but Rstudio is not R, and "Rstudio project" is not "R project".
– Ista
Nov 22 at 17:01
@ista - I didn't intend to start an ontological debate here, I just explained what I thought Felipe intended by his post. I understand that projects are an RStudio feature, since the URL I posted refers to the RStudio website.
– Len Greski
Nov 22 at 20:02
1
Thank you @LenGreski, that is indeed what I meant to say. I edited my answer to reflect this.
– Felipe Gerard
Nov 26 at 19:00
What is an "R project"?
– Ista
Nov 22 at 16:19
What is an "R project"?
– Ista
Nov 22 at 16:19
@Ista - see R projects in RStudio for details.
– Len Greski
Nov 22 at 16:48
@Ista - see R projects in RStudio for details.
– Len Greski
Nov 22 at 16:48
@LenGreski the article you linked to never uses the phrase "R project" because "R project" is not a thing. "Rstudio project" is a thing, but Rstudio is not R, and "Rstudio project" is not "R project".
– Ista
Nov 22 at 17:01
@LenGreski the article you linked to never uses the phrase "R project" because "R project" is not a thing. "Rstudio project" is a thing, but Rstudio is not R, and "Rstudio project" is not "R project".
– Ista
Nov 22 at 17:01
@ista - I didn't intend to start an ontological debate here, I just explained what I thought Felipe intended by his post. I understand that projects are an RStudio feature, since the URL I posted refers to the RStudio website.
– Len Greski
Nov 22 at 20:02
@ista - I didn't intend to start an ontological debate here, I just explained what I thought Felipe intended by his post. I understand that projects are an RStudio feature, since the URL I posted refers to the RStudio website.
– Len Greski
Nov 22 at 20:02
1
1
Thank you @LenGreski, that is indeed what I meant to say. I edited my answer to reflect this.
– Felipe Gerard
Nov 26 at 19:00
Thank you @LenGreski, that is indeed what I meant to say. I edited my answer to reflect this.
– Felipe Gerard
Nov 26 at 19:00
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433955%2frelative-paths-in-r-how-to-avoid-my-computer-being-set-on-fire%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
I didn't visit links 1 or 2 but
rprojroot
orhere
are my go-to options for projects (provided you've got one of the "markers" in the project directory (and you use projects like you should :-) With regard to scripts, the environment variable approach is pretty common for bash, python, etc scripts and is a fine option for R. For some automation tasks I have a "jobs" folder where the R scripts are and a "conf" folder where Renviron-like files sit and IreadRenviron
the associated one right at the top of the script. But for sharing projects, I see nothing wrong withhere::here()
– hrbrmstr
Nov 22 at 15:27
Create an environment variable for the root folder of each project you want to share in other ways than merely an interactive rstudio project.
– jwijffels
Nov 25 at 18:30