Relief from backslash irritation in R for Windows

前端 未结 9 2313
没有蜡笔的小新
没有蜡笔的小新 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:27

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

    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.

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

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

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