[R] How to automatically set R to read the files from a specific location
@v|gro@@ @end|ng |rom ver|zon@net
Thu Mar 3 02:02:03 CET 2022
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.
From: Bert Gunter <bgunter.4567 using gmail.com>
To: Paul Bernal <paulbernal07 using gmail.com>
Cc: R <r-help using 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.
"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 using gmail.com> wrote:
> 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.
> [[alternative HTML version deleted]]
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
More information about the R-help