如何在闪亮的应用程序中使用knitr:html2html之类的rmarkdown :: render?

打字员

我有一个可以正常使用的应用程序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的缺点

在此处输入图片说明

安娜·尼维森(Anna Nevison)

这是使用晶须模板的解决方案。

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)您先前分别致电HTMLknit2html

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在.Net Core 2.0 MVC应用程序中使用@ Scripts.Render?

在受限环境中使用`rmarkdown :: render`

Flask Render模板重复HTML

Knitr2pdf不适用于.Rmd文件,请使用rmarkdown :: render()

如何使用rmarkdown :: render()在html中渲染gganimate图,而不会产生不必要的输出

Rails,render_async gem:如何在通过ajax加载的部分中使用render_async

如何从 rmarkdown::render 函数调用 rmdformat 主题?

如何在React Render中使用Calc CSS函数

如何在React render函数中使用ejs变量?

如何在 React 组件的 render() 中使用回调

使用rmarkdown :: render时变量未知

如何在闪亮的应用程序中呈现HTML图

ReactDOM.render但使用类

SharePoint 框架应用程序中的 ReactDom.Render()

如何在 html iframe 中使用 heroku 应用程序?

在Rails api应用程序中使用“ ApplicationController.new.render_to_string”(config.api_only = true)

使用Render Angular后如何插入HTML元素?

在 React render() 中使用类属性

在Contentelement中使用v:content.render

在react组件的render函数中使用变量

避免在render方法中使用If语句

在render prop函数中使用react钩子

如何在ionic中使用render2设置子标题的样式?

ReactDom.render 不呈现 html 内容

MVC,包含AngularJS的Render HTML页面

来自JSON对象的React Render HTML对象

如何在render方法中渲染组件?

如何在render函数中调用函数?

如何在angular2应用程序中使用html代码