🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: aus.stats.s
Article #16181

Re: Teaching undergrad statistics with S

#16181
From: Rod Ball
Date: Wed, 29 Jan 1992 22:22
189 lines
6702 bytes
Doug Nychka asks :

>    As an alternative to batching the file does anyone know how to 
> source the S commands in a way where the output  looks the same as an 
> interactive session. For example when sourcing a file is it possible


This can be done using an extension to the emacs mode for S written by
Bates, Kademan and Ritter and available from statlib --- one has the
source file in one emacs window and S running in another. The emacs
function S-next-line-and-eval does what you want for one line, you can
type C-cC-n repeatedly in the source window for the desired effect.


I have written an emacs mode for genstat (based on S.el, 1990 version)
which includes a function genstat-eval-buffer-to-next-error to
evaluate the genstat buffer line by line until an error is found, I
have included the code for this in case someone wants to adapt it for S.


Regards,
Rod

Rod Ball			email : rod@marcam.dsir.govt.nz
DSIR Applied Maths		phone : 64-9-893-660 Ext. 6709
P.B. Auckland			fax   : 64-9-863-330
New Zealand



S-next-line-and-eval 
S-mode : Send the current line to the inferior S process, and step
to next line.  Unlike S-eval-line the line itself is sent to the S
process buffer. Useful for evaluating code line by line.



(i) add the line 

(define-key S-mode-map "\C-c\C-n" 'S-next-line-and-eval)

to the define-key statements for S-mode-map i.e. after

;;; S mode
;;;======================================================================
;;;
(defvar S-mode-map nil)
(if S-mode-map
    nil ...

in the file S.el


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Incorporate the following code into S.el:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; rdb (4/3/91)
;; send whole of the current line to the S process and move down 1 line
;; useful for stepping through S source files

(defun S-next-line-and-eval  ()
  "S-mode : Send the current line to the inferior S process, and step
to next line.  Unlike S-eval-line the line itself is sent to the S
process buffer Useful for evaluating code line by line."
  (interactive)
    (end-of-line)
    (let* ((end (point))
	   (cbuffer (current-buffer))
	   (sprocess (get-process "S"))
	   (sbuffer (process-buffer sprocess)))
      (beginning-of-line)
      (message (concat "Loading line: " (extract-word-name) "..."))
      (set-buffer sbuffer)
      (goto-char (process-mark sprocess))
      (let ((insert-point (point)))
	(set-marker (process-mark sprocess) (point-max))
	(set-buffer cbuffer)
	(message (concat "Loading line: " (extract-word-name) "..."))
	(S-eval-region (point) end)
;; stick in the command
;; but make sure last-command not a kill so previous kills not appended
	(setq last-command nil)
	(copy-region-as-kill (point) end)
	(switch-to-buffer-other-window sbuffer)
	(goto-char insert-point)
	(yank)
	(insert-string "\n")
	(set-marker (process-mark sprocess) (point-max))
	(goto-char (process-mark sprocess))
	(switch-to-buffer-other-window cbuffer)))
  (next-line 1))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
the following code could be adapted for S
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define-key genstat-mode-map "\C-cn"    'genstat-eval-buffer-to-next-error)

(defun genstat-eval-buffer-to-next-error (&optional buf)
  "Evaluate commands in buffer from point to next error, leaving cursor
on the line containing the error."
  (interactive)
  (if buf (set-buffer buf))
  (genstat-eval-region-line-by-line  (point)(point-max)))


(defun genstat-eval-region-line-by-line (beg end &optional no-copy-commands
					     no-copy-output &optional do-beep)
  "Send one line at a time from region to the Genstat process. If a fault
 occurs stop execution leaving cursor at the offending line. Returns fault
 message or if no fault, t"
  (interactive "r")
  (goto-char beg)
  (if (not do-beep)
      (setq do-beep genstat-beep-on-error-in-source))
  (let* ((tfilename (concat genstat-scratch-directory
			   "/"
			   (or genstat-work-file (make-temp-name "gwork."))))
	 old-work-buffer
	 (gprocess (get-process "Genstat"))
	 (gbuffer (process-buffer gprocess))
	 (cbuffer (current-buffer))
	 line-start
	 (next-line-start (point-marker))
	 tbuf
	 tpos
	 tend
	 com
	 (msg t)
	 (fault nil))
    (if (setq old-work-buffer (get-buffer tfilename))
	(progn
	  (set-buffer old-work-buffer)
	  (set-buffer-modified-p nil)
	  (kill-buffer old-work-buffer))) ;make sure we start fresh
    (setq tbuf (generate-new-buffer tfilename))
    (set-buffer tbuf)
    (setq tend (copy-marker (point-max)))
    (set-buffer cbuffer)
    (while (and (< next-line-start end) (not fault))
	(setq line-start next-line-start)
	(next-line 1)
	(beginning-of-line)
	(setq next-line-start (point-marker))
	(setq com (buffer-substring line-start next-line-start))
	(set-buffer tbuf)
	(goto-char tend)
	(if no-copy-commands nil (insert-string (concat "> " com)))
	(command-to-genstat com tbuf)
	(setq tpos tend)
	(setq tend (copy-marker (point-max)))
	(setq fault
	      (genstat-find-fault-in-region tpos tend))
	(if fault
	    (progn 
	      (setq genstat-last-error-message (buffer-substring fault tend))))
	(set-buffer cbuffer))
    (if fault (progn
		(next-line -1)
		(beginning-of-line)
		(let ((beg (point))
		      end
		      line-str)
		  (end-of-line)
		  (setq end (point))
		  (setq line-str (buffer-substring beg end))
		  (beginning-of-line)
		  (setq msg (format
			     "Genstat error : %s : %s...(type C-c? for more information)"
			     (what-line)
			     (substring line-str 0 15))))))

    (if no-copy-output nil
      (set-buffer gbuffer)
      (goto-char (point-max))
      (beginning-of-line)
      (while (re-search-forward inferior-genstat-prompt nil t)
	(delete-region (match-beginning 0)(match-end 0)))
      (insert-buffer tbuf)
      (set-marker (process-mark gprocess) (point-max)))
    (set-buffer cbuffer)
    (if do-beep (beep))
    (flash-line 1 0)
    msg))

		     
D
Technical Details
Message-ID: <9201292150.AA15751@kahu.marcam.dsir.govt.nz>
Path: arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!uniwa!dmsperth!dmssyd.syd.dms.CSIRO.AU!news

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: fa931,dfc81fccdbfd0229
X-Google-Attributes: gidfa931,public
X-Google-ArrivalTime: 1992-01-29 14:53:07 PST
Path: arc1-ca.pugleaf.net!sparky!uunet!munnari.oz.au!uniwa!dmsperth!dmssyd.syd.dms.CSIRO.AU!news
From: rod@tui.marcam.dsir.govt.nz (Rod Ball)
Newsgroups: aus.stats.s
Subject: Re:  Teaching undergrad statistics with S
Message-ID: <9201292150.AA15751@kahu.marcam.dsir.govt.nz>
Date: 29 Jan 92 22:22:54 GMT
Sender: daemon@syd.dms.CSIRO.AU
Organization: CSIRO Div. Maths & Stats, CSIRO Div. Applied Physics
Lines: 188
X-Gateway: dmssyd.syd.dms.CSIRO.AU