How do I get org-mode to execute code blocks consistently - org-mode

I'm running R code blocks in a session in an org-mode file, and I can usually get them to execute by C-c C-c, as expected.
I think the pertinent PROPERTY lines I've put at the top of my file are
#+PROPERTY: session *R*
#+PROPERTY: cache yes
#+PROPERTY: exports both
#+PROPERTY: tangle yes
Sometimes, nothing happens when I press C-c C-c. AFAICT, that's a seemingly random event; the same code block may work one time and fail another time.
When such a block fails, I do see the results echoed to the minibuffer, but I don't see any results (e.g., a new data frame, as I would have expected in many cases) in the R session.
If I press C-' to edit the code block and then press C-j on each line (or C-r on each region), the code does execute, and the results get echoed appropriately to the org file and show up in the R session.
Here are some sample code block begin lines:
#+begin_src R :results silent :exports code
#+begin_src R :results value :colnames yes :exports both
I tested both just now by doing rm(list=ls()), running the code blocks, and doing ls() in the session to see if the results were there.
The first block is simple:
#+begin_src R :results silent :exports code
require(stringr)
require(ggplot2)
require(scales)
require(arm)
require(YaleToolkit)
require(stinepack)
require(mixtools)
require(lubridate)
source("utilities.R")
pf <- function (x,y) {
z <- sqrt(x * x + y * y)
return(x/z)
}
#+end_src
In two tests, it worked once and failed once, as detected by looking for pf in the R session.
If important, I can try to produce an ECM that fails frequently enough to be useful, but I suspect this may be a common setup or version problem that someone has seen and solved. It seems related to Org-mode code block evaluation, but the solution there seems to have been to :export results. In most cases, I use :exports both, which would seem to cover that case, and, in at least some cases, I care about setting up the environment in the session and not exporting to the org file. I'm never passing data through org-mode; I'm only using the session for that.
BTW, I'm running org-mode 7.8.03 on GNU Emacs 23.3.1 on XP Pro 64.

Related

org-indent-line always adds a comma before an leading asterisk in code block

Launch Emacs with emacs -q and write in org-mode:
#+BEGIN_SRC C
/*
* This is a comment line
*/
#+END_SRC
Press TAB inside the block. It becomes:
#+BEGIN_SRC C
/*
,* This is a comment line
,*/
#+END_SRC
Is there any way to get rid of this behevior?
This is a quoting mechanism to make sure that the asterisks are not interpreted as headline markers and throwing off Org mode's parser. See Literal Examples in the manual.
There is no way to turn it off and you shouldn't anyway because Org mode will be very confused otherwise. The commas do not affect the exporting of the document or the evaluation of a source block: they are properly stripped at the appropriate time.

Evaluate all code chunks below current header in emacs org mode

I use org mode for reproducible research and literate programming, mainly using R for computations. I'm new to emacs.
If I'm starting a new R session or my session is interrupted, I will have to rerun a number of code chunks. This is very easy with the speed commands (typing "e" to evaluate the chunk, then "n" to move to the next). I love this feature, the only problem is that my headers and subtrees are then expanded and I have to close them again to keep the page tidy.
Is there an existing feature or lisp expression to evaluate all code chunks below a header? And is it possible to do this without expanding the content below?
many thanks
For example, if the cursor were at the start of header 1, could you evaluate the code chunks under header 2 and 3 with a single command?
* header 1
** header 2
#+BEGIN_SRC R
print("hello")
#+END_SRC
** header 3
#+BEGIN_SRC R
print("world")
#+END_SRC
It sounds like you want org-babel-execute-buffer or org-babel-execute-subtree (C-c C-v s).

how to override the save-file command in org-babel

I really like the org-babel that enables me to organize my scripts in org-mode, however, i found there are some issues with the org src buffer when i edit the source code in a separate buffer (using keybinding C-c ').
first thing is, even i explicitly run write-file, and then specifies the file path and name to save, the buffer is not saved to that file, but the source code block in the .org file gets updated and the .org -file is saved.
second thing is, whenever i run save-buffer in org src buffer, the buffer screen will automatically scroll down till current mouse position is the last line in the buffer. this is annoying because sometimes i lose tracking my scripts.
i am not so familiar with elisp, and can only do simple work like define-key or add-hook, i hope i can get help from here. thanks
Org-babel is not meant for organizing scripts, but for including source code as part of your document.
You may be interested in tangling, which allows to join and extract source code blocks from your orgmode document into separate files.
The following example will merge 2 source code fragments into the file test.m when tangling (org-babel-tangle, bound to C-c C-v t):
* Tangling example
Set up a vector:
#+begin_src octave :tangle test.m
a = 1:10;
#+end_src
Then find out squares
#+begin_src octave :tangle test.m
b = a.^2
#+end_src

Why does emacs' comint-send-string behave differently in different derived modes?

I've been fooling around with comint-mode lately and I'm noticing some weird behaviors. Its very poorly documented, so I'm wondering if anyone has any insight on this.
In some modes, comint-send-string causes whatever is sent to be inserted into the comint buffer and then sent to the associated process, whereas in others, the input is send directly to the process without being placed into the buffer. For example, do run-python with the new (24.3) python.el and then do (comint-send-string "*Python*" "x=3\n"), the string x=3 is inserted into the buffer and then executed. If you do M-x shell, however, and then (comint-send-string "*shell*" "x=3\n"), no text is inserted into the buffer, the input is simply sent to the shell process directly to be executed.
Does anyone know why this difference in behavior exists or how I can change it?
I observe identical behavior on linux (emacs-version == "24.3.50.7", both GUI and emacs -Q -nw): neither
(comint-send-string "*Python*" "x=3\n")
nor
(comint-send-string "*shell*" "x=3\n")
insert anything in the comint buffer (i.e., the next prompt appears
right after the previous prompt - without even a newline between them).
I eventually figured it out. For some reason the system python on OSX causes this behavior, installing python from homebrew fixed it.

Perl debugger on Emacs: Clear screen (buffer)?

Say I have long debugging session in Perl with perldb on Emacs (M-x perldb). The output piles up, and at some point it becomes difficult to navigate through it.
I can restart the debugger with R, but that doesn't clear up the previous output. Is there a command to clear up the output of the buffer of the Perl debugger without having to kill the debugger and starting a new session?
You can run comint-truncate-buffer. This will remove all but the last 1024 lines from the buffer. The size can be customized by changing comint-buffer-maximum-size.
If you'd like to have the buffer truncated automatically, run this snippet of elisp:
(add-hook 'comint-output-filter-functions 'comint-truncate-buffer)
Debugger input and output is buffer contents like any other — if you want to delete it, just do it. For example, C-x h C-w (mark-whole-buffer followed by kill-region) works in perldb buffers just fine, and is the closest equivalent to a "clear screen" command in a text terminal.