Relief from backslash irritation in R for Windows

前端 未结 9 2320
没有蜡笔的小新
没有蜡笔的小新 2021-02-10 10:41

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

相关标签:
9条回答
  • 2021-02-10 11:14

    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
    
    0 讨论(0)
  • 2021-02-10 11:16

    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.

    0 讨论(0)
  • 2021-02-10 11:18

    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...

    0 讨论(0)
  • 2021-02-10 11:24

    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.

    0 讨论(0)
  • 2021-02-10 11:26

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

    Local $text1 = ClipGet()
    $text2=StringReplace($text1,"\","/")
    ClipPut($text2)
    
    0 讨论(0)
  • 2021-02-10 11:27

    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.

    0 讨论(0)
提交回复
热议问题