Posts

Build R project using Make

Image
My default for building projects is to use make . I find it helps organise a projects workflow, even if there are other build tools available. It is succinct, and descriptive. A previous post showed how you can use make to build Haskell projects using GHC, Cabal and Stack. This blog show how to do the same for R projects. Below is an R script that can be used to render R markdown to PDF. It is invoked from a parent Makefile which is what is read when you call make. make.R #!/usr/bin/env R   # Render R markdown to PDF. # Invoke with: # > R -q -f make.R --args my_report.Rmd   # load packages require ( rmarkdown )   # require a parameter naming file to render if ( length ( args ) == 0 ) { stop ( "Error: missing file operand" , call. = TRUE ) } else { # read report to render from command line for ( rmd in commandArgs ( trailingOnly = TRUE ) ) { # render Rmd to PDF if ( grepl ( " \\ .Rmd$" , rmd ) &...

Render Jupyter Notebook to HTML or PDF

The following shows how to render a  Jupyter Notebook  into a static form, such as HTML or PDF document.  Render to HTML To render a Jupyter Notebook to HTML: jupyter nbconvert --exec --to html --output document.html document.ipynb You can also specify an output directory: jupyter nbconvert --exec --to html --output document.html --output-dir target document.ipynb The HTML has all images embedded. This makes sharing easy is the HTML is fully self contained. Render to PDF I have had mixed results converting a notebook directly to PDF. The conversion complained that embedded images were not found. Instead I suggest first converting it to LaTeX and then to a PDF. To convert to Tex run: jupyter nbconvert --exec --to latex --output document.tex document.ipynb Now, it is an easy matter to convert TeX to PDF with pdflatex : pdflatex document.tex Build using Makefile A generic Makefile that can be used in a Jupyter project to perform ...

Build Haskell projects using Make

Image
Make has been around for a long time . It does, however, have a moderate learning curve. Once you understand and overcome some of its idiosyncrasies, it is a very powerful generic build tool. Below are my variants for building Haskell projects using make. Most of my projects now use Stack . Even so, having a Makefile helps to coordinate building project artefacts. It becomes an organising script for the project. A full description on building modules can be found in the chapter Using Make in the GHC documentation . For a single target, you can use make to build a Haskell project. Make is whitespace sensitive. You must use tabs. The default is to use a tab setting of 8 spaces. GHC Below is a version using just GHC for compilation. We are defining a custom pattern to build from Haskell source (.hs) to an object (.o). This rule is cheating, as the end result is actually a executable, but as I didn't want to add a .exe extension, we are 'faking' it with testi...

Clear GPS metadata from an image

Image
In March 2012 I wrote a blog titled,  GPS Information in photos . In it I describe how to extract and resolve Exif GPS data to a map reference. Recently I wrote an update to that blog that included how to clear metadata from an image. Essentially, the two methods I would use are: (1) Using ImageMagick with convert gps.jpg -strip stripped.jpg This  appears to re-compress the JPEG image which may not be what you want. (2) So, there is an alternative tool such as the exiv2 utility. I've not used this myself, but it seems to have great command line options.

Automation with Ansible

Ansible is a Python based automation tool. I started using it to revert changes made on my local machine every time there were package updates. That is I wanted to ensure my options and settings were preserved. Professionally my team uses Puppet but we were looking at alternatives. Ansible was chosen for a variety of reasons: it is easy to learn, has broad functionality and an active community. There is next to no new lingo to learn: a task is an action you want to perform a list of tasks can be organised into a playbook handlers are special tasks that are triggered by a change in a task a group of playbooks is organised into roles an inventory is a list of hosts (or group of hosts) to apply a playbook to playbooks are under the roles directory tasks are stored in the tasks directory, under a role variables are stored in the vars directory, under a role templates are stored in templates directory, under a role So, any typical Ansible project would look something like: ...

Converting MS Documents to PDF using LibreOffice

I've been a long time user of LibreOffice . I first started with StarOffice , before moving to  OpenOffice , and finally the latest incarnation,  LibreOffice . While I use office suites less, preferring Wiki 's and if a versioned document is required, LaTeX , I still on occasion receive MS documents. Rather than reading these documents in MS-Word or MS-PowerPoint formats I prefer to convert to a PDF. Here is how to do that. The following shows how to convert a presentation to a PDF. It is a headless operation that works just as well over a set of documents. loimpress --headless --convert-to pdf presentation.pptx Similarly, lowriter --headless --convert-to pdf document.doc

Render R markdown to PDF from command line

I have been using Makefiles where command line tools for building a project are not generally available. This includes LaTeX documents and recently R projects. So here is an R script that can be used to render R markdown to PDF. #!/usr/bin/env R # Render R markdown to PDF. # Invoke with: # > R -q -f make.R --args my_report.Rmd # load packages require ( rmarkdown ) # require a parameter naming file to render if ( length ( args ) == 0 ) { stop ( "Error: missing file operand" , call. = TRUE ) } else { # read report to render from command line for ( rmd in commandArgs ( trailingOnly = TRUE )) { # render Rmd to PDF if ( grepl ( " \\ .Rmd$" , rmd ) && file.exists ( rmd )) { render ( rmd , pdf_document ()) } else { print ( paste ( "Ignoring: " , rmd )) } } } It will process Rmd files listed after the --args parameter. This script can t...