File Watchers in RStudio?

I have been using the excellent styler package, and its associated RStudio Add-Ins to automatically format R code. Along with CI and pre-commit hooks, this makes a great workflow to enforce standards without draconian barriers to code contributions.

However, one thing that is missing is a "File Watcher" capability in RStudio. When working in PyCharm IDE, I can set up a watcher to run a code formatter on a file every time I save. I haven't been able to tweak RStudio to get similar functionality working-- while I can use a hotkey to style my active file with styler (I believe this can be done with formatR as well), it is another step to remember in addition to saving the file.

Is there some way to set up File Watcher-esque functionality within RStudio that I haven't discovered? Or, does anyone have other suggestions for how I could set up a workflow that makes saving a file and formatting a file one and the same?


You could probably do it with later package. There was recently a YouTube video where later was demonstrated. And the example showed R studio plotting a dataframe whenever its contents changed.

Interesting. Can you post the link to this video? styler is quite slow (I may say that, I wrote most of it) but because of caching of top level expressions (like assignments, function declarations, piped expressions), styling of files with minor diff to previously styled files (e.g. all changes in one function declaration) should be relatively fast.

1 Like

Thanks for contributing to styler, I use it everyday !

@lorenzwalthert, you were holding out on me (or more likely, I didn't ask my question clearly enough)!

I was looking in the source code for styler, and noticed that you've got functionality all set up to utilize the RStudio API to save upon styling if the environment variable "save_after_styling" was set to TRUE. So, I did the following:

  1. Via usethis::edit_r_profile(), I added the line

    Sys.setenv(save_after_styling = TRUE)

    to my .Rprofile file.

  2. In RStudio, click through Tools -> AddIns -> Browse Add-Ins. That brings up a list of available add-ins, and in the bottom-lefthand side of the box there is a "Keyboard Shortcuts..." button. Click that, and you can add a hotkey for "Style active file." Set this hotkey to whatever you want, I set mine to Ctrl + Alt + S.

    As @lorenzwalthert notes below, be aware that if you use this keyboard shortcut on a non-R file (aka anything that's not .R, .Rmd, or .Rnw), this command will fail and your file will not save.

  3. That's it! Restart R and your active file in RStudio should be automatically styled and saved with Ctrl + Alt + S.


if that's a solution for you - then yes. I don't think this works well, because it will apply the addin to any file, i.e. also yaml, plain text, latex, python and other, and always fail. And then not save. So basically you can't use Ctrl + S anymore to save arbitrary files. At least that's my apprehension. Can you confirm this? If that's true, then you should edit your answer to include this caveat or even remove the solution tag from it. If not, I am happy it works for you :smile:

Very good point. I'll change the suggested shortcut above and make these pitfalls clear

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.