Rstudio and makefiles: Mind your options!

February 14, 2014

(This article was first published on Landscape Ecology 2.0 » R, and kindly contributed to R-bloggers)

I have written this post mostly for myself. I don’t want to waste 2 hours on this problem again at some point in the future.  Hopefully others might stumble on it too and save some aggravation.

So, the issue I had this morning was writing up a a makefile in RStudio.  I am new to make and makefiles, but have been able to get them running successfully in the past; however, the makefiles I’ve used were mostly borrowed from others and only minor edits were made.  This morning I was trying to add a new target and some dependencies.  As most of my work these days is with R, I prefer to do most of my writing and editing directly in RStudio.  Thus, I fire up RStudio, penned some great prose in R Markdown, and then went to add a new target to a makefile that was to take my R Markdown file, and create an output .pdf and .docx.  Seemed simple enough.

Here’s the makefile like the one I created:

all: file.pdf file.docx file.Rmd
  Rscript -e "library(knitr); knit('file.Rmd')"

  pandoc -H format.sty -V fontsize=12pt -o file.pdf

  pandoc -s -S --reference-docx=format.docx -o file.docx

Looks pretty good!  Or so I thought.  I sauntered on over to my terminal window, and confidently type make and, it runs the first target fine, and then stops… Next step for me was Google and I was rewarded with A Brief Intro to Makefiles. Ah ha! My problem is the tabs. I then head back to RStudio, dutifully replace what I thought were spaces with tabs, run make. Same problem.

This next step took me a while of trying the same thing over and over again, but expecting something different. Say what you will about me. After this brief foray into insanity I then realized that I had my options in RStudio set to replace tabs with spaces. Again, I think I have the problem licked. In RStudio, I go to Tools:Global Options:Code Editing and click off my “Insert spaces for tab” option. That looks like:

After making this change, I once again replace my spaces with tabs, try again, and still no good.  At this point I am sure I know the problem is related to the spaces, but for the life of me I can’t figure out why RStudio tabs are still getting input as spaces.  I give up and copy/paste everything into notepad++, reset my tab options there and go about replacing the tabs.  This time it works.

While I got my makefile working, it was a very unsatisfying solution.  I kept digging and am pleased to report RStudio is not to blame (I never thought it was. User error all along).  As is turns out there is an additional Rstudio Project level option that also controls the tabs.  I have now checked off Tools:Project Options: Code Editing: Insert spaces for tab. That looks like:

At long last my tabs are tabs and my makefile makes and it can all be done in RStudio, provided I take care of my options.

Filed under: Open Science, Programming, R, Things I Forget Tagged: knitr, make, pandoc, R Markdown, tabs to spaces

To leave a comment for the author, please follow the link and comment on their blog: Landscape Ecology 2.0 » R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training


CRC R books series

Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)