Skip to content

Instantly share code, notes, and snippets.

@dmontaner
Last active September 16, 2020 15:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dmontaner/7871774 to your computer and use it in GitHub Desktop.
Save dmontaner/7871774 to your computer and use it in GitHub Desktop.
How to set up a Nemo (file manager) Actions to execute an R script in BATCH mode. Knitr spin based versions included.

This files and scripts are used to set up some actions for the Nemo file manager. Those actions will let you execute an R script in BATCH mode but using the Nemo graphical interface.

The first action uses the Linux shell command R CMD BATCH and will produce an .Rout file (besides the script results).

The two last action use knitr::spin and Pandoc to create HTML or PDF log/report files.

##nemo_action_r_cmd_batch.nemo_action
##2013-12-09 dmontaner@cipf.es
##Nautilus action file to execute R CMD BATCH
## Custom action files (.nemo_action) may be located at:
## - /usr/share/nemo/actions/
## - $HOME/.local/share/nemo/actions/
## Icon-Name: must be the name of the file containing the image without extension.
## Must be located some of the icon folders of the system.
## In my Xfce this means: /usr/share/icons
## Selection: What type selection: [S]ingle, [M]ultiple, Any, NotNone, None (background click),
## or a number representing how many files must be selected to display.
## Defaults to Single if this field is missing.
## Extensions: What extensions to display on - this is an array, end with a semicolon
## Use "dir" for directory selection and "none" for no extension
## Use "any" by itself, semi-colon-terminated, for any file type
## Extensions are NOT case sensitive. jpg will match JPG, jPg, jpg, etc...
[Nemo Action]
Active=true
Name=R CMD BATCH
Comment=Runs an R script in batch mode
Exec=/home/dmontaner/bin/000_david/nemo_action_r_cmd_batch.sh %F
Icon-Name=Rlogo
Selection=Single
Extensions=r;
#!/bin/sh
#nemo_action_r_cmd_batch-sh
#2013-12-09 dmontaner@cipf.es
#execute an R scripts in R CMD BATCH from Nemo
## Argument $1 should catch %F, the full path to the file to be executed.
mydir=`dirname $1`
cd $mydir
R CMD BATCH --no-save --no-restore $1
##nemo_action_spin_html.nemo_action
##2014-04-29 dmontaner@cipf.es
##Nautilus action file to run knitr::spin
## Custom action files (.nemo_action) may be located at:
## - /usr/share/nemo/actions/
## - $HOME/.local/share/nemo/actions/
## Icon-Name: must be the name of the file containing the image without extension.
## Must be located some of the icon folders of the system.
## In my Xfce this means: /usr/share/icons
## Selection: What type selection: [S]ingle, [M]ultiple, Any, NotNone, None (background click),
## or a number representing how many files must be selected to display.
## Defaults to Single if this field is missing.
## Extensions: What extensions to display on - this is an array, end with a semicolon
## Use "dir" for directory selection and "none" for no extension
## Use "any" by itself, semi-colon-terminated, for any file type
## Extensions are NOT case sensitive. jpg will match JPG, jPg, jpg, etc...
[Nemo Action]
Active=true
Name=knitr spin HTML
Comment=Runs knitr spin to crate an html file.
Exec=/home/dmontaner/bin/000_david/nemo_action_spin_html.r %F
Icon-Name=Rlogo
Selection=Single
Extensions=r;
#! /usr/local/bin/Rscript --vanilla
##nemo_action_spin_html.r
##2014-04-29 dmontaner@cipf.es
##This script uses knitr::spin to process an R file.
##Pandoc is may be used to obtain a PDF file.
## I use hidden variables in this script (names start with dot)
## so that rm in the compiled script does not clear them.
## In spin
## format = "Rmd" -> always creates the file .Rmd
## knit = TRUE -> creates the file .md
## report = TRUE -> creates the file .html
## R.version.string ##"R version 3.0.2 (2013-09-25)"
## packageDescription ("knitr", fields = "Version") #"1.5"
library (tools)
library (knitr)
opts_chunk$set (echo = FALSE, comment = "", results = "hide") ## results = "markup" results = "hide" results = "asis"
## input file
.inFile = commandArgs (trailingOnly = TRUE)[1]
.baFile <- file_path_sans_ext (.inFile)
.mdFile <- paste0 (.baFile, ".md")
.rmdFile <- paste0 (.baFile, ".Rmd")
## wd for knitr; otherwhise it works at home
setwd (dirname (.inFile))
## spin HTML
.htmlFile <- paste0 (.baFile, ".html")
unlink (.htmlFile)
spin (.inFile, knit = TRUE, report = TRUE, format = "Rmd") ##html
unlink (c (.rmdFile, .mdFile))
## ## spin PDF
## .pdfFile <- paste0 (.baFile, ".pdf")
## unlink (.pdfFile)
## spin (.inFile, knit = TRUE, report = FALSE, format = "Rmd") ##just md
## system (paste ("pandoc -V geometry:margin=25mm -o", .pdfFile, .mdFile))
## unlink (c (.rmdFile, .mdFile))
## exit
q ("no")
##nemo_action_spin_html.nemo_action
##2014-04-29 dmontaner@cipf.es
##Nautilus action file to run knitr::spin
## Custom action files (.nemo_action) may be located at:
## - /usr/share/nemo/actions/
## - $HOME/.local/share/nemo/actions/
## Icon-Name: must be the name of the file containing the image without extension.
## Must be located some of the icon folders of the system.
## In my Xfce this means: /usr/share/icons
## Selection: What type selection: [S]ingle, [M]ultiple, Any, NotNone, None (background click),
## or a number representing how many files must be selected to display.
## Defaults to Single if this field is missing.
## Extensions: What extensions to display on - this is an array, end with a semicolon
## Use "dir" for directory selection and "none" for no extension
## Use "any" by itself, semi-colon-terminated, for any file type
## Extensions are NOT case sensitive. jpg will match JPG, jPg, jpg, etc...
[Nemo Action]
Active=true
Name=knitr spin PDF
Comment=Runs knitr spin to crate an html file.
Exec=/home/dmontaner/bin/000_david/nemo_action_spin_pdf.r %F
Icon-Name=Rlogo
Selection=Single
Extensions=r;
#! /usr/local/bin/Rscript --vanilla
##nemo_action_spin_html.r
##2014-04-29 dmontaner@cipf.es
##This script uses knitr::spin to process an R file.
##Pandoc is may be used to obtain a PDF file.
## I use hidden variables in this script (names start with dot)
## so that rm in the compiled script does not clear them.
## In spin
## format = "Rmd" -> always creates the file .Rmd
## knit = TRUE -> creates the file .md
## report = TRUE -> creates the file .html
## R.version.string ##"R version 3.0.2 (2013-09-25)"
## packageDescription ("knitr", fields = "Version") #"1.5"
library (tools)
library (knitr)
opts_chunk$set (echo = FALSE, comment = "", results = "hide") ## results = "markup" results = "hide" results = "asis"
## input file
.inFile = commandArgs (trailingOnly = TRUE)[1]
.baFile <- file_path_sans_ext (.inFile)
.mdFile <- paste0 (.baFile, ".md")
.rmdFile <- paste0 (.baFile, ".Rmd")
## wd for knitr; otherwhise it works at home
setwd (dirname (.inFile))
## ## spin HTML
## .htmlFile <- paste0 (.baFile, ".html")
## unlink (.htmlFile)
## spin (.inFile, knit = TRUE, report = TRUE, format = "Rmd") ##html
## unlink (c (.rmdFile, .mdFile))
## spin PDF
.pdfFile <- paste0 (.baFile, ".pdf")
unlink (.pdfFile)
spin (.inFile, knit = TRUE, report = FALSE, format = "Rmd") ##just md
system (paste ("pandoc -V geometry:margin=25mm -o", .pdfFile, .mdFile))
unlink (c (.rmdFile, .mdFile))
## exit
q ("no")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment