简体   繁体   中英

Is it possible to use Sublime text 2 to work in with R-markdown-files?

I recently discovered how neatly you can work with R-markdown(.Rmd) files. Since I prefer to have all my work in Sublime text 2 I was glad to find this package . While the sending to R works nicely thanks to this post , for some reason sublime doesn't compile my markdown file. After strg+shift+b instead of an .md-file I get this error:

Traceback (most recent call last):
  File ".\sublime_plugin.py", line 337, in run_
  File ".\exec.py", line 154, in run
TypeError: __init__() got an unexpected keyword argument 'shell_cmd'

The test file has this form

% Test document

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do `eiusmod` tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo.

```{r example_chunk, fig.width=5, fig.height=3}
x <- rnorm(100)
y <- rnorm(100)
plot(y ~ x, pch=20)
``` 

I use python 2.7. and installed all the dependencies for knitr.

The build system from that package contains a Run variant that is not compatible with Sublime Text 2, which is what is causing your issue. That seems to be an oversight on the part of the package author, so it may be worth filing a bug report.

In the meantime, you can fix the problem on your own by making a modification to the build file. If you open Packages\\SublimeKnitr\\knitr-Markdown.sublime-build (use PackageResourceViewer if you didn't manually install the plugin), you will see the following:

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

  "variants":
  [
    {
      "name": "Run",
      "working_dir": "$file_path",
      "shell_cmd": "Rscript -e \"rmarkdown::render(input = '$file')\""
    }
  ]
}

The issue is that Sublime Text 2 does not support shell_cmd (that is a Sublime Text 3 addition, it would seem). The main build uses cmd and sets shell to true to run through the shell, which is the correct way to go about this in Sublime Text 2.

If you modify the variant by changing shell_cmd to cmd and wrapping the string argument in square brackets and including the shell line:

"cmd": [ "Rscript -e \"rmarkdown::render(input = '$file')\"" ],
"shell": true

It should work for you. Note that the build file is JSON, so it's important that everything be properly comma separated.

The addition of the "shell": true portion may not be needed here. It might be retained from earlier in the build. I'm not familiar with how Sublime Text 2 handles that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM