Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

specifying output path for knit2html

I'm having trouble specifying an output path for the html generated by knit2html or its dependent functions. I would like to specify 'outfile' in the the call to knit2html(), but I get the error,

Error in knit2html(input = "test.Rmd", output = "test-abcd.html") :
object 'outfile' not found

'output' is a parameter of markdownToHTML which should work I'd think. I can't find anywhere in the source where 'outfile' is used.

This should reproduce my experience.

library(knitr)
library(markdown)

# a minimal example
writeLines(c("```{r hello-random, echo=TRUE}", "rnorm(5)", "```"), 
           "test.Rmd")

# this works and outputs to test.html
knit2html(input = "test.Rmd")

# this generates the above error
knit2html(input   = "test.Rmd", 
          output  = "test-abcd.html")

# breaking it down into two steps works in this simple case,
# but not in my application.  trying to diagnose that difference currently
knit("test.Rmd")    
markdownToHTML("test.md", 
               output="test-abcd.html")

relevant version info might be useful?

sessionInfo()
R version 3.0.0 (2013-04-03)
Platform: x86_64-pc-linux-gnu (64-bit)

other attached packages:
[1] plyr_1.8         knitr_1.2        digest_0.6.3     markdown_0.5.4   xtable_1.7-1     reshape2_1.2.2   scales_0.2.3     ggplot2_0.9.3.1  data.table_1.8.8
like image 795
Sam Swift Avatar asked Apr 29 '13 20:04

Sam Swift


1 Answers

First, thanks for the very clear and reproducible question. If you take a look at the knit2html function source code, you can understand what the problem is :

R> knit2html
function (input, ..., envir = parent.frame(), text = NULL, quiet = FALSE, 
    encoding = getOption("encoding")) 
{
    if (is.null(text)) {
        out = knit(input, envir = envir, encoding = encoding, 
            quiet = quiet)
        markdown::markdownToHTML(out, outfile <- sub_ext(out, 
            "html"), ...)
        invisible(outfile)
    }
    else {
        out = knit(text = text, envir = envir, encoding = encoding, 
            quiet = quiet)
        markdown::markdownToHTML(text = out, ...)
    }
}
<environment: namespace:knitr>

If the text argument is NULL (ie, if you provide a file as input instead of a character vector), then the given file is passed to the knit function, and the markdownToHTML function is called the following way :

markdown::markdownToHTML(out, outfile <- sub_ext(out, "html"), ...)

So in this case the output file name is generated by substituting the existing file name extension with html, and you can't provide your own output filename as an argument.

like image 156
juba Avatar answered Oct 04 '22 18:10

juba