0

How do I determine the file name of the currently opened R script in R?

  • 2 Example scenarios where this would be helpful:

    1. Saving a current script with a new slightly different name each time an adjustment is made.

    2. My filename is too long, so I cannot read the entire script file name in the window header or in the device menu.

I'll frame the rest of the question on #2 b/c that's my current problem...

  • My scripts are saved with date/times at the end, so I need to see the end of the file name in order to tell which file I'm looking at.

The directory path is irrelevant to me because all my files are saved in the same directory.

  • I just need to know the full name of the specific script I have opened.

This question does NOT seem to have any solutions that answer my question as I do not want to source any additional files.

  • Ex: commandArgs() does not provide any relevant info for me

Also, the solution mentioned here did not work either.

    system("locate fileName.R")
    Warning message:
    running command 'locate fileName.R' had status 127 

Update: I'm using Rgui not RStudio.

Please help!

theforestecologist
  • 4,667
  • 5
  • 54
  • 91
  • ...use shorter names? – Hong Ooi Jul 12 '17 at 05:38
  • I knew someone would be a jerk and say that. Yeah, I get it. But that doesn't help me currently. Also, truncated window displays seems like a poor reason to quit providing my r scripts detailed/useful names that otherwise do not negatively impact my workflow... – theforestecologist Jul 12 '17 at 05:41
  • Maybe if you use [this](https://stackoverflow.com/a/13480153) answer and use a split like [here](https://stackoverflow.com/a/9704341). (split = dirname) – Numb3rs Jul 12 '17 at 06:00
  • In RStudio, when I click the "Source" button in the top right of an open script, I see a command in the console like `source("myopenscript.R")`. So you could source it and check the filename there? – Marius Jul 12 '17 at 06:00
  • 1
    What do you mean by "opened script"? – kangaroo_cliff Jul 12 '17 at 06:01
  • @DiscoSuperfly The script I currently have opened in my R window – theforestecologist Jul 12 '17 at 06:02
  • Why the downvotes?? It's a legitimate question that's on topic and provides examples of previous effort to answer on my own both on SO and externally. Instead of downvoting , why not help provide a useful answer? – theforestecologist Jul 12 '17 at 06:05
  • I'm not sure that "please help me remember what I was working on" is that useful a question for others, it seems fairly specific to your situation? – Marius Jul 12 '17 at 06:08
  • @Marius no it's not that specific. Others might want to quickly copy the file name of the given script for saving, sourcing, etc. I've just framed it in the context of not being able to read a long name b/c that's my current problem, and otherwise I'd get 5 comments asking "why do you want to know" -_- – theforestecologist Jul 12 '17 at 06:10
  • @Marius If I reframe in a different context, will you remove your downvote?? – theforestecologist Jul 12 '17 at 06:12
  • 1
    I think you need to specify what GUI/IDE you're using, in RStudio for example I think you can solve this with `rstudioapi::getActiveDocumentContext()` but in RGui I don't have any idea. – Marius Jul 12 '17 at 06:17
  • What about this? https://stackoverflow.com/questions/13645106/full-filename-of-the-currently-sourced-file – Roman Luštrik Jul 12 '17 at 09:15
  • To the close voter: what is so hard to understand? I'll break it down once more: I have a .R file saved on my windows computer. I had previously opened said .R file in R as a script file using File > Open Script. I now want to be able to extract the name of this loaded .R file (after it has already been opened in R). – theforestecologist Jul 13 '17 at 14:48

0 Answers0