Skip to content

How to automatically set R to read the files from a specific location

6 messages · Paul Bernal, Bert Gunter, Jeff Newmiller +2 more

#
Dear friends,

I am working on an assignment using R, and I would like to set my R code so
that R automatically recognizes where the files that need to be read are
without having to use the absolute path?
The idea is that when I send my .R script and my professor receives it, he
can just execute the code without running into any issues.

Thanks, beforehand, for your valuable feedback.

Cheers,
Paul
#
I may well be wrong, but I believe you will need to give more info on
where the files are located (e.g. on a remote server, on a website, a
local machine, ...), whether they consist only of data or whether
there are packages with functions that need to be used, etc.

A better approach might be to combine data and files into a package,
put the package where you all have access, and then give your prof the
code to download it from there.

Others may have better ideas, so do wait for more responses.

Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Wed, Mar 2, 2022 at 2:55 PM Paul Bernal <paulbernal07 at gmail.com> wrote:
#
1) make a working directory. Put all related files (including your R file(s) in that directory or in subdirectories.
2) Set your working directory as "current" before starting R. If you or anyone who wants to use your code does this, no actions will be needed in the script to "change" the directory. This is straightforward at the shell using "cd", and GUI file managers will normally do this automatically for you if you "open" a file using an action that triggers starting R from there. RStudio uses the .Rproj file to serve this purpose, so if you use RStudio then always create a Project for each separate "project" you work on.
3) Use relative paths in your code. You can access a csv file in your working directory as "filename.csv" or in a subdirectory data_dir of your working directory using "data_dir/filename.csv".
4) Send a zip archive of your "project" directory/contents to the recipient so the relative locations of files is maintained. Or consider using GitHub (e.g. [1]) to share and track changes.

Note that sometimes you may not want to include the data in your project directory because switching data sets will be common. In that case you cannot achieve this goal, but you can use a global variable to specify your data directory path and use file.path( dtadir, fname ) wherever your specify a file for input. The recipient then only needs to alter one line of your code.

[1] https://happygitwithr.com/
On March 2, 2022 2:54:43 PM PST, Paul Bernal <paulbernal07 at gmail.com> wrote:

  
    
#
All reasonable answers so far to the question but what is missing here is what the ASSIGNMENT is. Are the student(s) and professors/teachers sharing an environment on a multi-tasking machine or some networked situation or each is alone?
If I were teaching a class, I might be supplying the data files to be used and hence have a local copy on my machine in a designated area. This same issue comes up when someone asks me to do some analysis for them, sometimes without my having a copy of the data file, just knowing details about what it contains, such as column headers in a CSV.
So it is possibly something the teacher should have taught and set up. For example, they can tell you the file is in /usr/tmp (or whatever shared area) or a networked alternative where you are asked to copy the file into some local directory and setwd() into that directory and the? program is to be run from THAT directory. That makes it simple as each user can place the file anywhere they wish.
An alternative, as pointed out, is to ask the person to edit one line of? the R script being supplied such as:
WHERE <- "C:/..."setwd(WHERE)
Or, as pointed out, R has utilities to easily splice together a filename, as simple as:
FILE <- "xyz.csv"FULL_NAME <- paste(WHERE, "/", FILE)
and in operations on the file, use FULL_NAME.
Whatever method is used can be implemented best if you get instructions. If the Professor wants to see your program, the above and variants should work. If they just want to see the output file for correctness, and do not plan on running the R script, then attaching the output results in an email might do.
NOTE, I used slash notation for directory structures above as it is most portable. Of course, if everyone is on something like MS Windows, you have the option of doubled backslashes and so on. Some R utilities handle this better and might be preferable to what I show as a SIMPLE example using base R.

-----Original Message-----
From: Bert Gunter <bgunter.4567 at gmail.com>
To: Paul Bernal <paulbernal07 at gmail.com>
Cc: R <r-help at r-project.org>
Sent: Wed, Mar 2, 2022 6:07 pm
Subject: Re: [R] How to automatically set R to read the files from a specific location

I may well be wrong, but I believe you will need to give more info on
where the files are located (e.g. on a remote server, on a website, a
local machine, ...), whether they consist only of data or whether
there are packages with functions that need to be used, etc.

A better approach might be to combine data and files into a package,
put the package where you all have access, and then give your prof the
code to download it from there.

Others may have better ideas, so do wait for more responses.

Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Wed, Mar 2, 2022 at 2:55 PM Paul Bernal <paulbernal07 at gmail.com> wrote:
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
#
Just a silly? thought, Paul. If this is really for a class and the file(s) being used are small enough, you can play a game where the data is already in the R program and is simply written into a file at the start in the current directory or a designated area. If that succeeds, you can then have the program start as if from scratch and read it back in and continue.?
An alternative if the file is not local but in a known place that can be reached, is code that copies it here ...
Of course in real life with existing huge files, not so much a good idea. But many programming assignments to students use smaller data sets. Some require having loaded a package and then have an access method to make the data visible and loaded. As long as the professor and others all have the same package, ...

-----Original Message-----
From: Paul Bernal <paulbernal07 at gmail.com>
To: R <r-help at r-project.org>
Sent: Wed, Mar 2, 2022 5:54 pm
Subject: [R] How to automatically set R to read the files from a specific location

Dear friends,

I am working on an assignment using R, and I would like to set my R code so
that R automatically recognizes where the files that need to be read are
without having to use the absolute path?
The idea is that when I send my .R script and my professor receives it, he
can just execute the code without running into any issues.

Thanks, beforehand, for your valuable feedback.

Cheers,
Paul

??? [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
#
YAA (Yet Another Approach)
If the data is part of the assignment then the instructor presumably has a
copy of the data file(s).
Your script could get the location of these files via an environment
variable using Sys.getenv().
The instructor would need to set the environment variable prior to running
your script.
Your script should issue an error if the environment variable has not been
set.


On Thu, Mar 3, 2022 at 3:12 AM Avi Gross via R-help <r-help at r-project.org>
wrote: