emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Charles C. Berry" <cberry@tajo.ucsd.edu>
To: Eric Schulte <schulte.eric@gmail.com>
Cc: emacs-orgmode@gnu.org
Subject: Re: Prompts again WAS Re: avoiding source block prompts
Date: Wed, 4 Aug 2010 20:09:30 -0700	[thread overview]
Message-ID: <Pine.LNX.4.64.1008041905500.18329@tajo.ucsd.edu> (raw)
In-Reply-To: <874ofj63dw.fsf@gmail.com>

On Wed, 28 Jul 2010, Eric Schulte wrote:

> Thanks Chuck,
>
> I've just swapped in your new and improved regexp.
>
> I share your concern both that there could be cases where prompts aren't
> matched, or more seriously where the actual output looks like a prompt
> (e.g. user returns a prompt-like string).
>
> Before taking this explicit prompt removal approach I tried
> automatically removing comment lines from the input before passing it to
> the inferior R process, however that ran into similar problems of users
> potentially inputting valid strings which look like comments -- which
> would be hard to catch without implementing a fairly sophisticated R
> parser as part of Babel.
>
> This new regexp looks like the best option for now.
>
> Thanks! -- Eric

Eric (and anyone else listening in),

I've put together a revised org-babel-R-evaluate.

With "R :session :results ouput" it  will gracefully handle commands like:

 	cat("\n > > + > \n")

leaving the output in place, but trimming out the prompts and gratuitous 
lines.

The code appears below along with a couple of test cases.

I did not touch anything in org-babel-R-evaluate above the second
'(output'; it looks like the ':session :results value' case 
will  have trouble when a block ends with something like this

 	cat( "no newline" );invisible(1+1)

(say) and (inferior-ess-send-input) does not result in a newline, but
I am not sure how to engineer that. Maybe just
eval( parse( text=..ob.body ) ), but I don't know how to get the text in
body sent to R as ..ob.body.

If the elisp looks peculiar, it is because I am just picking it up.
Criticism and pointers welcome.

Chuck


#+begin_src emacs-lisp
   (setq org-babel-R-initiate-sink
        "..ob.tcon <- textConnection(\"..ob.tcon.txt\",\"w\")
        ..ob.eol <- \"\n\"
         sink( ..ob.tcon ) \n")

   (setq org-babel-R-finish-sink
        "\nsink()
         close( ..ob.tcon )
         cat( ..ob.eol )
         cat(..ob.tcon.txt, sep=..ob.eol)
         rm( ..ob.tcon.txt )")

(defmacro sinkout (bd)
   (list 'concat org-babel-R-initiate-sink bd org-babel-R-finish-sink))

(defun drop-two-lines (linestring)
   "Search for second '\\n' and delete all characters up to an including it."
   (substring  linestring
               (progn (string-match "\n.*\n" linestring)
                      (match-end 0))))

(defun org-babel-R-evaluate
   (session body result-type column-names-p row-names-p)
   "Pass BODY to the R process in SESSION.
   If RESULT-TYPE equals 'output then return a list of the outputs
   of the statements in BODY, if RESULT-TYPE equals 'value then
   return the value of the last statement in BODY, as elisp."
   (if (not session)
       ;; external process evaluation
       (case result-type
         (output (org-babel-eval org-babel-R-command body))
         (value
          (let ((tmp-file (make-temp-file "org-babel-R-results-")))
            (org-babel-eval org-babel-R-command
                            (format org-babel-R-wrapper-method
                                    body tmp-file
                                    (if row-names-p "TRUE" "FALSE")
                                    (if column-names-p
                                        (if row-names-p "NA" "TRUE")
                                      "FALSE")))
            (org-babel-R-process-value-result
             (org-babel-import-elisp-from-file
              (org-babel-maybe-remote-file tmp-file)) column-names-p))))
     ;; comint session evaluation
     (case result-type
       (value
        (let ((tmp-file (make-temp-file "org-babel-R"))
              broke)
          (org-babel-comint-with-output (session org-babel-R-eoe-output)
            (insert (mapconcat
                     #'org-babel-chomp
                     (list
                      body
                      (format org-babel-R-wrapper-lastvar
                              tmp-file
                              (if row-names-p "TRUE" "FALSE")
                              (if column-names-p
                                  (if row-names-p "NA" "TRUE")
                                "FALSE"))
                      org-babel-R-eoe-indicator) "\n"))
            (inferior-ess-send-input))
          (org-babel-R-process-value-result
           (org-babel-import-elisp-from-file
            (org-babel-maybe-remote-file tmp-file))  column-names-p)))
       (output
        (drop-two-lines
         (mapconcat
          #'org-babel-chomp
          (butlast
           (delq nil
                 (mapcar
                  #'identity
                  (org-babel-comint-with-output (session org-babel-R-eoe-output)
                    (insert (mapconcat #'org-babel-chomp
                                       (list (sinkout body) org-babel-R-eoe-indicator)
                                       "\n"))
                    (inferior-ess-send-input)))) 2) "\n"))))))
#+end_src

#+results:
: org-babel-R-evaluate

#+begin_src R :session :results output
cat(" >\n")
1+1
#+end_src

#+results:
:  >
: [1] 2

#+begin_src R :session :results output
   cat(" > start of block < \n" )
   x <-
       rnorm(3)
   x
   y <- as.data.frame(diag(2))
   y+x
   cat(" >end of block< ") # no newline
#+end_src

#+results:
:  > start of block < 
: [1] -1.4569661  0.1888919 -0.3685594
:           V1         V2
: 1 -0.4569661 -0.3685594
: 2  0.1888919 -0.4569661
:  >end of block<

Add an extra newline for :session :results value ?

#+begin_src R :sesison :results value
## use C-g to return after C-c C-c
## a user might do this by mistake:
cat("no newline")
invisible(1+1)
#+end_src

#+results:
: 2

>
> "Charles C. Berry" <cberry@tajo.ucsd.edu> writes:
>
>> Eric,
>>
>> In ob-R.el, changing this
>>
>> 	  (if (string-match "^\\([ >]+\\)\\[[0-9]+\\]" line)
>>
>> to this
>>
>> 	  (if (string-match "^\\([ ]*[>+][ ]?\\)+\\([[0-9]+\\|[ ]\\)" line)
>>
>> seems to fix things (note I added plus sign which is the continuation
>> prompt in R).
>>
>> I tried it on a bunch of varied source blocks and it seems to work OK,
>> but I worry that there may be case where it will fail.
>>
>> Chuck
>>
>>
>> On Tue, 27 Jul 2010, Charles C. Berry wrote:
>>
>>>
>>> Eric,
>>>
>>> I spoke too soon.
>>>
>>> Have a look at the following. You'll see that the prompts show up there. :-(
>>>
>>> Chuck
>>>
>>> #+begin_src R :session :results output
>>> ### create x
>>> x <- data.frame(a=rnorm(2),b=rnorm(2))
>>> ### now print the result
>>> x
>>> #+end_src
>>>
>>> #+results:
>>> :
>>> : > >            a          b
>>> :  1 0.2702748 -0.2938296
>>> :  2 1.1095136  0.1769425
>>>
>>>
>>>
>>> On Tue, 27 Jul 2010, Charles C. Berry wrote:
>>>
>>>>  On Tue, 27 Jul 2010, Eric Schulte wrote:
>>>>
>>>>>   Hi Charles,
>>>>>>   I just pushed up a fix for the extra prompts in your output.
>>>> You will
>>>>>   still have a blank line (the output from "x <- rnorm(1)"), however you
>>>>>   can suppress that line by placing the "x <- rnorm(1)" command in a
>>>>>   previous code block in the same session.
>>>>
>>>>  Eric,
>>>>
>>>>  Thanks. That works for me.
>>>>
>>>>  Chuck
>>>>
>>>>>>   Best -- Eric
>>>>>>   "Charles C. Berry" <cberry@tajo.ucsd.edu> writes:
>>>>>>>   I think this is a new feature or perhaps a bug, which I
>>>> noticed when
>>>>>>   upgrading to version 7.01.
>>>>>>>>   The newline and the two prompts '> >' in the results
>>>> block below did
>>>>>>   not show up in the earlier versions I used.
>>>>>>>>   If this is a 'feature', is there a clean way to change
>>>> this behavior?
>>>>>>>>   If this is a bug, is there a simple patch to fix it?
>>>>>>>>   #+begin_src R :session :results output
>>>>>>   ### create x
>>>>>>   x <- rnorm(1)
>>>>>>   ### now print the result
>>>>>>   x
>>>>>>   #+end_src
>>>>>>>>   #+results:
>>>>>>   :
>>>>>> : > >   [1] 2.186783
>>>>>>>>>>   What I would have liked is to have something that
>>>> looks like this:
>>>>>>>>   #+results:
>>>>>> :   [1] 2.186783
>>>>>>>>   Thanks,
>>>>>>>>   Chuck
>>>>>>>>>>   Charles C. Berry                            (858)
>>>> 534-2098
>>>>>>                                               Dept of
>>>> Family/Preventive > >   Medicine
>>>>>>   E mailto:cberry@tajo.ucsd.edu	            UC San Diego
>>>>>>   http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San
>>>> Diego > >   92093-0901
>>>>>>>>>>>>   _______________________________________________
>>>>>>   Emacs-orgmode mailing list
>>>>>>   Please use `Reply All' to send replies to the list.
>>>>>>   Emacs-orgmode@gnu.org
>>>>>>   http://lists.gnu.org/mailman/listinfo/emacs-orgmode
>>>>>
>>>>
>>>>  Charles C. Berry                            (858) 534-2098
>>>>                                             Dept of Family/Preventive
>>>>  Medicine
>>>>  E mailto:cberry@tajo.ucsd.edu	            UC San Diego
>>>>  http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901
>>>>
>>>>
>>>>
>>>>  _______________________________________________
>>>>  Emacs-orgmode mailing list
>>>>  Please use `Reply All' to send replies to the list.
>>>>  Emacs-orgmode@gnu.org
>>>>  http://lists.gnu.org/mailman/listinfo/emacs-orgmode
>>>>
>>>>
>>>
>>> Charles C. Berry                            (858) 534-2098
>>>                                            Dept of Family/Preventive
>>> Medicine
>>> E mailto:cberry@tajo.ucsd.edu	            UC San Diego
>>> http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901
>>>
>>>
>>>
>>> _______________________________________________
>>> Emacs-orgmode mailing list
>>> Please use `Reply All' to send replies to the list.
>>> Emacs-orgmode@gnu.org
>>> http://lists.gnu.org/mailman/listinfo/emacs-orgmode
>>>
>>
>> Charles C. Berry                            (858) 534-2098
>>                                             Dept of Family/Preventive Medicine
>> E mailto:cberry@tajo.ucsd.edu	            UC San Diego
>> http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry@tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

      reply	other threads:[~2010-08-05  3:11 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-07-26 18:28 avoiding source block prompts Charles C. Berry
2010-07-27 18:18 ` Eric Schulte
2010-07-27 19:40   ` Charles C. Berry
2010-07-27 20:15     ` Prompts again WAS " Charles C. Berry
2010-07-27 22:31       ` Charles C. Berry
2010-07-28 14:03         ` Eric Schulte
2010-08-05  3:09           ` Charles C. Berry [this message]

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://www.orgmode.org/

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

  git send-email \
    --in-reply-to=Pine.LNX.4.64.1008041905500.18329@tajo.ucsd.edu \
    --to=cberry@tajo.ucsd.edu \
    --cc=emacs-orgmode@gnu.org \
    --cc=schulte.eric@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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).