[ESS] Helper function for assignInNamespace?
Seth Falcon
sethfalcon at gmail.com
Sun Jan 22 21:27:59 CET 2006
On 22 Jan 2006, blindglobe at gmail.com wrote:
> It would be cool to also modify the prompt, ala Common Lisp/SLIME,
> to display the "guess" (it's not a guess in CL, where you'd just
> (in-package BLAH) or probe *package* to switch).
It isn't as clear to me how this would be useful. In CL, if you are
"in package BLAH", then new definitions go in BLAH. In R, regardless
of the search() path, new definitions go in the GlobalEnv.
> But what hints would you use to guess the current package in R?
> Search path, or?
For the purpose of sending functions from a source code buffer to the
R process, I think using the buffer filename does what I would want
most of the time (hacking R packages).
I can imagine that doing something like you are suggesting might allow
for developing packages with NAMESPACES from scratch interactively.
Is that the idea?
Here's a first draft (comments on style and other improvements most
welcome):
(defun syf/ess-guess-current-package ()
"Guess the name of the package belonging to the current
source file"
(interactive)
(let* ((filename (buffer-file-name))
(parent-dir (file-name-directory filename))
(package-dir (expand-file-name (concat parent-dir ".."))))
(when (and (string-match "\\.R$" filename)
(string-match "R/$" parent-dir)
(file-exists-p (concat package-dir "/NAMESPACE")))
(file-name-nondirectory package-dir))))
(defun syf/ess-eval-function-ns (vis)
"Send the current function to the inferior ESS process as a
call to assignInNamespace. The package is guessed from the
file path of the current buffer. Ignores vis arg."
(interactive "P")
(save-excursion
(let* ((beg-end (ess-end-of-function))
(beg (nth 0 beg-end))
(end (nth 1 beg-end))
(package (syf/ess-guess-current-package))
name
code
beg-def)
(goto-char beg)
(setq name (ess-extract-word-name))
(setq beg-def (string-match "function" (buffer-substring beg end)))
(setq beg-def (+ beg beg-def))
(setq code (concat "assignInNamespace(\"" name "\", "
(buffer-substring beg-def end)
", ns=\"" package "\")"))
;;(message code)
(ess-force-buffer-current "Process to load into: ")
(message "Starting evaluation...")
(let ((sprocess (get-ess-process ess-current-process-name)))
(process-send-string sprocess code)
(process-send-string sprocess "\n"))))
(message "Finished evaluation"))
More information about the ESS-help
mailing list