Build R package from GNU Emacs on Windows 10

I use Windows 10 machine, at work, with GNU Emacs installed from choco repository. Script from my article Run shiny R application from Emacs was not working like in my old Windows 7. First thing is that it was not founding my R binary and I’ve got error:

apply: Spawning child process: Invalid argument

First thing was adding path to R binary second issue was path to Root of the package

Here is first function based on the one from previous article:

(defun call-r-command (command-list)
  "run R command in shell buffer window

  if there is displayed buffer that have shell it will use that window"
  (let* ((name "*R*")
         (new-buffer (get-buffer-create name))
         (old-buff (get-buffer name))
         ;; run R command
         (script-proc-buffer
          (apply 'make-comint-in-buffer
                 "script"
                 new-buffer
                 "c:/Program Files/R/R-3.5.2/bin/R.exe"
                 nil
                 command-list))
         ;; if there is *R* buffer try to find its window
         (old-window (if old-buff (get-buffer-window old-buff)))
         ;; use old window or find any that have shell mode
         (window (or old-window
                     (get-window-with-mode '(comint-mode eshell-mode))))
       (script-proc (get-buffer-process script-proc-buffer)))
    ;; reuse window or create new one
    (if window
        (set-window-buffer window new-buffer)
      (switch-to-buffer-other-window new-buffer))))

This function execute R command, so my new shiny function is shorter

(defun shiny ()
  "run shiny R application in shell buffer"
  (interactive)
  (let ((R (concat "shiny::runApp('" default-directory "')")))
    (call-r-command `("-e" ,R "--no-save"))))

Next is function that return root of git repo (since my R package is also git repo):

(defun shell-line (command)
  "Function execute shell command and return the result as single line"
  (replace-regexp-in-string "\n" "" (shell-command-to-string command)))

The problem with git on Windows 10 in GNU Emacs (or because of shell-command-to-string function) is that path is in Unix format not in Windows format, so I need to fix the path to use it in my R calling script.

(defun git-root-repo ()
  "Function return root of the git repo"
  (interactive)
  (replace-regexp-in-string "/mnt/c/" "c:/" (shell-line "git rev-parse --show-toplevel")))

Now my final function that build the R package:

(defun build-r-package ()
  "Build R package using git root directory as path to R package"
  (interactive)
  (call-r-command `("CMD" "INSTALL" ,(git-root-repo))))

Missing function for finding buffer with same major mode can be found in my previous article Run shiny R application from Emacs.

Convert pdf to jpg for free on windows using open source tools

To convert pdf to jpg on windows you can use existing open source tools ImageMagic and GhostScript.

Get and Install latest ImageMagic from:

http://www.imagemagick.org/script/binary-releases.php#windows

Ensure that you install with OLE support (for scripting with VBScript)

Get and install latest GPL version of Ghostcript from sourceforge

http://sourceforge.net/projects/ghostscript/files/GPL%20Ghostscript/

Unfortunately there is no GUI for ImageMagic but you can use script below, just save it in pdf2jpg.vbs – to run the script just drag all pdf files that you want to convert on script icon. The resulting images will have the same name but with jpg extension and they will be placed in the same directory as pdf files.

Set imageMagick = CreateObject("ImageMagickObject.MagickImage.1")
For Each name in Wscript.Arguments
       pdf = Replace(name, ".pdf", ".jpg")
       imageMagick.Convert name, "-resize", 650, "-quality", 80, pdf
next

With ImageMagic you can do lots of cool stuff like create, edit, compose, convert bitmap images, scale, flip, mirror, rotate, distort, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves.

For more information see command line and COM+ ImageMagickObject documentations.

Instead of VBScript you can use other languages.