1

I am would like to edit my R chunks from RMarkdown files the way org-edit-special does. I found generic-edit-special from jonathan leech-pepin that does a similar thing for js, css and ruby in html files. I figured I could tweak it as suggested by the author to make it work for my case but I was not able to make it work even in its original form.

I was able to run the function ges/org-edit-special from a html file with a js script block but nothing happened (no error and no new buffer). I used the Internal Script example from here as html file for this test.

Here is the init.el I made for testing:

(require 'package)
(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/"))
(setq package-list
    '(js2))

; activate all the packages
(package-initialize)
(load "~/.emacs.d/lisp/generic-edit-special")

; Org setup (telling org-mode to edit javascript with js2)
(push (cons "javascript" 'js2) org-src-lang-modes)
;; For html-mode

(require 'generic-edit-special)
(eval-after-load "sgml-mode" '(define-key sgml-mode-map [(control c) ?'] 'ges/org-edit-special))

I am pretty new to emacs and use spacemacs usually so I guess it is just me not being able to configure things correctly but I am clearly lost right now.

I also know about polymode for editing RMarkdown files but do not like to use it. I prefer the "external code buffer" approach. And I cannot use pure org-mode either as I have to collaborate with people not using Emacs.

Rorschach
  • 31,301
  • 5
  • 78
  • 129
Gallarus
  • 476
  • 3
  • 9
  • Hi @Gallarus I use `polymode` and works well for me. And you writhe standard RMarkdown files that you should be able to share with non emacs users. – dmontaner Feb 02 '20 at 23:57
  • Using `polymode` I have several issues. Emacs can become laggy if the file is big. Line truncation does not work (it gets set to nil as soon as any completion occurs). Some weird characters appears at the end of all lines. But mostly I prefer the "multiple buffer option" – Gallarus Feb 03 '20 at 01:29

2 Answers2

1

Like you, I prefer the "external code buffer" but often need to share .Rmd or other formats. Luckily, both knitr and rmarkdown support markup in your R code buffer. See here for a (dated) intro to "spinning" your R code.

Anyway, the format is basically just markdown after ##' comments (double hash with quote) with some yaml header information as in Rmd files, eg. the following can be run as pure R code,

##' ---
##' title: "Foo"
##' output:
##'   html_document:
##'     toc: TRUE
##' author: Me
##' ---

##- r setup, include=FALSE -------------------------------------------------
library(ggplot2)
knitr::opts_chunk$set(echo = TRUE)
## /* end r setup */

##' # A header
##' a code block
##- blk1 -------------------------------------------------------------------
dat <- data.frame(x=sample(10, 10), y=runif(10))
## /* end blk1 */

##' # Another section
##' A code block w/ image
##- img,  fig.width=9, fig.height=4 ----------------------------------------
plot(y ~ x, data=dat, type='l')
## /* end sem */

##' # Next section
##' etc.

and converted into an Rmd with knitr::spin("<filename.R>", knit=FALSE), or rendered to HTML (as specified above) with rmarkdown::render("<file.R>").

Personally, I would simplify the file generation with a Makefile (make sure those are tabs), eg. to both render and create an Rmd,

foo.html: foo.R
    rscript -e "knitr::spin(\"$^\", knit=FALSE); \
    rmarkdown::render(\"$^\", \"all\")"

Alternatively, the commands could be used from Emacs to compile, etc.

Rorschach
  • 31,301
  • 5
  • 78
  • 129
  • Actually it is what I am doing already. But I have two problem with it: if I need some back and forth I don't know how to unspin (Rmd -> R) and you loose the benefits of emacs markdown mode for the markdown chunks – Gallarus Feb 03 '20 at 01:25
  • (1) `knitr::purl` converts Rmd -> R, although I never used it much, but it seems to work fine on the above example (then you can just search-replace "##" for "##' " for example) – Rorschach Feb 03 '20 at 01:33
  • (2) If you want the benefits of markdown-mode that would be trickier, eg. along the lines of `org-edit-special`, or more heavyweight `polymode` as you mentioned earlier. Personally, I just have my `newline` insert `##' ` by default when the cursor is in a markup line, and that takes away most of the hassle, plus a couple of snippets for code blocks / headers – Rorschach Feb 03 '20 at 01:35
  • I thought you only get the code with `purl` but with the option `documentation = 2` it allows a pretty good back and forth when added to spin. How do you get your `newline` to insert `##'` ? (I am very new to all this sorry) – Gallarus Feb 03 '20 at 01:53
  • my newline involves a few hundred lines of code, but if you ask a new question, I or someone else could provide something that would work for this case – Rorschach Feb 03 '20 at 03:24
0

I just discovered the markdown-edit-code-block function from markdown-mode that does exactly what I want.

I set the major mode for .Rmd files to be markdown-mode and executing it inside an R chunk opens an indirect buffer with ess-r-mode as major mode. (In doom emacs, the default keybinding is , ' )

Gallarus
  • 476
  • 3
  • 9