简体   繁体   English

将 rmarkdown 渲染为字符变量

[英]Render rmarkdown to character variable

I am fairly new to R markdown.我对 R 降价相当陌生。 I have built an app that requires the user to provide multiple inputs to generate a table, which can then be saved locally.我构建了一个应用程序,它需要用户提供多个输入来生成一个表,然后可以将其保存在本地。 I have been now asked to implement a sort of report to list all the variables inserted by the user (in a sort of formatted document), so that before generating the table one can review all the settings and change them in case of errors.我现在被要求实现一种报告来列出用户插入的所有变量(在一种格式化的文档中),以便在生成表之前可以查看所有设置并在出现错误时更改它们。

To avoid major UI restructure, I thought about using ar markdown document and visualize it inside a modal.为了避免重大的 UI 重构,我考虑使用 ar markdown 文档并将其可视化在模态中。 My problem is that rmarkdown::render renders to an output, while bs_modal takes for the argument body a character (HTML) variable.我的问题是rmarkdown::render呈现到输出,而bs_modal将参数主体作为字符 (HTML) 变量。 Is there a way to make this work?有没有办法使这项工作? Or are there better way to accomplish this?或者有没有更好的方法来实现这一点?

A minimal example:一个最小的例子:

my .Rmd我的.Rmd

---
title: "Dynamic report"
output:
  html_document: default
  pdf_document: default
params:
  n : NA
---

A plot of `r params$n` random points.

```{r, echo=FALSE}
plot(rnorm(params$n), rnorm(params$n))
```

My App.R我的应用程序

library(shiny)
library(bsplus)
library(rmarkdown)

shinyApp(
  ui = fluidPage(
    selectInput(
      inputId = "numb",
      label = "Label with modal help",
      choices = 50:100
    ),
    actionButton(inputId = "mysheet",
                 label = "Open modal") %>%  bs_attach_modal(id_modal = "modal1"),
    textOutput("result")
  ),
  server = function(input, output) {
    observeEvent(input$mysheet, {
      params <- input$numb
      md_out <-
        rmarkdown::render(
          "report.Rmd",
          params = params,
          envir = new.env(parent = globalenv())
        )
      bs_modal(
        id = "modal1",
        title = "Equations",
        body = md_out,
        size = "medium"
      )
      
    })
    output$result <- renderText({
      paste("You chose:", input$numb)
    })
  }
)

bs_modal does not work like this, it must be in the UI. bs_modal不能这样工作,它必须在 UI 中。 Below is a solution using the classical Shiny modal, no bsplus or other package.下面是使用经典 Shiny 模式的解决方案,没有bsplus或其他包。

library(shiny)

shinyApp(
  ui = fluidPage(
    selectInput(
      inputId = "numb",
      label = "Label with modal help",
      choices = 50:100
    ),
    actionButton(inputId = "mysheet",
                 label = "Open modal"),
    textOutput("result")
  ),
  server = function(input, output) {
    observeEvent(input$mysheet, {
      params <- list(n = input$numb)
      md_out <-
        rmarkdown::render(
          "report.Rmd",
          params = params,
          envir = new.env(parent = globalenv())
        )
      showModal(modalDialog(
        includeHTML(md_out),
        title = "Equations",
        size = "m"
      ))
      
    })
    output$result <- renderText({
      paste("You chose:", input$numb)
    })
  }
)

Use html_fragment as the Rmd output:使用html_fragment作为 Rmd 输出:

---
title: "Dynamic report"
output:
  html_fragment
params:
  n : NA
---

A plot of `r params$n` random points.

```{r, echo=FALSE}
plot(rnorm(params$n), rnorm(params$n))
```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM