0

I'm trying to create my own sublime-build in Sublime Text 3, since the default build in knitr package (https://sublime.wbond.net/packages/knitr) doesn't work (for me). I modified the one in knitr package as follows:

{
    "selector": "text.html.markdown.knitr",
    "working_dir": "${project_path:${folder}}",
    "cmd": ["Rscript", "-e", "library(knitr); knit('$file')" ],
    "shell": true
}

but get the error:

Error: '\G' is an unrecognized escape in character string starting "'C:\G"
Execution halted
[Finished in 0.4s with exit code 1]
[cmd: ['Rscript', '-e', "library(knitr); knit('C:\\GitHub\\Projects\\test\\testLaTeXing\\knitrRmd\\testRmd.Rmd')"]]

obviously some windows path escape issue, but how can I fix this, when I want to acces file path dynamically?

I'm working on windows7.

adibender
  • 7,288
  • 3
  • 37
  • 41

2 Answers2

1

Try setting your "cmd" line to this:

"cmd": ["Rscript", "-e", "library(knitr); knit('${file/\\\\/\//}')" ],

Basically, it's a regular expression to match two \ characters and replace them with one / character. Each \ and / needs to be escaped by a \, hence the chicken scratch.

I'm not on Windows to test, but theoretically this should work. If it doesn't, another option to try is

"cmd": ["Rscript", "-e", "library(knitr); knit('${file/\\\\/\\/}')" ],

where it's replacing the \\ with a single \.

Please let me know how it works, I'm interested to know!

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • doesn't quite work, first option removes both backslashes (after C:, rest remains), second erases "library(...) completely – adibender Dec 24 '13 at 05:21
  • oops, missed a final `/`. How about `${file/\\\\/\//}` or `${file/\\\\/\\/}`? – MattDMo Dec 24 '13 at 16:12
  • @adibender - see my edit and comment above - does the new version work? – MattDMo Dec 24 '13 at 18:39
  • unfortunately it doesn't work either, same problem as before. I played around a little bit and this `${file/\\\\/\/g}` replaces all double backslashes with empty string, but I can't get replacement to work.. – adibender Dec 25 '13 at 13:03
  • ok, this seems to work: `"cmd": ["Rscript", "-e", "library(knitr); knit('${file/\\\\/\\/\/g}')" ],` – adibender Dec 25 '13 at 13:07
1

This is what I finally cooked up, thanks to @MattDMo.

{
    "selector": "text.html.markdown.knitr",
    "working_dir": "${project_path:${folder}}",
    "cmd": [ "Rscript -e \"library(knitr); knit('$file', output='$file_path/$file_base_name.md')\"" ],
    "shell": true,

    "windows":
    {
        "selector": "text.html.markdown.knitr",
        // "working_dir": "${project_path:${folder}}",
        // "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
        "cmd": ["Rscript", "-e", "library(knitr); knit('${file/\\\\/\\/\/g}', output='${file_path/\\\\/\\/\/g}/$file_base_name.md' )" ],
        "shell": true
    }
}

where the default command is from the knitr sublime package (https://github.com/andrewheiss/SublimeKnitr/blob/master/knitr-Markdown.sublime-build)

adibender
  • 7,288
  • 3
  • 37
  • 41