我有一个可以正常使用的应用程序knitr::knit2html
(除了一些小故障,单击该代码后,稍后会执行代码)。
我想使用rmarkdown::render
功能而不是knitr::knit2html
码
library(shinyAce)
library(shinyjs)
library(shiny)
codeUI <- function(id) {
ns <- NS(id)
tagList(htmlOutput(ns("output")))
}
codeSE <- function(id, active_id, code, env) {
moduleServer(id,
function(input, output, session) {
output$output <- renderUI({
req(id == active_id(), cancelOutput = TRUE)
eval_code <- paste0("\n```{r echo = TRUE, comment = NA}\n", code, "\n```\n")
HTML(knitr::knit2html(text = eval_code, fragment.only = TRUE, quiet = TRUE, envir = env))
})
})
}
ui <- fluidPage(
useShinyjs(),
tags$style(type = "text/css", "
.foot{
position:fixed;
bottom:0;
right:0;
left:0;
/* background:#00adfc; */
padding:10px;
box-sizing:border-box;
}
"),
div(id = "add_here"),
div(id = "end", " "),
div(style = "height: 80vh;"),
div(class = "foot",
aceEditor("code", mode = "r", height = "50px",
highlightActiveLine = FALSE,
fontSize = 16,
showLineNumbers = FALSE),
actionButton("eval", "Run"))
)
env <- environment()
server <- function(input, output, session) {
counter <- 1
active_id <- reactiveVal()
observeEvent(input$eval, {
req(code)
current_id <- paste0("out_", counter)
active_id(current_id)
codeSE(id = current_id, active_id = active_id, code = input$code, env = env)
insertUI(selector = "#add_here",ui = codeUI(current_id))
counter <<- counter + 1
runjs('
document.getElementById("end").scrollIntoView();
')
}) }
shinyApp(ui, server)
我想rmarkdown::render
用来克服无样式knitr::kable
表的缺点。
这是使用晶须模板的解决方案。
在 globals.R
output_rmd <- function(code_chunk) {
render_dir <- fs::path_temp(round(runif(1, 100000, 1000000), 0))
rmd_path <- file.path(render_dir, "input.Rmd")
final_path <- file.path(render_dir, "body_snippet.html")
fs::dir_create(render_dir, recurse = TRUE)
# read in template for rmarkdown
whisker_template <- readr::read_lines("input.template")
# render template with input code chunk
rendered_temp <- whisker::whisker.render(whisker_template,
data = list(code_chunk = code_chunk))
# save out rendered template as .Rmd to temp dir
readr::write_lines(rendered_temp, path = rmd_path)
# render the temp .Rmd file as html
out_path <- rmarkdown::render(rmd_path)
# read in the html, select the body portion only, save that out to temp
xml2::write_html(rvest::html_node(xml2::read_html(out_path), "body"), file = final_path)
# read in the html body portion
lines <- readr::read_lines(final_path)
# add table table-condensed class to all tables so they render in snippet like they would in full html
lines <- gsub("<table>", '<table class="table table-condensed">', lines, fixed = TRUE)
# save out the final html snippet
readr::write_lines(lines, final_path)
return(final_path)
}
该函数读入input.template
,将要运行的代码附加到模板,将完成的.Rmd文件保存到temp目录,rmarkdown::render
在该temp目录中使用它进行渲染,然后将文件路径返回到最终的html渲染输出。
input.template
---
title: "Shiny Run Code"
output: html_document
---
```{r echo = TRUE, comment = NA}
{{{ code_chunk }}}
```
然后,在app.R
你只需要调用rmd_file <- output_rmd(code)
和includeHTML(rmd_file)
您先前分别致电HTML
和knit2html
library(shinyAce)
library(shinyjs)
library(shiny)
source('globals.R') #changed typo
codeUI <- function(id) {
ns <- NS(id)
tagList(htmlOutput(ns("output")))
}
codeSE <- function(id, active_id, code, env) {
moduleServer(id,
function(input, output, session) {
output$output <- renderUI({
req(id == active_id(), cancelOutput = TRUE)
rmd_file <- output_rmd(code)
includeHTML(rmd_file)
})
})
}
ui <- fluidPage(
useShinyjs(),
tags$style(type = "text/css", "
.foot{
position:fixed;
bottom:0;
right:0;
left:0;
/* background:#00adfc; */
padding:10px;
box-sizing:border-box;
}
"),
div(id = "add_here"),
div(id = "end", " "),
div(style = "height: 80vh;"),
div(class = "foot",
aceEditor("code", mode = "r", height = "50px",
highlightActiveLine = FALSE,
fontSize = 16,
showLineNumbers = FALSE),
actionButton("eval", "Run"))
)
env <- environment()
server <- function(input, output, session) {
observeEvent(input$code, {
if(input$code == ''){
shinyjs::disable("eval")
} else {
shinyjs::enable("eval")
}
})
counter <- 1
active_id <- reactiveVal()
observeEvent(input$eval, {
req(code)
current_id <- paste0("out_", counter)
active_id(current_id)
codeSE(id = current_id, active_id = active_id, code = input$code, env = env)
insertUI(selector = "#add_here",ui = codeUI(current_id))
counter <<- counter + 1
runjs('
document.getElementById("end").scrollIntoView();
')
}) }
shinyApp(ui, server)
最后,我shinyjs::disable/enable
在观察器中添加了,以解决您在点击时遇到的错误问题。
您的文件结构应如下所示:
- myapp
- app.R
- globals.R
- input.template
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句