There is a somewhat hacky solution.
The following advice re-defines the source code string returned by
org-babel-get-src-block-info if the region is active within this source code block and it is the top-level source code block.
You can switch off this behavior by setting the
(defcustom org-babel-region t
"Restrict execution to region."
(defadvice org-babel-get-src-block-info (after org-babel-src-region activate)
"Restrict org-babel execution to region
if region is active within this source code block
and it is the top-level source code block."
;; source code block string is (nth 1 (org-babel-get-src-block-info))
;; head marker is (last (org-babel-get-src-block-info))
;; the outer-most src code block is stored in variable `org-babel-current-src-block-location'
(let (head-marker b e)
(setq head-marker (car (last ad-return-value)))
(equal org-babel-current-src-block-location head-marker)
(setq b (region-beginning) e (region-end))
(and (goto-char b)
(equal head-marker (org-babel-where-is-src-block-head))
(equal head-marker (org-babel-where-is-src-block-head))))
(setf (nth 1 ad-return-value)
(buffer-substring-no-properties b e)))))
I have tested the advice with code blocks for
I do not have R but it should work for R too.
In the current implementation output remains enabled for the evaluation of a region if it is enabled for the whole source code block (e.g., in the header arguments).
This may be a catch. There are some source code blocks where you must print the output within the source code block or you get an error.
In this case you will get an error if the selected region does not include the print directives, i.e., in most cases the end of the source code block.