Associate other file extensions with RStudio/R

I want to be able to save my R code with two different extensions:

  • ".R" for R code that will be incorporated into R packages
  • ".Rscript" for R code that is meant to be used in a semi-interactive manner when I am sitting at the computer doing things

When I try to save files with the ".Rscript" extension, I get warning messaging asking if I want to "confirm change file type". If I confirm this change, the file saves as expected, but the code/text is treated as plain text and loses all of the editor theme colors.

Is there a way to associate custom file extensions with RStudio so that files with the ".Rscript" extension will be treated as code and colored appropriately? Thanks.

The coloring (aka syntax highlighting) is a property of the IDE or application in which you are viewing the document, not of the document itself. RStudio does its best to detect the filetype, but if it's a different extension, you might have to override it manually. You can do this by clicking the button in the bottom right-hand corner of the Source pane and selecting the type you want it to be interpreted as (in this case, R Script)

2 Likes