17

I am writing an R script in Rstudio, and I would like some way to get the name of the file the code is written in. The reason why I want to do this is I want a piece of code I can stick at the bottom of all my scripts so that my results are automatically emailed to me using the sendmailR package.

I'd like my attachment name to have the structure filename_date where filename is the name of the file being executed and date is the time of the execution. Is there a way of doing this, or will I have to type out the name of the file by hand every time I run a new script.

A similar question was asked here, but because the use was different the workaround solution never provided an answer. I actually don't need the location, just the filename and I think this is what should make it doable (but maybe not)

Find location of current .R file

Rscript: Determine path of the executing script (asks basically the same question but wants the whole path, no accepted answer)

Community
  • 1
  • 1
WetlabStudent
  • 2,556
  • 3
  • 25
  • 39
  • How are you executing the script? Using `Rscript?` use `run all` in `RStudio`? – mnel Aug 16 '13 at 02:41
  • 1
    This is no different than the second question you linked to. Once you have the full path, getting the file name just requires a simple regex. – joran Aug 16 '13 at 02:54
  • thanks, that question didn't have an accepted answer though, so I just assumed it wasn't answered. Its really important to accept answers, especially when these is so many unrelated answers given on that thread. – WetlabStudent Aug 16 '13 at 19:30
  • @joran could you elaborate which answer give the path with file name? I could only find solutions producing the former. BTW, I am working on Windows 10 – mjs Dec 04 '19 at 11:21

2 Answers2

5

Another workaround would be to source your scripts from a master script,

script <- "mycode.R"
source(script)
...
doOtherThings(script)

and you could loop through all scripts you have, send emails about them, whatever.

Remko Duursma
  • 2,741
  • 17
  • 24
0

I don't know of a way to have R give you the script name. But you could include it at the top of the script - like so:

Script.Name<-c("current name")

Then using the Sys.time() and paste() commands you could make the object you want:

Final.Name<-paste(Script.Name, Sys.time(), sep="_")

If you just want the date of the run, use Sys.Date() instead of Sys.time().

John Paul
  • 12,196
  • 6
  • 55
  • 75
  • 3
    '@John Paul' how that a solution? As I undestand the question it was about how to find it out in a generic way. – mjs Dec 04 '19 at 11:28
  • @mjs Its been a long time since I wrote this, but I think the idea was that in all the scripts the OP was working on, there would be a `Script.Name` variable defined, and then a standard line of code would paste together the desired text. Its not automatic, but if you are writing the scripts anyway it did not seem too onerous. – John Paul Dec 04 '19 at 12:54
  • 1
    thanks for clarifying. In Matlab there is the magic 'mfilename' command - 'File name of currently running code'. Why is it so difficult to do the same in R??? – mjs Dec 04 '19 at 20:47