How to evaluate code line-by-line in org-mode?
Asked Answered
D

2

7

I want to be able to execute code line-by-line in a chunk of code that is in a org buffer. My goal is to execute code line-by-line without having to run all the code inside a chunk at once (C-c C-c), and without having to switch to a special buffer (C-c '). I use R, but it could be applied to any other language. Is there a way to do this?

Example: Execute only the first line in the following chunk of code, such that the variable a gets the value 7 in the session.

#+BEGIN_SRC R :session
a <- 3 + 4 
a <- 5 + 6
#+END_SRC
Duel answered 6/4, 2015 at 13:19 Comment(6)
What's the criterion you'd be using to know what line(s) should be executed?Maryn
The criterion of evaluation is the line under the cursor. I guess that in the end I should be calling the function ess-eval-region-or-line-and-step, but not sure what's the best way to do it. Maybe change to ess-mode while the cursor is inside a code chunk?Duel
@Duel To clarify, do you care about the output being put into the #+RESULTS:? Otherwise, it is trivial to do this by running M-x ess-eval-region-or-line-and-step as you said. You could give this a key binding and use it even if your buffer is not in ess-modeTombola
I do not care about the #+RESULTS:. I just want to run some specific lines without having to switch to the special buffer. I tried M-x ess-eval-region-or-line-and-step but I get prompted for the dialect, and after choosing R emacs hangs. I am using spacemacs. Not sure that's the reason.Duel
I'm not familiar with spacemacs but it's possible that this is the reason. Running your example with ess-eval-region-or-line-and-step works for me using Emacs 24.3.1, R version 3.1.2, and org version 20130802 when I'm in org-mode.Tombola
It also works with me with a new buffer. The issue before was that I had a line starting with # stuff, and somehow the cursor ended up there. Thanks!Duel
T
5

Not a complete answer but I'd say that if you want to run line-by-line, it is probably better to do this in an actual R session until you figure out what you actually want.

If you actually want results at multiple stages, you can split the code into multiple blocks and they'll use the same R session 'session'

#+BEGIN_SRC R :session                                                                                                                                                                                         
a <- 3 + 4                                                                                                                                                                                                     
#+END_SRC                                                                                                                                                                                                      

#+RESULTS:
: 7                                                                                                                                                                                                            

#+BEGIN_SRC R :session                                                                                                                                                                                         
a <- a + 6                                                                                                                                                                                                     
#+END_SRC                                                                                                                                                                                                      

#+RESULTS:
: 13                                                                                                                                                                                                           
Tombola answered 6/4, 2015 at 15:6 Comment(2)
It may seem strange, but my goal is exactly to avoid having many chunks in a buffer, which hurts readability. My goal is to have big chunks of code and be able to run line-by-line for testing purposes.Duel
In that case, we can probably modify some of the org-babel code to run a line instead of the whole src block. I'll look into this. I think we can do it by modifying a function like org-babel-R-evaluate-session in ob-R.el (see the code here orgmode.org/w/?p=org-mode.git;a=blob_plain;f=lisp/…)Tombola
I
3

In a code block you can use C-c C-v z to switch to the session with the code. Then you can evaluate line by line as if you were in a .R file and come back to the .org file with C-'

Have a look at the documentation. or C-c C-h in a .org file for some quick reference.

Ideality answered 22/12, 2015 at 18:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.