Org-mode mailing list
 help / color / mirror / Atom feed
From: Jeremie Juste <jeremiejuste@gmail.com>
To: Org Mode <emacs-orgmode@gnu.org>
Cc: Jack Kamm <jackkamm@gmail.com>,
	"Berry, Charles" <ccberry@health.ucsd.edu>
Subject: [PATCH] ob-R output file with graphics parameter
Date: Thu, 24 Jun 2021 00:50:04 +0200
Message-ID: <8735t8nqlf.fsf@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 678 bytes --]


Hello,

With the following patch the link to graphics output is finally back on
with R source code. Note that on top of the "graphics" parameter the
"file" parameter have to be used for the org-link to work out of the
box. I will try to remove the need for a second "file" parameter in the
future. 



#+begin_src R :results output graphics file  :file test2.pdf :session *local* :cache no :dir "~/Documents/images"
plot(rnorm(100))
#+end_src

#+RESULTS:
[[file:images/test2.pdf]]

The link also works for R remote sessions. Comments are welcome.  The
next steps

- Remove second the need for a second file parameter
- Add tests to avoid regression
- implement async in ob-R



[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: ob-R-graphs.patch --]
[-- Type: text/x-diff, Size: 5447 bytes --]

From e3d37da7b643990dc70ee42528051f1323fa5cae Mon Sep 17 00:00:00 2001
From: Jeremie Juste <jeremiejuste@gmail.com>
Date: Wed, 23 Jun 2021 23:58:26 +0200
Subject: [PATCH 1/4] ob-R.el: Remove redundant argument to function

* lisp/ob-R.el (org-babel-R-construct-graphics-device-call):  Modify
function to take only 'params' and not 'graphics-file'. The parameter
graphics-file can be extracted from params.
---
 lisp/ob-R.el | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/lisp/ob-R.el b/lisp/ob-R.el
index 2d9073cee..8266dc4ae 100644
--- a/lisp/ob-R.el
+++ b/lisp/ob-R.el
@@ -170,8 +170,7 @@ This function is called by `org-babel-execute-src-block'."
 	      (mapconcat 'identity
 			 (if graphics-file
 			     (append
-			      (list (org-babel-R-construct-graphics-device-call
-				     graphics-file params))
+			      (list (org-babel-R-construct-graphics-device-call params))
 			      inside
 			      (list "},error=function(e){plot(x=-1:1, y=-1:1, type='n', xlab='', ylab='', axes=FALSE); text(x=0, y=0, labels=e$message, col='red'); paste('ERROR', e$message, sep=' : ')}); dev.off()"))
 			   inside)
@@ -311,13 +310,16 @@ Each member of this list is a list with three members:
 3. the name of the argument to this function which specifies the
    file to write to (typically \"file\" or \"filename\")")
 
-(defun org-babel-R-construct-graphics-device-call (out-file params)
+(defun org-babel-R-construct-graphics-device-call (params)
   "Construct the call to the graphics device."
   (let* ((allowed-args '(:width :height :bg :units :pointsize
 				:antialias :quality :compression :res
 				:type :family :title :fonts :version
 				:paper :encoding :pagecentre :colormodel
 				:useDingbats :horizontal))
+
+         (out-file (cdr (assq :file params)))
+
 	 (device (file-name-extension out-file))
 	 (device-info (or (assq (intern (concat ":" device))
 				org-babel-R-graphics-devices)
-- 
2.20.1


From 589eab5d98a4156b46943c7201404300f0ae6ca0 Mon Sep 17 00:00:00 2001
From: Jeremie Juste <jeremiejuste@gmail.com>
Date: Thu, 24 Jun 2021 00:04:59 +0200
Subject: [PATCH 2/4] ob-R.el: New function to construct org-link

* lisp/ob-R.el (org-babel-output-link-path:R):  New function to
contruct org-link from 'params'.
---
 lisp/ob-R.el | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/lisp/ob-R.el b/lisp/ob-R.el
index 8266dc4ae..c325c9cfa 100644
--- a/lisp/ob-R.el
+++ b/lisp/ob-R.el
@@ -185,6 +185,13 @@ This function is called by `org-babel-execute-src-block'."
 		 (org-babel-pick-name
 		  (cdr (assq :rowname-names params)) rownames-p)))))
       (if graphics-file nil result))))
+(defun org-babel-output-link-path:R (params)
+  "format org-link to file from PARAMS"
+  
+  (format "[[file:%s]]" (concat (cdr (assq :dir params))
+                                 "/"
+                        (cdr (assq :file params)))))
+  
 
 (defun org-babel-prep-session:R (session params)
   "Prepare SESSION according to the header arguments specified in PARAMS."
-- 
2.20.1


From b55148df0da94312e1a3e5709263ed9b66355384 Mon Sep 17 00:00:00 2001
From: Jeremie Juste <jeremiejuste@gmail.com>
Date: Thu, 24 Jun 2021 00:07:39 +0200
Subject: [PATCH 3/4] ob-R.el: Return link if graphics parameter is used

* lisp/ob-R.el (org-babel-execute:R): Return org-link instead of nil if graphics
parameter is used.
---
 lisp/ob-R.el | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lisp/ob-R.el b/lisp/ob-R.el
index c325c9cfa..9f32db97a 100644
--- a/lisp/ob-R.el
+++ b/lisp/ob-R.el
@@ -184,7 +184,9 @@ This function is called by `org-babel-execute-src-block'."
 	     (or (equal "yes" rownames-p)
 		 (org-babel-pick-name
 		  (cdr (assq :rowname-names params)) rownames-p)))))
-      (if graphics-file nil result))))
+      (if graphics-file (org-babel-output-link-path:R params)
+        result))))
+
 (defun org-babel-output-link-path:R (params)
   "format org-link to file from PARAMS"
   
-- 
2.20.1


From 12a06671415a55c726975d6e0297a20aefbbce62 Mon Sep 17 00:00:00 2001
From: Jeremie Juste <jeremiejuste@gmail.com>
Date: Thu, 24 Jun 2021 00:11:10 +0200
Subject: [PATCH 4/4] ob-R.el: Handle graphics file path for remote R session

* lisp/ob-R.el (org-babel-R-construct-graphics-device-call): If R
session is remote, remove "/ssh:" part in file path before writing the
graph to file on remote host.
---
 lisp/ob-R.el | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/lisp/ob-R.el b/lisp/ob-R.el
index 9f32db97a..2769804a4 100644
--- a/lisp/ob-R.el
+++ b/lisp/ob-R.el
@@ -336,6 +336,11 @@ Each member of this list is a list with three members:
         (extra-args (cdr (assq :R-dev-args params))) filearg args)
     (setq device (nth 1 device-info))
     (setq filearg (nth 2 device-info))
+    (setq out-file-path  (concat
+                          (replace-regexp-in-string "/ssh:.*?:" "" (cdr (assq :dir params)))
+                                 "/"
+                                      out-file))
+
     (setq args (mapconcat
 		(lambda (pair)
 		  (if (member (car pair) allowed-args)
@@ -344,7 +349,7 @@ Each member of this list is a list with three members:
 			      (cdr pair)) ""))
 		params ""))
     (format "%s(%s=\"%s\"%s%s%s); tryCatch({"
-	    device filearg out-file args
+	    device filearg out-file-path args
 	    (if extra-args "," "") (or extra-args ""))))
 
 (defconst org-babel-R-eoe-indicator "'org_babel_R_eoe'")
-- 
2.20.1


[-- Attachment #3: Type: text/plain, Size: 34 bytes --]



Best regards,
-- 
Jeremie Juste

             reply	other threads:[~2021-06-23 22:51 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-23 22:50 Jeremie Juste [this message]
2021-06-24  9:21 ` Colin Baxter
2021-06-27 17:12   ` Jack Kamm
2021-06-28  9:56     ` Colin Baxter
2021-06-28 21:54     ` Jeremie Juste
2021-07-03  4:21       ` Jack Kamm
2021-07-03  4:52         ` Timothy
2021-07-03 13:01           ` Jack Kamm
2021-07-03 17:08         ` Jeremie Juste
2021-07-03 18:14         ` Berry, Charles via General discussions about Org-mode.
2021-07-03 20:48           ` Jack Kamm
2021-07-06 15:04             ` Jack Kamm
2021-07-06 17:58               ` Berry, Charles via General discussions about Org-mode.
2021-07-06 19:20                 ` Jack Kamm
2021-07-06 21:43                   ` Berry, Charles via General discussions about Org-mode.
2021-07-10 10:16                     ` Jeremie Juste
2021-07-10 21:00                     ` Jack Kamm
2021-07-11  3:23                       ` Berry, Charles via General discussions about Org-mode.

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://orgmode.org

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8735t8nqlf.fsf@gmail.com \
    --to=jeremiejuste@gmail.com \
    --cc=ccberry@health.ucsd.edu \
    --cc=emacs-orgmode@gnu.org \
    --cc=jackkamm@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Org-mode mailing list

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://orgmode.org/list/0 list/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 list list/ https://orgmode.org/list \
		emacs-orgmode@gnu.org
	public-inbox-index list

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.yhetil.org/yhetil.emacs.orgmode
	nntp://news.gmane.io/gmane.emacs.orgmode


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git