首页 > 解决方案 > 在 R 中返回 renderUi 的完整 HTML 内容

问题描述

是否可以获取页面的完整 HTML 文本,该页面生成为闪亮的 renderUi 对象?我需要内容来使用 ajax 生成屏幕截图。

示例 1 按预期工作:页面直接在 ui 中生成。生成的 ui 对象包含完整的 HTML 文本。它可以保存为 .html 文件并在浏览器中打开以显示页面。

ui <- fluidPage(
  div(numericInput("invalue", label = "Input", 10)),
  h4(div("Output")),
  textOutput("outvalue")
)

server <- function(input, output, session) {
output$outvalue <- renderText(input$invalue * 10)
}

shinyApp(ui, server)

界面内容:

<div class="container-fluid">
  <div>
    <div class="form-group shiny-input-container">
      <label for="invalue">Input</label>
      <input id="invalue" type="number" class="form-control" value="10"/>
    </div>
  </div>
  <h4>
    <div>Output</div>
  </h4>
  <div id="outvalue" class="shiny-text-output"></div>
</div>

示例 2 无法正常工作:页面在服务器端使用 renderUI 生成并发送到 ui 函数,在该函数中通过 uiOutput 处理。该页面看起来与示例 1 中的完全相同,但生成的 ui 对象仅包含一行。如果将其保存为 .html 文件并在浏览器中打开,则不会显示任何内容。

ui <- uiOutput("page")

server <- function(input, output, session) {
  output$outvalue <- renderText(input$invalue * 10)
  output$page <- renderUI({
    fluidPage(
      div(numericInput("invalue", label = "Input", 10)),
      h4(div("Output")),
      textOutput("outvalue")
    )
  })
}

shinyApp(ui, server)

界面内容:

<div id="page" class="shiny-html-output"></div>

所以问题是:是否有可能从示例 2 中获取完整的 HTML 内容?我需要它来从页面生成一个 ajax 屏幕截图,该页面是使用 renderUi 动态生成的。

标签: htmlrshiny

解决方案


推荐阅读