0

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.

Community
  • 1
  • 1
m-dz
  • 2,342
  • 17
  • 29
  • 1
    also checkout autohotkey. I used to use something that pasted the clipboard with backlashes fixed whenever I wrote "rpaste" – OganM Feb 18 '17 at 01:13
  • There is no "\" in "\M". Check with nchar("\M"). Answer == 1. – IRTFM Feb 18 '17 at 07:23
  • @42-, I really don't get you, it is clear to me, that `'\M'` for R is not the same as you see here, but where is the point? The problem is clear and the solution is even clearer. If it is your down vote for the Q and A, would you please elaborate or propose a better solution to this problem? – m-dz Feb 18 '17 at 11:07
  • @OganM, thanks, you one seems to see where the problem is! I have found an [autohotkey solution here](http://stackoverflow.com/questions/1407238/relief-from-backslash-irritation-in-r-for-windows), but the drawback is you need to have the autohotkey software installed, am I right? – m-dz Feb 18 '17 at 11:08
  • @m-dz. I don't downvote except when questioner persistently fail to face reality after efforts at correction . (And I was wrong about `nchar("\M")`. That's just an error.) My point was that you would see instead "\\M" in an R session. If the backslash is actually there in a file, then you will see it as ''C:\\Marketing" when it is read into an R character object. So you would NEVER do this as R code: `path_parse('C:\Marketing')` – IRTFM Feb 18 '17 at 16:02
  • You are missing the point, it is not about the string R objects, it is about pasting a Windows path (e.g. `C:\folder`) into a file and then running it. Have you ever tried that? It is a completely different situation than you described above. – m-dz Feb 18 '17 at 23:31

1 Answers1

0

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

m-dz
  • 2,342
  • 17
  • 29
  • Any function replacing characters (`chartr()`, `gsub()` etc.) should work here. I have tried the addin without `chartr` and, as expected, it is just adding single quotes around the text, which is not enough here. – m-dz Feb 18 '17 at 23:27
  • A minor amendment, `chartr()` changed to two `gsub()`'s to deal with escaped backslashes. – m-dz Mar 02 '17 at 09:52