Enable file autocompletion in my functions

Hi all,

I've been wondering if there is a way to enable file autocompletion in functions that we write?

For example, I sometimes specify a temp directory at the start of my script using a function, and later use it with load/save/etc., like this:

    tmpdir <- function(x) {
      paste0('/some/path/to/a/temp/dir/', x)


and later when I do load(tmpdir('<TAB> I would like to see some_data.rda be suggested by autocomplete, much like it would be if I manually typed the full path to my temp directory. Any ideas?

Not sure if this is RStudio specific, might be.


This has nothing to do with your question, but why don't you use the tempfile and tempdir functions?

This is for cases when I can't use just any temporary directory. We have different file systems that are optimized for different needs of size, speed, and security. I may be abusing the notion of the term "temp" here, these are more like unfinished work in progress data pieces.

If you are hoping to use this sort of feature inside RStudio, then yes I think this is an RStudio-specific question (and it might be a good idea to move this post to the RStudio IDE category). Are you imagining this as a feature request, or something you want to implement yourself (such as an RStudio AddIn)? I’m not sure there’s enough exposed by rstudioapi right now to do exactly what you describe, but it might be possible to make an add-in that still helped automate this process.

That said, the path completion system will complete any file name within the current Project even if it’s in a sub-directory, so if your not-really-temp temp folders are within an RStudio Project structure, that might help?

Really I'm talking about any arbitrary directory within a file system, including spaces where files are shared between users, so not necessarily within a project directory. But I think your point gets to the heart of it - usually we want everything to be contained relative to the project directory so we are not referring to things like "C:\Users\brianstamper\...\my_data.csv" because if we share that code then it isn't going to work for someone else. But sometimes we need to refer to (or create) files that are stored in fixed locations in some common file system, and to keep that information from being buried somewhere deep in the code I like to somehow declare the location near the beginning of my script, either in a function like above or even just as a character string.

Another way I have done this is through symlinks, like

if (!file.exists('data')) file.symlink('/some/common/data/location/', 'data')

But to people not familiar with symlinks that may be a little cryptic.

Obviously this isn't a major issue and there are some workarounds, I just came to the thought because I was writing something where I was doing a lot of file creating/reading in one of these directories and was pining for tab completions.

I see what you mean! And when I can't escape referencing some absolute path, I also try to put it up top in one place and use functions/variables to make later paths relative to that one reference point. (If you're doing a lot of this directory spelunking, you might like the fs package!)

But I suspect it would be pretty complex to implement tab completion that can analyze arbitrary code to understand that in the context of this specific function it should be indexing a completely different directory.

This is sort of a terrible kludge, but if you're in the midst of writing a bunch of code that's using one of your absolute-path-abstracting functions, you could temporarily run setwd() at the console to change the working directory to the root of your arbitrary location. This will make RStudio's file path autocomplete start looking in that directory, instead of in your project. You'd of course have to remember to change the working directory back before actually running any code, but it might be worth it if you need to spend a few minutes typing a whole lot of file names into a script.

Have you seen FuzzyFilePath for Sublime Text? Definitely doesn't work in RStudio, and I don't know if there would be any additional obstacles because of the inner-workings. Nevertheless, thought I'd pass it along for possible inspiration: