emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] ob-R output file with graphics parameter
@ 2021-06-23 22:50 Jeremie Juste
  2021-06-24  9:21 ` Colin Baxter
  2021-09-26  8:48 ` Bastien
  0 siblings, 2 replies; 21+ messages in thread
From: Jeremie Juste @ 2021-06-23 22:50 UTC (permalink / raw)
  To: Org Mode; +Cc: Jack Kamm, Berry, Charles

[-- 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

^ permalink raw reply related	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2021-09-26  9:31 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-06-23 22:50 [PATCH] ob-R output file with graphics parameter Jeremie Juste
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.
2021-09-26  8:48 ` Bastien
2021-09-26  9:13   ` Jeremie Juste
2021-09-26  9:29     ` Bastien

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).