2016-04-30 58 views
1

有沒有辦法在RStudio中測試和查看Markdown的選定部分的輸出?看來你要麼運行R代碼,要麼必須編譯整個RMD頁面才能看到輸出。在rstudio中執行選定Markdown塊的方式(使用knitr)

+0

爲什麼不給不需要的塊添加'eval = FALSE'並且只用想要的(「selected」)塊來編譯整個Rmd? – PoGibas

+0

問題是關於減號塊,它存在於塊之外。例如,'有'(my_data)\'個人。'你如何選擇類似的東西,並將其編譯爲rmarkdown – Cenoc

回答

1

這是一個Windows的唯一解決方案,它使用剪貼板,而不是目前的選擇:

定義以下功能:

preview <- function() { 
    output <- tempfile(fileext = ".html") 
    input <- tempfile(fileext = ".Rmd") 
    writeLines(text = readClipboard(), con = input) 
    rmarkdown::render(input = input, output_file = output) 
    rstudioapi::viewer(output) 
} 

然後,複製要預覽和運行preview()降價。請注意,輸出可能成爲輸出不同,因爲

  • 代碼在當前環境
  • 只複製降價評估被評估的最後文件中,這意味着該片段沒有上下文任何責任。

,而不使用剪貼板將最有可能採用rstudioapi::getActiveDocumentContext()一個解決方案。它歸結爲沿這可以通過運行preview()其次是降價用來渲染修改preview功能

preview2 <- function() { 
    code <- rstudioapi::getActiveDocumentContext()$selection 
    # drop first line 
    # compile document (as in preview()) 
    # stop execution (THIS is the problem) 
} 

線的東西:

preview2() 
The value of pi is `r pi`. 

的問題是,我沒有看到在調用preview2()以防止R試圖解析The value of …後,如何停止執行。請參閱this related discussion

相關問題