Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

include ggmap using knitr

Tags:

r

knitr

ggmap

I am trying to include a map using the ggmap library in knitr:

library(ggmap)
murder = subset(crime, offense='murder')
qmplot(lon, lat, data=murder)

It runs fine outside of knitr, but when I try to run this code chunk in knitr, I get the error:

Error in UseMethod("depth"): no applicable method for 'depth' applied to an object of class "NULL"

And there is no map in the final PDF

I tried wrapping the qmplot line inside of evaluate(...), but that didn't work


Output of library(knitr); sessionInfo()

> library(knitr)
> sessionInfo()
version 3.0.1 (2013-05-16)
Platform: i686-redhat-linux-gnu (32-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5]   LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8      
 [7] LC_PAPER=C                 LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C               
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  utils     datasets  grDevices methods   base     

other attached packages:
[1] knitr_1.2      vimcom_0.9-8   setwidth_1.0-3

loaded via a namespace (and not attached):
[1] digest_0.6.3   evaluate_0.4.3 formatR_0.7    stringr_0.6.2  tools_3.0.1  

I also tried running the same code via Rscript (i.e. not from within VIM) with the following:

Rscript -e "library(knitr); knit('map.Rnw')"

And I get a similar error:

Error in UseMethod("depth") : 
no applicable method for 'depth' applied to an object of class "NULL"
Calls: knit ... grid.draw -> grid.draw.gTableChild -> upViewport -> depth

Using ggmap v 2.3 and ggplot v 0.9.3.1

like image 244
Mark McFarland Avatar asked Jun 01 '13 15:06

Mark McFarland


People also ask

What is the functionality of knitr package?

It is a package in the programming language R that enables integration of R code into LaTeX, LyX, HTML, Markdown, AsciiDoc, and reStructuredText documents. The purpose of knitr is to allow reproducible research in R through the means of literate programming. It is licensed under the GNU General Public License.

What is knitr in RMarkdown?

RMarkdown is an extension to markdown which includes the ability to embed code chunks and several other extensions useful for writing technical reports. The rmarkdown package extends the knitr package to, in one step, allow conversion between an RMarkdown file (.Rmd) into PDF, HTML, word document, amongst others.


1 Answers

I found the problem: I was using the tikz device to include the map in my final document. That tikz device doesn't seem to work for maps (or at least larger maps).

When I changed the device to pdf or png, and the map was included in my document with no problems.

like image 122
Mark McFarland Avatar answered Nov 25 '22 18:11

Mark McFarland