[英]Way of executing a selected block of markdown in rstudio (with knitr)
Is there a way to test-out and peek at the output of a selected portion of markdown in RStudio?有没有办法在 RStudio 中测试和查看 Markdown 选定部分的输出? It seems you either run R code or have to compile the entire RMD page in order to see the output.看来您要么运行 R 代码,要么必须编译整个 RMD 页面才能查看输出。
This is a Windows-only solution and it uses the clipboard instead of the current selection:这是一个仅限 Windows 的解决方案,它使用剪贴板而不是当前选择:
Define the following function:定义以下函数:
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)
}
Then, copy the markdown you want to preview and run preview()
.然后,复制要预览的 Markdown 并运行preview()
。 Note that the output might be different from the output in the final document because请注意,输出可能与最终文档中的输出不同,因为
A solution without using the clipboard will most likely employ rstudioapi::getActiveDocumentContext()
.不使用剪贴板的解决方案很可能会使用rstudioapi::getActiveDocumentContext()
。 It boils down to something along the lines of a modified preview
function它归结为类似于修改后的preview
功能的东西
preview2 <- function() {
code <- rstudioapi::getActiveDocumentContext()$selection
# drop first line
# compile document (as in preview())
# stop execution (THIS is the problem)
}
which could be used by running preview()
followed by the markdown to render:可以通过运行preview()
后跟 markdown 来渲染:
preview2()
The value of pi is `r pi`.
The problem is, I don't see how the execution could be halted after calling preview2()
to prevent R from trying to parse The value of …
.问题是,我没有看到在调用preview2()
以防止 R 尝试解析The value of …
后如何停止执行。 See this related discussion .请参阅此相关讨论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.