spacemacs ess knitr doesn't work with Rmd files - r

I really want to switch from RStudio to Spacemacs with ess layer. However I can't get it to work. I have the following in my dotfile:
dotspacemacs-configuration-layers
'(
html
;; ----------------------------------------------------------------
;; Example of useful layers you may want to use right away.
;; Uncomment some layer names and press <SPC f e R> (Vim style) or
;; <M-m f e R> (Emacs style) to install them.
;; ----------------------------------------------------------------
markdown
helm
ess
auto-completion
better-defaults
emacs-lisp
git
polymode
mu4e
;; markdown
;; (shell :variables
org
;; shell-default-height 30
;; shell-default-position 'bottom)
spell-checking
syntax-checking
;; version-control
)
I have the following packages.el in my .emacs.d private folder copied with thanks from github.
;;; packages.el --- polymode layer packages file for Spacemacs.
;;
;; Copyright (c) 2012-2016 Sylvain Benner & Contributors
;;
;; Author: Walmes Zeviani & Fernando Mayer
;; URL: https://github.com/syl20bnr/spacemacs
;;; Code:
(defconst polymode-packages
'(polymode))
(defun polymode/init-polymode ()
(use-package polymode
:mode (("\\.Rmd" . Rmd-mode))
:init
(progn
(defun Rmd-mode ()
"ESS Markdown mode for Rmd files"
(interactive)
(require 'poly-R)
(require 'poly-markdown)
(R-mode)
(poly-markdown+r-mode))
))
)
;;; packages.el ends here
Syntax highlighting and code completeion etc are working fine but if I try and send code chunks to REPL i get 'wrong type argument stringp nil' error with both Rmd and Rmv files.
emacs 25.2.1 with spacemacs
fresh install on both mac and linux with no other config loaded. Own .emacs has been deleted
some functions seem to work but not like they should e.g. eval-buffer will just send the current code chunk to REPL
Very grateful for any help.

It was a bit tricky to me to make it works with Rmd. Have you try with a simple .R file to see if it works?
This is what I currently have in mi .emacs file for the Rmd files
;; MARKDOWN
(add-to-list 'auto-mode-alist '("\\.md" . poly-markdown-mode))
;; R modes
(add-to-list 'auto-mode-alist '("\\.Snw" . poly-noweb+r-mode))
(add-to-list 'auto-mode-alist '("\\.Rnw" . poly-noweb+r-mode))
(add-to-list 'auto-mode-alist '("\\.Rmd" . poly-markdown+r-mode))
;; polymode
(setq load-path (append '("/home/fer/.emacs.d/elpa/polymode-20170307.322"
"/home/farce/.emacs.d/polymode/modes") load-path))
(require 'poly-R)
(require 'poly-markdown)
(add-to-list 'auto-mode-alist '("\\.Rmd" . poly-markdown+r-mode))

Related

Start ESS process in R script

I've got the following problem in my Emacs (i am a complete newby and way far from LISP programming): When i open a .R file, no ESS process is starting. This is a problem because i want to configure TAB-completion and see R Documentation in scripts.
This is the Error message:"No ESS process associated with current buffer
"
This is only the case in .R files. When starting inferior ESS i dont have this problem. (hope i got this right: M-x R starts a process und Emacs and is therefore called inferior?)
My .emacs is cofigured like this:
(require 'auto-complete)
(require 'auto-complete-config)
(require 'ess-stata-mode)
(require 'ess-eldoc)
(add-hook 'inferior-ess-mode-hook 'ess-use-eldoc)
(autoload 'R-mode "ess-site.el" "" t)
(add-to-list 'auto-mode-alist '("\\.R\\'" . R-mode))
Those two lines above should start ESS in script, shouldn't they?
(global-auto-complete-mode t)
(setq ess-tab-complete-in-script t)
(setq ess-use-auto-complete 't)
(setq ess-use-eldoc 't)
(setq ess-use-company 't)
And I included ElDoc and AC to get Autocompletion.
I want to get to see the Documentation of R in Emacs.
I'd really appreciate your help. What did I miss?
Greetings and Thanks in advance

ESS and R from spacemacs

I hope this isn't a really dumb question, but I've used emacs in the past with ESS to edit R files and run them in an R process. I'd like to do the same with spacemacs. As far as I can read and grasp, it just means I have to enable the ess layer in the .spacemacs file, and that's it.
So in my .spacemacs I have the following:
'(
;; ----------------------------------------------------------------
;; Example of useful layers you may want to use right away.
;; Uncomment some layer names and press <SPC f e R> (Vim style) or
;; <M-m f e R> (Emacs style) to install them.
;; ----------------------------------------------------------------
;; auto-completion
;; better-defaults
emacs-lisp
;; git
markdown
javascript
colors
haskell
c-c++
ess
html
;; org
;; (shell :variables
;; shell-default-height 30
;; shell-default-position 'bottom)
;; spell-checking
;; syntax-checking
;; version-control
)
But if I close spacemacs then, and reboot, and try to create say an R file, there's no ess or formatting/colours or anything like that. The same for something like markdown too. All files are just opened in Fundamental mode. I thought spacemacs had layers so you can just add them and it work out of the box. So what am I missing or what should I check? I'm on OSX.
Thanks,
Ben.

ESS: retrieving command history from commands entered in ESS[R] inferior mode or script

When using ESS[R] in inferior mode I can retrieve the most recent command output using C-c C-p which moves the cursor to the previous command output. Alternatively I can use C-up which essentially copies the most recently entered command from the inferior process (similar to how readline works). I prefer the C-up approach but unfortunately it does not retrieve commands that were entered from a script using any of the ess-eval commads. Is there to get the functionality of C-up for commands entered in both inferior mode and by ess-eval?
Your solution works fine for single line commands, but needs a small tweak to handle multi-line statements:
(defun ess-readline ()
"Move to previous command entered from script *or* R-process and copy
to prompt for execution or editing"
(interactive)
;; See how many times function was called
(if (eq last-command 'ess-readline)
(setq ess-readline-count (1+ ess-readline-count))
(setq ess-readline-count 1))
;; Move to prompt and delete current input
(comint-goto-process-mark)
(end-of-buffer nil) ;; tweak here
(comint-kill-input)
;; Copy n'th command in history where n = ess-readline-count
(comint-previous-prompt ess-readline-count)
(comint-copy-old-input)
;; Below is needed to update counter for sequential calls
(setq this-command 'ess-readline)
)
(global-set-key (kbd "\C-cp") 'ess-readline)
This enables you to move up through the previous commands; the following function enables you to move back down again, so you can go up and down to find the command you're after:
(defun ess-readnextline ()
"Move to next command after the one currently copied to prompt and copy
to prompt for execution or editing"
(interactive)
;; Move to prompt and delete current input
(comint-goto-process-mark)
(end-of-buffer nil)
(comint-kill-input)
;; Copy (n - 1)'th command in history where n = ess-readline-count
(setq ess-readline-count (max 0 (1- ess-readline-count)))
(when (> ess-readline-count 0)
(comint-previous-prompt ess-readline-count)
(comint-copy-old-input))
;; Update counter for sequential calls
(setq this-command 'ess-readline)
)
(global-set-key (kbd "\C-cn") 'ess-readnextline)
It doesn't seem that there is any built in function to do this so I wrote the function below. Essentially this function automates the process of using the keystokes C-c C-p to move the cursor to a previous command in the R-process buffer followed by C-c RET to copy that command to the prompt for editing
(defun ess-readline ()
"Move to previous command entered from script *or* R-process and copy
to prompt for execution or editing"
(interactive)
;; See how many times function was called
(if (eq last-command 'ess-readline)
(setq ess-readline-count (1+ ess-readline-count))
(setq ess-readline-count 1))
;; Move to end of buffer and delete current input
(end-of-buffer nil)
(comint-kill-input)
;; Copy n'th command in history where n = ess-readline-count
(comint-previous-prompt ess-readline-count)
(comint-copy-old-input)
;; Below is needed to update counter for sequential calls
(setq this-command 'ess-readline)
)
(global-set-key (kbd "\C-cp") 'ess-readline)

Can I specify directory for shell command?

I use the following function to run shell commands:
(defun sh (cmd)
#+clisp (shell cmd)
#+ecl (si:system cmd)
#+sbcl (sb-ext:run-program "/bin/sh" (list "-c" cmd) :input nil :output*standard-output*)
#+clozure (ccl:run-program "/bin/sh" (list "-c" cmd) :input nil :output*standard-output*)))
For example, How to specify the current directory for command python -m CGIHTTPServer ?
Sincerely!
In ECL you can use EXT:CHDIR before SYSTEM, which changes both default-pathname-defaults and the value of the current directory, as understood by the operating system and C library.
BTW: If possible use (EXT:RUN-PROGRAM "command" list-of-args) instead
A more portable way would be to use pathnames and dynamically binding *default-pathname-defaults*, which would effectively set your current working directory. I had the same problem today. Here is a working adaptation of dot->png from Land of Lisp text by Conrad Barski, that specifies the current working directory:
(defun dot->png (filespec thunk)
"Save DOT information generated by a thunk on a *STANDARD-OUTPUT* to a FILESPEC file. Then use FILESPEC to create a corresponding png picture of a graph."
;; dump DOT file first
(let ((*default-pathname-defaults*
(make-pathname :directory (pathname-directory (pathname filespec)))))
;; (format t "pwd (curr working dir): ~A~%" *default-pathname-defaults*)
(with-open-file (*standard-output*
filespec
:direction :output
:if-exists :supersede)
(funcall thunk))
#+sbcl
(sb-ext:run-program "/bin/sh"
(list "-c" (concatenate 'string "dot -Tpng -O " filespec))
:input nil
:output *standard-output*)
#+clozure
(ccl:run-program "/bin/sh"
(list "-c" (concatenate 'string "dot -Tpng -O" filespec))
:input nil
:output *standard-output*)))
Posted in the hope that this could be useful to someone in a similar situation & running across this thread.

Smalltalk equivalent to Common Lisp's #| ... |# comments?

Common Lisp's multiline comments make it easier to include multiline shebangs:
#!/bin/bash
#|
exec clisp -q -q $0 $0 ${1+"$#"}
exit
|#
;;; Usage: ./scriptname.lisp
(defun main (args)
(let ((program (car args)))
(format t "Program: ~a~%" program)
(quit)))
Without this syntax, only very simple shebangs can be used. Is there a pound-based multiline comment for Smalltalk that would facilitate multiline shebangs?
It doesn't NEED to be multi-line; as long as bash can see "into" the Smalltalk comment, you should be okay. I'd be more worried about Smalltalk seeing past the #!
How about something based on:
"exec" "/usr/bin/gst" "--foo" "$0" "--bar" "$#"
There's no need for exit unless you expect the exec ever to fail. Make sure your Smalltalk is where you expect it to be! If you do want the safety valve:
"exec" ...
"exit"
Having to double-quote arguments can get in the way, of course. Double-quoting shell operators breaks their specialness, for example.
"echo" "hello" ">" "/dev/null"
just prints "hello > /dev/null"

Resources