mkoohafkan / kfigr

A streamlined cross-referencing package designed specifically for use with knitr.
GNU General Public License v3.0
25 stars 10 forks source link

"First reference must define the type." on Check #5

Open ldecicco-USGS opened 9 years ago

ldecicco-USGS commented 9 years ago

I've got a bunch of vignettes that I'm converting from Rnw to Rmd thanks to this package. Individually, they are all Kniting just fine. However, when I use devtools::build_vignettes() or just run a 'check', I get errors:

failed with diagnostics:
First reference must define the type.

Each vignette starts like this:


---
title: "xxx"
author: "xxx"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
  rmarkdown::html_vignette:
    fig_height:  6
    fig_width: 6
    toc: yes
    fig_caption: yes
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteIndexEntry{xxx}
  \usepackage[utf8]{inputenc}

---

```{r setup, echo=FALSE, warning=FALSE, message=FALSE}
library(knitr)
library(kfigr)
library(rmarkdown)
opts_chunk$set(message=FALSE,warning=FALSE,dev="png")
opts_knit$set(eval.after='fig.cap')

Then when I make my figures, I create the chunk like this:


```{r chunk1, anchor="figure", fig.cap=paste0("**Figure ** ",figr('chunk1'),": Dummy plot.")}
plot(1)

Any idea why it would fail during check (with that error) and not when just using the 'Knit' RStudio button? I realize there could be a lot of reasons un-related to your package, but that error I think is unique to kfigr.

mkoohafkan commented 9 years ago

I am pretty sure this has to do with the order in which chunk options are being evaluated---kfigr is throwing the error because the fig.cap option is being evaluated before the anchor option, so the reference has not yet been defined when the figure caption is evaluated. I think you currently have two options:

  1. instead do fig.cap=paste0("**Figure ** ",figr('chunk1', type = 'figure'),": Dummy plot.") so that kfigr defines the reference type in the fig.cap call. A bit redundant, but it should work no matter what order the chunk options are evaluated.
  2. Try setting the eval.after knitr option so that fig.cap is evaluated at the end of the chunk, i.e. opts_knit$set(eval.after = 'fig.cap'). This should guarantee that the anchor option is evaluated before the fig.cap option.

If this does not solve the issue, let me know and I will dig into it further.

One possible reason why this only occurs with R CMD check is that R CMD check tangles a document and evaluates the code when checking a package, so perhaps there is something happening there where some chunk options are evaluated but others are not, or that they are evaluated in a different order (I warn about possible issues with tangle at the end of the kfigr vignette, but my test cases didn't catch the error you are experiencing). Another possibility is the that knit button in RStudio (which I don't use) has different default options, e.g. perhaps it automatically sets opts_knit$set(eval.after = 'fig.cap') while R CMD check does not.

ldecicco-USGS commented 9 years ago

(Maybe view source of this comment...I'm having trouble trying to just paste in verbatim text)

I think I was doing #2 originally (? unless I'm missing something).

I added the type and didn't seem to fix the problem. Here's a really simple Rmd that won't pass the R Check (or, an easier test is devtools::build_vignettes():

title: "Test" author: "me" date: "r format(Sys.time(), '%d %B, %Y')" output: rmarkdown::html_vignette: fig_height: 6 fig_width: 6 toc: yes fig_caption: yes vignette: > %\VignetteEngine{knitr::rmarkdown} %\VignetteIndexEntry{test}

\usepackage[utf8]{inputenc}

library(knitr)
library(kfigr)
library(rmarkdown)
opts_chunk$set(message=FALSE,warning=FALSE,dev="png")
opts_knit$set(eval.after='fig.cap')
set.seed(25)
x <- rnorm(100)
plot(x)

#########################################

If I remove the fig.cap, then it will pass the checks.

mkoohafkan commented 9 years ago

Hm, I am not able to reproduce the error. I added the vignette you specified here to my test package, ballin-octo-batman, which I use to test package building with kfigr. The figure caption and vignette build fine for me when using devtools::build_vignettes and devtools::check does not produce any errors.

I am realizing that kfigr produces some strange behaviors when interacting with pandoc, which is used by the knitr::rmarkdown engine. My test cases did not cover pandoc and it seems like they should---almost all the issues posted to kfigr are related to pandoc. I will be doing some more rigorous testing soon but in the meantime you may want to check out the captioner package, which seems to already play nice with pandoc (although the captioner vignette suggests avoiding use of the fig.cap option).

For completeness, my output of sessionInfo() is below.

R version 3.2.1 (2015-06-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages:
[1] rmarkdown_0.7 kfigr_1.2     knitr_1.10.5 

loaded via a namespace (and not attached):
 [1] Rcpp_0.11.6     digest_0.6.8    git2r_0.10.1    formatR_1.2    
 [5] magrittr_1.5    evaluate_0.7    stringi_0.5-5   curl_0.9       
 [9] xml2_0.1.1      devtools_1.8.0  tools_3.2.1     stringr_1.0.0  
[13] yaml_2.1.13     rversions_1.0.1 memoise_0.2.1   htmltools_0.2.6