Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to evaluate code line-by-line in org-mode?

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
like image 445
Rodrigo Avatar asked Apr 06 '15 13:04

Rodrigo


People also ask

How do I run code in Org mode?

Org provides many ways to execute code blocks. C-c C-c or C-c C-v e with the point on a code block143 calls the org-babel-execute-src-block function, which executes the code in the block, collects the results, and inserts them in the buffer. By calling a named code block144 from an Org mode buffer or a table.

What is so great about Org mode?

Org mode is routinely used to build and manage complex workflows. It does this using an elegantly simple syntax that scales from basic markup to full LaTeX typesetting and from plain text notes to literate programs. Everything you need to get started is demonstrated in the example.

How do you make a table in Org mode?

The easiest way to create a table is to directly type the "|" character at the beginning of a line, or after any amount of white space. This will put you in the first field of an atomic table. Once you've finished editing this cell, you can jump to the next one by pressing TAB .


2 Answers

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                                                                                                                                                                                                           
like image 103
Stefan Avey Avatar answered Oct 23 '22 12:10

Stefan Avey


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.

like image 23
DJJ Avatar answered Oct 23 '22 14:10

DJJ