2013-04-29 35 views
6

我無法爲由knit2html或其依賴函數生成的html指定輸出路徑。我想在調用指定「OUTFILE」到knit2html(),但我得到的錯誤,指定knit2html的輸出路徑

錯誤knit2html(輸入=「test.Rmd」,輸出=「測試abcd.html」 ):
對象OUTFILE'未找到

「輸出」是markdownToHTML的一個參數,它應該工作,我會想。我無法在使用'outfile'的源代碼中找到任何地方。

這應該重現我的經驗。

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") 

相關版本信息可能很有幫助?

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 

回答

3

首先,感謝您的非常清楚和可重複的問題。如果你看一看的knit2html功能的源代碼,你可以瞭解是什麼問題:

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> 

如果text說法是NULL(即,如果您提供的文件作爲輸入,而不是一個特徵向量),然後給定的文件傳遞給knit功能,並且markdownToHTML函數調用方式如下:

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

因此,在這種情況下,通過替換現有的文件擴展名與html生成的輸出文件名,你不能p將自己的輸出文件名作爲參數。

+0

Thanks @juba,我想這會讓這個休息。我認爲我甚至已經看過這個功能,但是一定是錯過了解決問題模糊中的重要部分。我想理想情況下,它會檢查'輸出',如果沒有給出,只使用默認名稱。 – 2013-04-30 14:41:17

+0

@SamSwift對於麻煩抱歉;它是在開發版本中已修復的knitr錯誤:https://github.com/yihui/knitr – 2013-05-15 13:21:15

+1

注意:要閱讀'sub_ext'的源代碼,您需要使用_three_打印:''s:' knitr ::: sub_ext'。 – isomorphismes 2014-12-23 22:13:36