├── .no-sublime-package ├── .gitignore ├── messages ├── 1.1.1.txt ├── 1.1.2.txt ├── 1.2.0.txt ├── 1.1.0.txt └── install.txt ├── messages.json ├── Default.sublime-commands ├── test_file.Rmd ├── knitr-rnw-chunk.sublime-snippet ├── knitr-md-chunk.sublime-snippet ├── test_file.Rnw ├── knitr-Markdown.sublime-build ├── Default (OSX).sublime-keymap ├── Default (Windows).sublime-keymap ├── Default (Linux).sublime-keymap ├── chunksend.py ├── knitr-Rnw.tmLanguage ├── knitr-Markdown.tmLanguage └── README.md /.no-sublime-package: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.pyc 3 | *.cache 4 | .R* 5 | figure/* 6 | -------------------------------------------------------------------------------- /messages/1.1.1.txt: -------------------------------------------------------------------------------- 1 | ------------------- 2 | Sublime knitr 1.1.1 3 | ------------------- 4 | 5 | Fixed: 6 | * There was a typo in the keymap files that prevented the snippets from working. Those work now :) -------------------------------------------------------------------------------- /messages.json: -------------------------------------------------------------------------------- 1 | { 2 | "install": "messages/install.txt", 3 | "1.1.0": "messages/1.1.0.txt", 4 | "1.1.1": "messages/1.1.1.txt", 5 | "1.1.2": "messages/1.1.2.txt", 6 | "1.2.0": "messages/1.2.0.txt" 7 | } 8 | -------------------------------------------------------------------------------- /messages/1.1.2.txt: -------------------------------------------------------------------------------- 1 | ------------------- 2 | Sublime knitr 1.1.1 3 | ------------------- 4 | 5 | Fixed: 6 | * Building `.Rmd` files would fail if the file was not inside the main project or folder. This update fixes that. (Thanks to @jlegewie!) -------------------------------------------------------------------------------- /messages/1.2.0.txt: -------------------------------------------------------------------------------- 1 | ------------------- 2 | Sublime knitr 1.2.0 3 | ------------------- 4 | 5 | Added: 6 | * Build variant that uses `rmarkdown::render()` to automatically create an HTML file in addition to the Markdown file (thanks @mmarascio!) 7 | 8 | Fixed: 9 | * `.Rmd` files are now built using UTF-8 encoding. -------------------------------------------------------------------------------- /Default.sublime-commands: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "caption": "knitr: Send Chunk to R", 4 | "command": "knitr_send_chunk" 5 | }, 6 | { 7 | "caption": "knitr: Move to Next Chunk", 8 | "command": "knitr_next_chunk" 9 | }, 10 | { 11 | "caption": "knitr: Move to Previous Chunk", 12 | "command": "knitr_prev_chunk" 13 | } 14 | ] 15 | -------------------------------------------------------------------------------- /test_file.Rmd: -------------------------------------------------------------------------------- 1 | % Test document 2 | % Author 3 | % Date 4 | 5 | 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. 6 | 7 | ```{r example_chunk, fig.width=5, fig.height=3} 8 | x <- rnorm(100) 9 | y <- rnorm(100) 10 | plot(y ~ x, pch=20) 11 | ``` 12 | -------------------------------------------------------------------------------- /messages/1.1.0.txt: -------------------------------------------------------------------------------- 1 | ------------------- 2 | Sublime knitr 1.1.0 3 | ------------------- 4 | 5 | Exciting news! The plugin now has basic support for R Markdown (`.Rmd`) files. 6 | 7 | New Features 8 | * Insert Markdown chunks with the same keyboard shortcut (`super/ctrl+alt+c`) 9 | 10 | * Build an `.Rmd` file to create a knitted `.md` file that you can use anywhere else 11 | (i.e. with Pandoc, Marked, Jekyll, etc.) 12 | -------------------------------------------------------------------------------- /knitr-rnw-chunk.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | >= 4 | $2 5 | @ 6 | ]]> 7 | text.tex.latex.knitr 8 | Insert knitr Rnw chunk 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /knitr-md-chunk.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | 7 | text.html.markdown.knitr 8 | Insert knitr Markdown chunk 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /test_file.Rnw: -------------------------------------------------------------------------------- 1 | \documentclass{article} 2 | 3 | \begin{document} 4 | 5 | Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod 6 | tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, 7 | quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo 8 | consequat. 9 | 10 | <>= 11 | x <- rnorm(100) 12 | y <- rnorm(100) 13 | summary(lm(y ~ x)) 14 | plot(y ~ x, pch=20) 15 | @ 16 | 17 | \end{document} -------------------------------------------------------------------------------- /knitr-Markdown.sublime-build: -------------------------------------------------------------------------------- 1 | { 2 | "selector": "text.html.markdown.knitr", 3 | "working_dir": "${project_path:${folder}}", 4 | "env": { "LANG": "en_US.UTF-8" }, 5 | "cmd": [ "Rscript -e \"library(knitr); knit('$file', output='$file_path/$file_base_name.md')\"" ], 6 | "shell": true, 7 | 8 | "variants": 9 | [ 10 | { 11 | "name": "Run", 12 | "working_dir": "$file_path", 13 | "shell_cmd": "Rscript -e \"rmarkdown::render(input = '$file')\"" 14 | } 15 | ] 16 | } -------------------------------------------------------------------------------- /messages/install.txt: -------------------------------------------------------------------------------- 1 | ------------- 2 | Sublime knitr 3 | ------------- 4 | 5 | Thanks for installing the Sublime knitr plugin! 6 | 7 | 8 | ## --------------------------- IMPORTANT!!! --------------------------- ## 9 | 10 | Make sure you install the R-Box and LaTeXing (or LaTeXTools) packages 11 | before using this plugin. All are available via Package Control. 12 | 13 | Instructions for (1) getting LaTeXing and/or LaTeXTools to work and 14 | (2) working with R Markdown files are in the README file, accessible: 15 | 16 | 1. at the GitHub page (https://github.com/SublimeText/LaTeXTools), or 17 | 2. by using the "Browse Packages..." command in Sublime Text. 18 | 19 | ## -------------------------------------------------------------------- ## 20 | -------------------------------------------------------------------------------- /Default (OSX).sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | // Insert knitr Rnw chunk 3 | { 4 | "keys": ["super+alt+c"], 5 | "command": "insert_snippet", "args": {"name": "Packages/knitr/knitr-rnw-chunk.sublime-snippet"}, 6 | "context": [ 7 | { "operand": "text.tex.latex.knitr", "operator": "equal", "match_all": true, "key": "selector" } 8 | ] 9 | }, 10 | 11 | // Insert knitr Markdown chunk 12 | { 13 | "keys": ["super+alt+c"], 14 | "command": "insert_snippet", "args": {"name": "Packages/knitr/knitr-md-chunk.sublime-snippet"}, 15 | "context": [ 16 | { "operand": "text.html.markdown.knitr", "operator": "equal", "match_all": true, "key": "selector" } 17 | ] 18 | }, 19 | 20 | // Send current Rnw chunk to R 21 | { 22 | "keys": ["super+b"], 23 | "command": "knitr_send_chunk", 24 | "context": [ 25 | { "operand": "text.tex.latex.knitr source.r.embedded.knitr, text.html.markdown.knitr source.r.embedded.knitr", "operator": "equal", "match_all":true, "key": "selector" } 26 | ] 27 | }, 28 | 29 | // Move between chunks 30 | { 31 | "keys": ["super+alt+."], 32 | "command": "knitr_next_chunk", 33 | "context": [ 34 | { "operand": "text.tex.latex.knitr, text.html.markdown.knitr", "operator": "equal", "match_all": true, "key": "selector" } 35 | ] 36 | }, 37 | { 38 | "keys": ["super+alt+,"], 39 | "command": "knitr_prev_chunk", 40 | "context": [ 41 | { "operand": "text.tex.latex.knitr, text.html.markdown.knitr", "operator": "equal", "match_all": true, "key": "selector" } 42 | ] 43 | } 44 | ] 45 | -------------------------------------------------------------------------------- /Default (Windows).sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | // Insert knitr Rnw chunk 3 | { 4 | "keys": ["ctrl+alt+c"], 5 | "command": "insert_snippet", "args": {"name": "Packages/knitr/knitr-rnw-chunk.sublime-snippet"}, 6 | "context": [ 7 | { "operand": "text.tex.latex.knitr", "operator": "equal", "match_all": true, "key": "selector" } 8 | ] 9 | }, 10 | 11 | // Insert knitr Markdown chunk 12 | { 13 | "keys": ["ctrl+alt+c"], 14 | "command": "insert_snippet", "args": {"name": "Packages/knitr/knitr-md-chunk.sublime-snippet"}, 15 | "context": [ 16 | { "operand": "text.html.markdown.knitr", "operator": "equal", "match_all": true, "key": "selector" } 17 | ] 18 | }, 19 | 20 | // Send current Rnw chunk to R 21 | { 22 | "keys": ["ctrl+b"], 23 | "command": "knitr_send_chunk", 24 | "context": [ 25 | { "operand": "text.tex.latex.knitr source.r.embedded.knitr, text.html.markdown.knitr source.r.embedded.knitr", "operator": "equal", "match_all":true, "key": "selector" } 26 | ] 27 | }, 28 | 29 | // Move between chunks 30 | { 31 | "keys": ["ctrl+alt+."], 32 | "command": "knitr_next_chunk", 33 | "context": [ 34 | { "operand": "text.tex.latex.knitr, text.html.markdown.knitr", "operator": "equal", "match_all": true, "key": "selector" } 35 | ] 36 | }, 37 | { 38 | "keys": ["ctrl+alt+,"], 39 | "command": "knitr_prev_chunk", 40 | "context": [ 41 | { "operand": "text.tex.latex.knitr, text.html.markdown.knitr", "operator": "equal", "match_all": true, "key": "selector" } 42 | ] 43 | } 44 | ] 45 | -------------------------------------------------------------------------------- /Default (Linux).sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | // Insert knitr Rnw chunk 3 | { 4 | "keys": ["ctrl+alt+c"], 5 | "command": "insert_snippet", "args": {"name": "Packages/knitr/knitr-rnw-chunk.sublime-snippet"}, 6 | "context": [ 7 | { "operand": "text.tex.latex.knitr", "operator": "equal", "match_all": true, "key": "selector" } 8 | ] 9 | }, 10 | 11 | // Insert knitr Markdown chunk 12 | { 13 | "keys": ["ctrl+alt+c"], 14 | "command": "insert_snippet", "args": {"name": "Packages/knitr/knitr-md-chunk.sublime-snippet"}, 15 | "context": [ 16 | { "operand": "text.html.markdown.knitr", "operator": "equal", "match_all": true, "key": "selector" } 17 | ] 18 | }, 19 | 20 | // Send current Rnw chunk to R 21 | { 22 | "keys": ["ctrl+b"], 23 | "command": "knitr_send_chunk", 24 | "context": [ 25 | { "operand": "text.tex.latex.knitr source.r.embedded.knitr, text.html.markdown.knitr source.r.embedded.knitr", "operator": "equal", "match_all":true, "key": "selector" } 26 | ] 27 | }, 28 | 29 | // Move between chunks 30 | { 31 | "keys": ["ctrl+alt+."], 32 | "command": "knitr_next_chunk", 33 | "context": [ 34 | { "operand": "text.tex.latex.knitr, text.html.markdown.knitr", "operator": "equal", "match_all": true, "key": "selector" } 35 | ] 36 | }, 37 | { 38 | "keys": ["ctrl+alt+,"], 39 | "command": "knitr_prev_chunk", 40 | "context": [ 41 | { "operand": "text.tex.latex.knitr, text.html.markdown.knitr", "operator": "equal", "match_all": true, "key": "selector" } 42 | ] 43 | } 44 | ] 45 | 46 | -------------------------------------------------------------------------------- /chunksend.py: -------------------------------------------------------------------------------- 1 | import sublime 2 | import sublime_plugin 3 | import os 4 | import subprocess 5 | import string 6 | import re 7 | 8 | class KnitrSendChunkCommand(sublime_plugin.TextCommand): 9 | 10 | def run(self, view): # runs on command 11 | initial_selection = self.view.sel()[0] 12 | 13 | if self.view.match_selector(0, "text.tex.latex.knitr"): 14 | for region in self.view.find_all('(?<=>>=\n)((.*\n)+?)(?=@)'): 15 | if region.contains(initial_selection.a): 16 | chunk_range = sublime.Region(region.a, region.b-1) 17 | break 18 | else: 19 | chunk_range = None 20 | 21 | elif self.view.match_selector(0, "text.html.markdown.knitr"): 22 | for region in self.view.find_all('(?<=\}\n)((.*\n)+?)(?=```)'): 23 | if region.contains(initial_selection.a): 24 | chunk_range = sublime.Region(region.a, region.b-1) 25 | break 26 | else: 27 | chunk_range = None 28 | 29 | else: 30 | chunk_range = None 31 | 32 | if not chunk_range: 33 | return 34 | 35 | # Add selection 36 | self.view.sel().add(chunk_range) 37 | 38 | print("send chunk:\n%s" % self.view.substr(chunk_range)) 39 | 40 | # Run command from Enhanced-R 41 | self.view.run_command('r_send_select') 42 | 43 | # Restore initial selection 44 | self.view.sel().subtract(chunk_range) 45 | self.view.sel().add(initial_selection) 46 | self.view.show(initial_selection.a) 47 | 48 | 49 | class KnitrNextChunkCommand(sublime_plugin.TextCommand): 50 | 51 | def run(self,edit): 52 | initial_selection = self.view.sel()[0] 53 | 54 | # Find next chunk 55 | if self.view.match_selector(0, "text.tex.latex.knitr"): 56 | for region in self.view.find_all('(?<=>>=\n)((.*\n)+?)(?=@)'): 57 | if region.b > initial_selection.a < region.a: 58 | chunk_range = region 59 | break 60 | else: 61 | chunk_range = None 62 | 63 | elif self.view.match_selector(0, "text.html.markdown.knitr"): 64 | for region in self.view.find_all('(?<=\}\n)((.*\n)+?)(?=```)'): 65 | if region.b > initial_selection.a < region.a: 66 | chunk_range = region 67 | break 68 | else: 69 | chunk_range = None 70 | 71 | else: 72 | chunk_range = None 73 | 74 | if chunk_range: 75 | self.view.sel().clear() 76 | self.view.sel().add(sublime.Region(chunk_range.a)) 77 | self.view.show(chunk_range.a) 78 | else: 79 | print("end of file reached with no more chunks") 80 | 81 | 82 | class KnitrPrevChunkCommand(sublime_plugin.TextCommand): 83 | 84 | def run(self,edit): 85 | initial_selection = self.view.sel()[0] 86 | 87 | # Find previous chunk 88 | if self.view.match_selector(0, "text.tex.latex.knitr"): 89 | for region in self.view.find_all('(?<=>>=\n)((.*\n)+?)(?=@)')[::-1]: 90 | if region.b < initial_selection.a > region.a: 91 | chunk_range = region 92 | break 93 | else: 94 | chunk_range = None 95 | 96 | elif self.view.match_selector(0, "text.html.markdown.knitr"): 97 | for region in self.view.find_all('(?<=\}\n)((.*\n)+?)(?=```)')[::-1]: 98 | if region.b < initial_selection.a > region.a: 99 | chunk_range = region 100 | break 101 | else: 102 | chunk_range = None 103 | 104 | else: 105 | chunk_range = None 106 | 107 | if chunk_range: 108 | self.view.sel().clear() 109 | self.view.sel().add(sublime.Region(chunk_range.a)) 110 | self.view.show(chunk_range.a) 111 | else: 112 | print("start of file reached with no more chunks") 113 | -------------------------------------------------------------------------------- /knitr-Rnw.tmLanguage: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | fileTypes 6 | 7 | rnw 8 | snw 9 | rtex 10 | 11 | keyEquivalent 12 | ^~S 13 | name 14 | knitr (Rnw) 15 | patterns 16 | 17 | 18 | begin 19 | ^\s*(<<) 20 | beginCaptures 21 | 22 | 1 23 | 24 | name 25 | punctuation.definition.parameters.begin.knitr 26 | 27 | 28 | end 29 | (>>)(?==) 30 | endCaptures 31 | 32 | 1 33 | 34 | name 35 | punctuation.definition.parameters.end.knitr 36 | 37 | 38 | name 39 | meta.block.parameters.knitr 40 | patterns 41 | 42 | 43 | captures 44 | 45 | 1 46 | 47 | name 48 | keyword.other.name-of-parameter.knitr 49 | 50 | 2 51 | 52 | name 53 | punctuation.separator.key-value.knitr 54 | 55 | 3 56 | 57 | name 58 | constant.language.boolean.knitr 59 | 60 | 4 61 | 62 | name 63 | constant.language.results.knitr 64 | 65 | 5 66 | 67 | name 68 | string.unquoted.label.knitr 69 | 70 | 71 | match 72 | (\w+)(=)(?:(true|false)|(verbatim|tex|hide)|([\w.]+)) 73 | name 74 | meta.parameter.knitr 75 | 76 | 77 | match 78 | [\w.]+ 79 | name 80 | string.unquoted.label.knitr 81 | 82 | 83 | match 84 | , 85 | name 86 | punctuation.separator.parameters.knitr 87 | 88 | 89 | 90 | 91 | begin 92 | (?<=>>)(=)(.*)\n 93 | beginCaptures 94 | 95 | 1 96 | 97 | name 98 | punctuation.section.embedded.begin.knitr 99 | 100 | 2 101 | 102 | name 103 | invalid.illegal.knitr 104 | 105 | 106 | contentName 107 | source.r.embedded.knitr 108 | end 109 | ^\s*(@)(.*)$ 110 | endCaptures 111 | 112 | 1 113 | 114 | name 115 | punctuation.section.embedded.end.knitr 116 | 117 | 2 118 | 119 | name 120 | comment.line.other.knitr 121 | 122 | 123 | name 124 | meta.block.code.knitr 125 | patterns 126 | 127 | 128 | include 129 | source.r 130 | 131 | 132 | 133 | 134 | begin 135 | ^\\begin(\{)Scode(\}) 136 | captures 137 | 138 | 1 139 | 140 | name 141 | punctuation.definition.arguments.begin.latex 142 | 143 | 2 144 | 145 | name 146 | punctuation.definition.arguments.end.latex 147 | 148 | 149 | contentName 150 | source.r.embedded.knitr 151 | end 152 | ^\\end(\{)Scode(\}) 153 | name 154 | meta.block.source.r 155 | patterns 156 | 157 | 158 | include 159 | source.r 160 | 161 | 162 | 163 | 164 | begin 165 | \\Sexpr(\{) 166 | beginCaptures 167 | 168 | 1 169 | 170 | name 171 | punctuation.definition.arguments.begin.latex 172 | 173 | 174 | end 175 | (\}) 176 | endCaptures 177 | 178 | 1 179 | 180 | name 181 | punctuation.definition.arguments.end.latex 182 | 183 | 184 | name 185 | source.r.embedded.knitr 186 | patterns 187 | 188 | 189 | include 190 | source.r 191 | 192 | 193 | 194 | 195 | include 196 | text.tex.latex 197 | 198 | 199 | scopeName 200 | text.tex.latex.knitr 201 | uuid 202 | 1F450973-8259-4BA2-A754-48C634561A13 203 | 204 | 205 | -------------------------------------------------------------------------------- /knitr-Markdown.tmLanguage: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | fileTypes 6 | 7 | Rmd 8 | 9 | name 10 | knitr (Markdown) 11 | patterns 12 | 13 | 14 | begin 15 | ^(`{3,}\s*\{r) 16 | beginCaptures 17 | 18 | 1 19 | 20 | name 21 | punctuation.definition.parameters.begin.knitr 22 | 23 | 24 | comment 25 | Capture 26 | 1. ```{r to start the parameters chunk.; 27 | 2. the closing } (unfortunately it captures all }s since limiting it to lines that begin with ```{r seems to require a wildcard inside a lookbehind assertion, which doesn't work, and 28 | 3. the different variables inside the parameters. 29 | 30 | end 31 | (\}) 32 | endCaptures 33 | 34 | 1 35 | 36 | name 37 | punctuation.definition.parameters.end.knitr 38 | 39 | 40 | name 41 | meta.block.parameters.knitr 42 | patterns 43 | 44 | 45 | captures 46 | 47 | 1 48 | 49 | name 50 | keyword.other.name-of-parameter.knitr 51 | 52 | 2 53 | 54 | name 55 | punctuation.separator.key-value.knitr 56 | 57 | 3 58 | 59 | name 60 | constant.language.boolean.knitr 61 | 62 | 4 63 | 64 | name 65 | constant.language.results.knitr 66 | 67 | 5 68 | 69 | name 70 | string.unquoted.label.knitr 71 | 72 | 73 | match 74 | (\w+)(=)(?:(true|false)|(verbatim|tex|hide)|([\w.]+)) 75 | name 76 | meta.parameter.knitr 77 | 78 | 79 | match 80 | [\w.]+ 81 | name 82 | string.unquoted.label.knitr 83 | 84 | 85 | match 86 | , 87 | name 88 | punctuation.separator.parameters.knitr 89 | 90 | 91 | 92 | 93 | begin 94 | (?<=\})(.*)(\n) 95 | beginCaptures 96 | 97 | 1 98 | 99 | name 100 | invalid.illegal.knitr 101 | 102 | 2 103 | 104 | name 105 | punctuation.section.embedded.begin.knitr 106 | 107 | 108 | comment 109 | Capture 110 | 1. the newline *after* the closing brace of the parameters section, 111 | 2. ``` to end the chunk, and 112 | 3. disallow spaces before the initial ```. 113 | 114 | I originally commented out stuff after } and ``` (with comment.line.other.knitr), but characters there actually interfere with sending the chunk to R, so now they are marked as errors. 115 | 116 | contentName 117 | source.r.embedded.knitr 118 | end 119 | ^(`{3,}\s*)(.*)$ 120 | endCaptures 121 | 122 | 1 123 | 124 | name 125 | punctuation.section.embedded.end.knitr 126 | 127 | 2 128 | 129 | name 130 | invalid.illegal.knitr 131 | 132 | 133 | name 134 | meta.block.code.knitr 135 | patterns 136 | 137 | 138 | match 139 | ^\s+`{3,}.*\n? 140 | name 141 | invalid.illegal.knitr 142 | 143 | 144 | include 145 | source.r 146 | 147 | 148 | 149 | 150 | comment 151 | No spaces allowed before the ending ```. 152 | match 153 | ^\s+`{3,}.*\n? 154 | name 155 | invalid.illegal.knitr 156 | 157 | 158 | begin 159 | ^(% .*) 160 | beginCaptures 161 | 162 | 1 163 | 164 | name 165 | string.unquoted.multimarkdown 166 | 167 | 168 | end 169 | ^$|^(?=[A-Za-z0-9]+:) 170 | name 171 | meta.header.pandoc.knitr 172 | patterns 173 | 174 | 175 | comment 176 | The reason for not setting scopeName = "string.unquoted" (for the parent rule) is that we do not want newlines to be marked as string.unquoted. 177 | 178 | Also, you can use title.meta.header.pandoc.knitr as a scope name for the beginCaptures section above for special title highlighting. 179 | match 180 | .+ 181 | name 182 | string.unquoted.multimarkdown 183 | 184 | 185 | 186 | 187 | comment 188 | Import any other Markdown syntaxes. 189 | include 190 | text.html.markdown 191 | 192 | 193 | scopeName 194 | text.html.markdown.knitr 195 | uuid 196 | 96695da4-0e45-45a0-9c77-1b82941531d6 197 | 198 | 199 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sublime knitr 2 | 3 | * Version: 1.2.2 4 | * Date: January 30, 2015 5 | 6 | This package provides [**knitr**](http://yihui.name/knitr/) Markdown and LaTeX support in Sublime Text 2 and 3. It comes with: 7 | 8 | * Language definitions for **knitr** LaTeX and Markdown files 9 | * A build system for R Markdown files. 10 | * The following commands (available via the command palette and with keyboard shortcuts): 11 | * Insert **knitr** chunk snippet: `super+alt+c` 12 | * Move between chunks: `super+alt+,` and `super+alt+.` 13 | * Send chunk to R GUI: `super+b` *(requires [R-Box](https://github.com/randy3k/R-Box))* 14 | 15 | By default, `ctrl` is used in place of `super` on Windows and Linux. 16 | 17 | 18 | ## Dependencies 19 | 20 | In order to use all the features of this package, you'll need to install two other packages. Both are easily installable via Package Control: 21 | 22 | * [R-Box](https://github.com/randy3k/R-Box) 23 | * [SendTextPlus](https://github.com/randy3k/SendTextPlus) 24 | * [LaTeXing](http://www.latexing.com/) or [LaTeXTools](https://github.com/SublimeText/LaTeXTools) (see patch below for LaTeXTools) 25 | 26 | The easiest way to use this plugin is to use [LaTeXing](http://www.latexing.com/), especially since development on LaTeXTools has slowed significantly. Simply enable the `knitr` setting and adjust the command in `knitr_command` if required. 27 | 28 | Alternatively, you can use this plugin with [LaTeXTools](https://github.com/SublimeText/LaTeXTools), with three manual patches, listed below. 29 | 30 | 31 | ## Building R Markdown files 32 | 33 | Building an `.Rmd` file creates an `.md` file in the same directory. It's up to you to use that file elsewhere (i.e. use Pandoc to convert it `.html`, `.docx`, `.rtf`, or even `.tex` if you feel like being extra circuitous). 34 | 35 | There is also a build variant that will create an HTML file from the knitted Markdown file. Use this with `super + shift + b`. 36 | 37 | I typically build the `.Rmd` file once, open the resulting `.md` file in [Marked](http://markedapp.com/), and then leave it open in Marked as I make further changes and newer builds. 38 | 39 | Alternatively, you can force the build system to open the resulting `.md` file in the default program for Markdown files by changing the `"cmd":` line in `knitr-Markdown.sublime-build` to: 40 | 41 | "cmd": [ "Rscript -e \"library(knitr); knit('$file_name')\"; open $file_base_name.md" ], 42 | 43 | 44 | ## Unicode and other encoding issues 45 | 46 | Working with non-ASCII characters in plots is a little tricky because of how LaTeX and R differently support Unicode. Here's are some general guidelines for fixing character encoding issues: 47 | 48 | 1. Add `LANG=en_US.UTF-8` to `~/.Renviron` (create this file if needed). This will ensure that R runs with Unicode support whenver it opens. 49 | 2. Add a separate chunk near the beginning of your document with this: `pdf.options(encoding = '')`, where `encoding` is any of those listed in the output of this command: `list.files(system.file('enc', package = 'grDevices'))`. Choose an encoding that encompasses all the characters you're using in your plots. 50 | 3. If using `.Rnw` and LaTeX, ensure that `\usepackage[utf8]{inputenc}` is in your preamble. 51 | 52 | 53 | ## Roadmap and wish list 54 | 55 | * Better Markdown syntax highlighting, including Multimarkdown and Pandoc extras like footnotes, tables, and citations. 56 | * Create commands for Pandoc conversion from R Markdown to other formats? (or maybe just use actual Pandoc packages for that). 57 | 58 | ------------ 59 | 60 | ### Manual patch for LaTeXTools 61 | 62 | If you want to use the LaTeXTools plugin, you need to patch three files to make the standard LaTeXTools build system knit and typest the `.Rnw` file. Make these three changes (*huge* thanks to [Heberto del Rio](http://stackoverflow.com/a/15017303/120898) for this!): 63 | 64 | **Important:** *Copying and pasting code from GitHub can do unexpected things to indentation (replacing tabs with spaces) and can temporarily break LaTeXTools. Make sure the indentation is correct after pasting.* 65 | 66 | #### File 1: `Packages/LaTeX/LaTeX.tmLanguage` 67 | 68 | Add `Rnw` to the list of accepted LaTeX file types, like so: 69 | 70 | 71 | 72 | 73 | 74 | fileTypes 75 | 76 | tex 77 | Rnw 78 | 79 | 80 | **Instructions for Sublime Text 3**: In ST3, default packages are hidden deep within ST itself and are difficult to access, let alone edit. However, you can still get to `LaTeX.tmLanguage` relatively easily if you install the [PackageResourceViewer plugin](https://github.com/skuroda/PackageResourceViewer). After installing it, run the "PackageResourceViewer: Open Resource:" command through the command pallete (command/ctrl + shift + p) and navigate to LaTeX.tmLanguage. After making changes, ST will save a copy of the file in a more accessible location (`Packages/LaTeX/LaTeX.tmLanguage`), overriding the default built-in file. 81 | 82 | #### File 2: `Packages/LaTeXTools/makePDF.py` 83 | 84 | Find this: 85 | 86 | if self.tex_ext.upper() != ".TEX": 87 | sublime.error_message("%s is not a TeX source file: cannot compile." % (os.path.basename(view.file_name()),)) 88 | return 89 | 90 | And replace with this: 91 | 92 | if (self.tex_ext.upper() != ".TEX") and (self.tex_ext.upper() != ".RNW"): 93 | sublime.error_message("%s is not a TeX or Rnw source file: cannot compile." % (os.path.basename(view.file_name()),)) 94 | return 95 | 96 | Then find this: 97 | 98 | # We should now be able to construct the builder object 99 | self.builder = builder_class(self.file_name, self.output, builder_settings, platform_settings) 100 | 101 | And replace with this: 102 | 103 | if self.tex_ext.upper() == ".RNW": 104 | # Run Rscript -e "library(knitr); knit('" + self.file_name + "')" 105 | os.system("Rscript -e \"library(knitr); knit('"+ self.file_name +"')\"") 106 | self.file_name = self.tex_base + ".tex" 107 | self.tex_ext = ".tex" 108 | 109 | # We should now be able to construct the builder object 110 | self.builder = builder_class(self.file_name, self.output, builder_settings, platform_settings) 111 | 112 | (If you want to use `Sweave` instead of `knitr`, change the `Rscript` command accordingly.) 113 | 114 | #### File 3: `Packages/LaTeXTools/jumpToPDF.py` 115 | 116 | Find this: 117 | 118 | if texExt.upper() != ".TEX": 119 | sublime.error_message("%s is not a TeX source file: cannot jump." % (os.path.basename(view.fileName()),)) 120 | return 121 | 122 | And replace with this: 123 | 124 | if (texExt.upper() != ".TEX") and (texExt.upper() != ".RNW"): 125 | sublime.error_message("%s is not a TeX or Rnw source file: cannot jump." % (os.path.basename(view.fileName()),)) 126 | return 127 | 128 | #### File 4: `Packages/LaTeXTools/viewPDF.py` 129 | 130 | Find this: 131 | 132 | if texExt.upper() != ".TEX": 133 | sublime.error_message("%s is not a TeX source file: cannot view." % (os.path.basename(view.fileName()),)) 134 | return 135 | 136 | And replace with this: 137 | 138 | if (texExt.upper() != ".TEX") and (texExt.upper() != ".RNW"): 139 | sublime.error_message("%s is not a TeX or Rnw source file: cannot view." % (os.path.basename(view.fileName()),)) 140 | return 141 | 142 | 143 | If you want to be able to use multiple files and to find your bib file, you'll also need to change the following files: 144 | #### File 5: `Packages/LaTeXTools/getTeXRoot.py` 145 | 146 | Find this: 147 | 148 | mroot = re.match(r"%\s*!TEX\s+root *= *(.*(tex|TEX))\s*$",line) 149 | 150 | And replace with this: 151 | 152 | mroot = re.match(r"%\s*!TEX\s+root *= *(.*(tex|rnw))\s*$",line, flags=re.IGNORECASE) 153 | 154 | #### File 6: `Packages/LaTeXTools/latex_cite_completions.py` 155 | 156 | Find this: 157 | 158 | if src[-4:].lower() != ".tex": 159 | 160 | And replace with this: 161 | 162 | if src[-4:].lower() not in [".tex",".rnw"]: 163 | 164 | 165 | --------------------------------------------------------------------------------