Error in custom sublime build for knitr markdown

427 views Asked by At

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.

2

There are 2 answers

0
adibender On BEST ANSWER

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)

5
MattDMo On

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!