Skip to content

CHANGES IN knitr VERSION 1.7

Compare
Choose a tag to compare
@yihui yihui released this 13 Oct 23:17
· 2771 commits to master since this release

NEW FEATURES

  • added a simple Fortran engine after sitting with John Nash for a few minutes at UseR!2014; now we can use the chunk option engine = 'fortran' to include Fortran code in a source document, which will be compiled and loaded via R CMD SHILIB and dyn.load(), respectively
  • added vignette engines with the suffix _notangle, which have the same weave functions as those engines without this suffix but have disabled the tangle function, meaning there will not be R scripts generated from the vignettes during R CMD build or R CMD check (thanks, Carl Boettiger and Michael Koohafkan, #784)
  • added an argument col.names to kable(), so we can specify different column names (thanks, @jackflibb, #801)
  • added a new output hook called text, and its default value knit_hooks$get('text') is the identity function function(x) x; this hook is applied to the text chunks (recall knit_hooks$get('chunk') is applied to code chunks)
  • added a chunk option fig.showtext to support the showtext package; if fig.showtext = TRUE (which is what you should do if you use the showtext package), showtext::showtext.begin() is called before drawing plots (thanks, @yufree, #799)
  • added a new language engine node for Node.js (thanks, Jake Burkhead, #823)
  • added a package option global.par; if we set opts_knit$set(global.par = TRUE) (by default it is FALSE), the par() settings from the last code chunk will be preserved and applied to the next code chunk (thanks, Jim Winget)
  • language engines also write error messages (if there are any) in the output now (thanks, Fabian Hirschmann, #789)
  • added the envir argument to knit_child() so that users can specify a different environment to evaluate the child documents (thanks, Stéphane Laurent, http://stackoverflow.com/q/24009622/559676)
  • for set_parent(), the lines in the parent document that start with \bibliography are matched and inserted in the child document so LaTeX bibliography also works for the child document (thanks, Mark Heckmann, #819)
  • for the chunk option engine = 'cat', the code chunk can be displayed in the output if the chunk language is specified via engine.opts, e.g. engine.opts = list(lang = 'makefile')

BUG FIXES

  • fixed #779: when the chunk options tidy=FALSE and eval=FALSE, prompt=TRUE did not work for R expressions of multiple lines (thanks, Qijie Zhao)
  • fixed #788: there was no increment in the chunk counter when the code chunks were read through read_chunk(), which may lead to clashes of chunk labels (thanks, Jason Ackman)
  • fixed #790: when chunk A reuses code from chunk B via <<B>>, and only the first line of B is empty, chunk reuse can fail because A sees B as empty (thanks, @kingaa)
  • fixed #791: if one has specified the chunk option dev.args, only pointsize and bg in it can be passed to the default recording device (the pdf() device) (thanks, @M-Russell and @joelgombin)
  • fixed #822: cache.lazy = FALSE did not really work (thanks, Liz Ing-Simmons)
  • fixed rstudio/rmarkdown#205: when R marks the encoding of the input document as latin1, knit() can fail to convert its encoding (thanks, @ripkrizbi)
  • fixed #828: scientific notation for inline numbers did not work in R Markdown v2 when the output format is LaTeX (thanks, @nacnudus)
  • fixed #833: for the LaTeX output format, when fig.cap contains . in {}, the automatic short caption does not work (thanks, Roman Luštrik)
  • fixed #844: when the digits argument is a vector and x is a numeric matrix, kable(x) did not work (thanks, @dmenne, #844)

MAJOR CHANGES

  • the knit() function no longer modifies R's default options(digits) from 7 to 4, since it may lead to confusion especially when printing summary() output; for those who want the old behavior, you must set options(digits = 4) in the beginning of your document (thanks, John Honaker, #777)

  • the figure file numbering scheme has changed: for a chunk with a label foo, its figure files are named as foo-i where i ranges from 1 to n (the total number of plots in this chunk); previously, the figure file was named as foo instead of foo-1 when there was only one plot generated in this chunk, which has a potential bug: consider two chunks named foo and foo2, respectively; foo generates two figures foo1.png and foo2.png, and foo2 generates one figure foo2.png, which will overwrite the second figure generated from the chunk foo (thanks, @kevinushey, @kohske, @kforner, #704, #832)

    You may use the function below to clean up the redundant figure files:

    #' Clean up figure files generated before knitr 1.7
    #' 
    #' This function finds figure files that may be redundant, e.g., knitr <= 1.6 
    #' generates foo.pdf for the chunk foo, and knitr >= 1.7 generates foo-1.pdf. If
    #' both foo.pdf and foo-1.pdf exist, foo.pdf might be redundant.
    #' @param dir the figure directory
    #' @param clean whether to remove the redundant figure files; make sure you take
    #'   a look at the list if files detected before you clean them up
    clean_figures = function(dir = './figure', clean = FALSE) {
      # figure files that do not have a numeric suffix
      old1 = list.files(dir, '[^0-9][.][a-z]{3,4}$', full.names = TRUE)
      # or do not have - before suffix
      old2 = list.files(dir, '[^-][0-9]+[.][a-z]{3,4}$', full.names = TRUE)
      new1 = gsub('(.)([.][a-z]{3,4})$', '\\1-1\\2', old1)
      new2 = gsub('([^-])([0-9]+)([.][a-z]{3,4})$', '\\1-\\2\\3', old2)
      new = c(new1, new2)
      old = c(old1, old2)
      idx = file.exists(new)
      message('Possibly redundant files:')
      cat(paste('rm', paste(old, collapse = ' \\\n')), '\n')
      if (!any(idx)) return()
      if (clean) file.remove(old[idx]) else {
        message('Perhaps you should remove these files:')
        cat(old[idx], sep = '\n')
      }
    }
  • for warnings and errors from code chunks, the call that produced them will be printed as part of the message, e.g. previously an error might just be Error: x must be positive, and now it may be Error in FUN(x = -1): x must be positive (thanks, @jennybc, #846)

  • for the engine coffee (CoffeeScript), the flag -p has been removed from the command line arguments, which means the default behavior of this engine is to evaluate the code, instead of printing JavaScript; if you want the old behavior, you need the chunk option engine.opts = '-p' (thanks, Jake Burkhead, #821)

  • when the chunk option results = 'hold', the text output blocks will be collapsed into a single block (thanks, Gavin Simpson, #798)

  • the video format for animations (when the chunk option fig.show='animate') was changed from OGG to WebM (http://www.webmproject.org), which has many benefits over other formats, especially for the web (thanks, @gaorongchao, #641)

  • the YAML metadata in Markdown child documents will be ignored (only the metadata in the top parent document is preserved)

MINOR CHANGES

  • scientific formatting for inline R output is only applied to objects of which the first class is numeric, e.g. chron::chron() objects will no longer be formatted using scientific notations (thanks, @sanfordweisberg, #806)
  • for R Markdown v2 documents, if the inline R output is formatted using the scientific notation, the output must be put in a math environment, e.g. $r 2e10$