Rstudio and makefiles: Mind your options!

[This article was first published on Landscape Ecology 2.0 » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.md file.pdf file.docx

file.md: file.Rmd
  Rscript -e "library(knitr); knit('file.Rmd')"

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

file.docx: file.md
  pandoc -s -S --reference-docx=format.docx file.md -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:
GlobalOptions

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:
ProjectOptions

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.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)