5

Early in my R life I discovered the pain of R and windows being on different pages when it came to the separator between directories and subdirectories. Eventhough I know about the problem, I am still pained by manually having to put a backslash in front of all my backslashes or replacing all of them with forward slashes.

I love copying a path name or an entire filename with any one of several applications that I have running on my computer (eg. XYPlorer, Everything by voidtools) and then pasting it into Tinn-R. Is there anyway that I could automate the task that I am currently doing manually.

  • Is there a setting in Tinn-R?
  • Is there a setting in R?
  • Is there a autohotkey script that could do it for me by default?

Background for those who don't know what I am talking about

Quoting from R for Windows FAQ, Version for R-2.9.2, B. D. Ripley and D. J. Murdoch

Backslashes have to be doubled in R character strings, so for example one needs `"d:\R-2.9.2\library\xgobi\scripts\xgobi.bat"'. You can make life easier for yourself by using forward slashes as path separators: they do work under Windows

Farrel
  • 10,244
  • 19
  • 61
  • 99

9 Answers9

7

I wrote a autohotkey script that is triggered by typing "rfil " - without the inverted commas.

:O:rfil:: ;replaces backslashes with forward slashes in a file name that is stored on the clipboard
StringReplace,clipboard,clipboard,\,/,All
send %clipboard%
return

If anyone can tell me a quicker way than using the send command I would appreciate it. I have an autohotkey script running all the time on all my computers so I did not have to download new software in order to run this script. I simply added it to my default script file.

I will be happy to explain what I did if you want me to.

Farrel
  • 10,244
  • 19
  • 61
  • 99
2

ClipPath adds right-click menu options to choose which kind of slash you want to paste.

Via Getting Genetics Done, which looks like it could be a useful resource for R users in general.

Matt Parker
  • 26,709
  • 7
  • 54
  • 72
  • Yip. ClipPath looks like a really easy solution. Have you seen if it runs in Vista or Windows 7. I noticed the line "Latest Version 2.1: Download File Size: 30kb (Supported OS: Win95/98/Me/NT4.0/2000/XP ) " Now that I know of this no brainer solution let me look if something exists similar to it in Tinn-R. – Farrel Sep 14 '09 at 21:17
  • Haven't tested it, sorry - I just saw it in that blog entry and immediately thought of this question. Hope it works! It does seem like it wouldn't be too challenging to add such a feature to Tinn-R, if the devs sign on. – Matt Parker Sep 14 '09 at 22:26
  • I looke around tinn-r and found no built in function to do it. I have not tried writing macros in tinn-r so I am going to turn to autohotkey. – Farrel Sep 16 '09 at 21:32
2

I've adapted the following autohotkey code shared to replace all backslashes with forward slashes whenever I paste anything in RStudio. There are pros and cons to this approach.

Pros: You don't have to think about it. The code will only run if the active window is RStudio.

Cons: The code is called every time you paste something in R. Each time it attempts to find backslashes and replace them with forward slashes.

GroupAdd, R, RStudio

;replaces backslashes with forward slashes in a file name that is stored on the clipboard
#IfWinActive ahk_group R
   ^v::
      StringReplace,clipboard,clipboard,\,/,All
      send %clipboard%
   return
#IfWinActive
user1583016
  • 79
  • 1
  • 11
1

This is AutoIt code which does the same thing (replaces \ with /).

Local $text1 = ClipGet()
$text2=StringReplace($text1,"\","/")
ClipPut($text2)
Stat-R
  • 5,040
  • 8
  • 42
  • 68
1

I use search & replace, but of course, it's not completely automatic and you have to take care not to replace "\t" or "\n".

Etienne Racine
  • 1,323
  • 1
  • 11
  • 25
  • Yes, I have done that too but it is a bit of a nuisance unless I could make a macro of it in Tinn-R. Do you use Tinn-R? – Farrel Sep 11 '09 at 15:29
  • Yes, a macro would be nice. I don't use Tinn-R alot anymore, but I still have that problem with other editors. Maybe someone speaking fluent regular expression could give one that handle the most common cases of replacements and mostly not replace every \. – Etienne Racine Sep 11 '09 at 19:59
1

Not exactly the answer you're looking for but R has its own shell scripting functions which I often use:

list.files(,full=TRUE) [returns full path with appropriate separators]

file.path() [joins with OS-specific separator]

and so on...

hatmatrix
  • 42,883
  • 45
  • 137
  • 231
1

You could create a wrapper function around all path names:

> replace.slash <- function(path.name) gsub("\\\\","/",path.name)
> path.name <- "c:\\tmp\\"
> replace.slash(path.name)
[1] "c:/tmp/"

[Edit]: Thanks Hadley. I corrected the error there.

Incidentally, I found this very useful discussion on this subject.

Shane
  • 98,550
  • 35
  • 224
  • 217
0

why not create a function that checks the OS and returns the proper file separator (the java solution i believe)?

file_sep <- function(){
ifelse(.Platform$OS.type == "unix", "/", "//")
}
file_sep()

you can pick a shorter name if you like. The big flaw here is that you have to paste together file paths, but it's still worth it long term if you're working on big projects.

Dan
  • 6,008
  • 7
  • 40
  • 41
0

Expanding slightly on @Farrel's code (thanks so much!), here's my AutoHotkey script that will get the full file path of any file I select and then (if desired) swap the slashes for better use in R.

The script also replaces any mapped drives with the full network path. To use this you'll need to edit this script to look for your particular mapped drives and then replace those drive letters with the full path.

It took a bit to set up but it's so useful. I use this every day.

;If Windows explorer is active...
#IfWinActive ahk_class CabinetWClass 

; ALT + F - Get the filepath to the file
!f::
SendInput, ^c
Sleep 100
Clipboard := Clipboard
return

;Check for and replace mapped drive names on the clipboard with full file paths
If InStr(Clipboard,"X:\",1) {
Clipboard := "\\SERVER_NAME\g$\" SubStr(Clipboard,4, (StrLen(Clipboard )))
} else if InStr(Clipboard,"K:\",1) {
Clipboard := "\\SERVER_NAME\Data\" SubStr(Clipboard,4, (StrLen(Clipboard )))
} else if InStr(Clipboard,"Q:\",1) {
Clipboard := "\\SERVER_NAME\Data\" SubStr(Clipboard,4, (StrLen(Clipboard )))
} else if InStr(Clipboard,"L:\",1) {
Clipboard := "\\SERVER_NAME\" SubStr(Clipboard,4, (StrLen(Clipboard )))
} 
Return

; ALT + S - Replaces backslashes with forward slashes (helpful for R)
; Source: https://stackoverflow.com/questions/1407238/relief-from-backslash-irritation-in-r-for-windows
!s::
StringReplace,clipboard,clipboard,\,/,All
send %clipboard%
return

; Scripts below this point will run in any active window
#IfWinActive
Ryan Bradley
  • 627
  • 6
  • 9