Paul Bernal
2022-Mar-02 22:54 UTC
[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]]
Bert Gunter
2022-Mar-02 23:07 UTC
[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:> > 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.
Jeff Newmiller
2022-Mar-02 23:27 UTC
[R] How to automatically set R to read the files from a specific location
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:>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.-- Sent from my phone. Please excuse my brevity.
Avi Gross
2022-Mar-03 01:08 UTC
[R] How to automatically set R to read the files from a specific location
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. [[alternative HTML version deleted]]