Search code examples
rrstudioadd-infilepathpath-separator

RStudio addin to parse input string literals by changing backslashes (as in 'C:\Marketing') into forward slashes


Clarification

Solving this problem with a function was not possible, which @IInspectable kindly confirmed in the comments under the old, deleted, question. Long story short: backslashes need to be escaped in string literals, i.e. a 'C:\\Marketing' needs to be passed instead of 'C:\Marketing'. I am fully aware of this and this question and my answer below is not about the error thrown, manual solutions or using other software (e.g. AutoHotkey) as given in other related questions.

Please note the question is a follow up of another one, thank you @IInspectable for your valuable comments.

"Windows" paths...

I do work with Windows formatted paths (like C:\Marketing) quite often by pasting them into R code. Changing the backslashes every time is highly annoying, thus I have attempted to write a parsing function for them. The desired usage was to use this function in the code with the copied path as a parameter, i.e. path_parse('C:\Marketing'). My first approach, based on @Tyler Rinker answer from here, was as following:

path_parse <- function(path = 'clipboard') {
  path <- if (path == 'clipboard') readClipboard() else path
  return(chartr('\\', '/', path))
}

and it was working nicely with the path copied to the clipboard, but unfortunately was throwing an error message an unrecognized escape in character string, as in e.g. this question, when used with pasted string literals:

> path_parse('C:\Marketing')
Error: '\M' is an unrecognized escape in character string starting "'C:\M"

Of course manually changing all the input string literals (as in passing 'C:\\Marketing' or 'C:/Marketing' instead of 'C:\Marketing') is not an option here, as this is exactly why I want to automate it somehow.

One closely related question to this one is here, but it is based on Tinn-R GUI and AutoHotkey software, which I do not want to install.

Addins to the rescue

The solution was closer than I thought, an RStudio addin (see e.g. here how to use them) parsing the selected text (i.e. the pasted "Windows" path) by changing all backslashes into forward slashes and inserting it back into the code. Code and screenshots in the answer below.


Solution

  • A working solution (or rather a workaround, as initially I wanted to solve this with a function)

    As I wrote in the question above, solving this problem with a function was not possible, so I tried a different solution, which is doing exactly what I wanted, and might be useful for others as well. I have built an addin package (a nice article by RStudio) with the following function:

    #' Parse selected "Windows" path to an "R-usable" one
    #'
    #' @return
    #' @export
    #' @importFrom rstudioapi getActiveDocumentContext
    #' @importFrom magrittr '%>%'
    #'
    #' @examples
    path_parse <- function() {
      getActiveDocumentContext()[['selection']][[1]][['text']] %>%
      { gsub('\\\\', '/', .) } %>%
      { gsub('//', '/', .) } %>%    # To remove double f. slashes
      { ifelse(check_for_quotes(.), insertText(.), insertText(paste0('\'', ., '\''))) }
    }
    ### Old function:
    # path_parse <- function() {
    #   getActiveDocumentContext()[['selection']][[1]][['text']] %>%
    #   { chartr('\\', '/', .) } %>% { insertText(paste0('\'', ., '\'')) }
    # }
    

    and assigned a Ctrl+Alt+P shortcut to it.

    What it does is, basically, parsing the selected text (i.e. the pasted "Windows" path) by changing all backslashes into forward slashes and inserting it back into the code:

    Addin screenshots