【问题标题】:Is it possible to render a "runtime:shiny" rmarkdown (.rmd) file, using an action button, in a shiny app?是否可以在闪亮的应用程序中使用操作按钮呈现“runtime:shiny”rmarkdown (.rmd) 文件?
【发布时间】:2020-05-14 11:51:23
【问题描述】:

假设我们有我在 RStudio 上构建的“运行时:闪亮”RMD 文件。它有 4 个主要部分:


    ---
    title: "Theory"
    output: html_document
    runtime: shiny
    ---

    Part 1: html code
    <style type="text/css">

    h1.title {

    text-align: center;
    color: DarkBlue;
    font-size: 38px;

    }

    p{

    font-size: 18pt;
    font-family: times, serif;
    }
    </style>


    <br>
    </br>

    ---

    Part2: Inline equations

    <p>
    This is an inline equation: $y = \frac{a}{b}$
    </p>

    <br>
    </br>

    ---

    Part3: Standalone equations

    $$y = \frac{a}{b} $$

    <br>
    </br>

    ---

    Part4: Embedded shiny inputs and outputs

    ```{r eruptions, echo=FALSE}
    inputPanel(
      selectInput("n_breaks", label = "Number of bins:",
                  choices = c(10, 20, 35, 50), selected = 20),

      sliderInput("bw_adjust", label = "Bandwidth adjustment:",
                  min = 0.2, max = 2, value = 1, step = 0.2)
    )

    renderPlot({
      hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
           xlab = "Duration (minutes)", main = "Geyser eruption duration")

      dens <- density(faithful$eruptions, adjust = input$bw_adjust)
      lines(dens, col = "blue")
    })

我可以在 rStudio 中“运行文档”,它运行良好。

现在我保存那个 .rmd 文件,然后在构建我的闪亮应用程序时引用它:


   library(shiny)
ui <- shinyUI(
  fluidPage(
    shinyWidgets::panel(
    fluidRow(
      column(12, align="center",
             actionButton("rmd", "Test")
      )
    ))
#    ,includeHTML(rmarkdown::render("test_rmd.Rmd"))
  )
)

server <- function(input, output) {
                  observeEvent(input$rmd, {
                    output$markdown <- renderUI({
                      includeHTML(rmarkdown::render("test_rmd.Rmd"))
                    })
                  })
}


shinyApp(ui, server) 

当我运行该应用程序时,它无法成功显示除文本本身之外的“运行时:闪亮”rmd 文件的所有 4 个部分。 我问的原因是因为我最终想要构建一个操作按钮,单击该按钮时将显示“运行时:闪亮”rmd 文件。

是否无法渲染“runtime: shiny”rmd 文件,因为它们与闪亮的应用程序格式不兼容?

附:我可以通过将 rmd 转换为 html IF 第 4 部分(“嵌入式闪亮输入和输出”)被删除(否则我得到一个错误)来解决这个问题。但如果可能的话,我想保留它。会让我的生活更轻松。

【问题讨论】:

    标签: r shiny r-markdown


    【解决方案1】:

    您的代码需要一些调整(如果您不介意的话)。
    最重要的是,您需要使用 shinyApp() 函数(第 4 部分)在 .Rmd 中嵌入 inline Shiny application(输入和输出):

    ---
    title: "Theory"
    output: html_document
    runtime: shiny
    ---
    
    Part 1: hidden css code
    
    ```{css part-1, echo = FALSE}
    .h1.title {
      text-align: center;
      color: DarkBlue;
      font-size: 38px;
      }
    
    .p{
      font-size: 18pt;
      font-family: times, serif;
      }
    ```
    
    <br>
    </br>
    
    ---
    
    Part2: Inline equations
    
    <p>
    This is an inline equation: $y = \frac{a}{b}$
    </p>
    
    <br>
    </br>
    
    ---
    
    Part3: Standalone equations
    
    $$y = \frac{a}{b} $$
    
    <br>
    </br>
    
    ---
    
    Part4: Embedded shiny inputs and outputs
    
    ```{r eruptions, echo=FALSE}
    shinyApp(
      ui = fluidPage(
        inputPanel(
          selectInput("n_breaks", label = "Number of bins:",
                      choices = c(10, 20, 35, 50), selected = 20),
    
          sliderInput("bw_adjust", label = "Bandwidth adjustment:",
                      min = 0.2, max = 2, value = 1, step = 0.2)
        ),
        plotOutput("eruptionsPlot")),
        
        server = function(input, output) {
          output$eruptionsPlot = renderPlot({
          hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
               xlab = "Duration (minutes)", main = "Geyser eruption duration")
    
          dens <- density(faithful$eruptions, adjust = input$bw_adjust)
          lines(dens, col = "blue")
        })
        },
        options = list(height = "600px")
    )
    ```
    

    现在,要在闪亮的应用程序中渲染 runtime: shiny(或任何其他 RMarkdown output: html_document),我会使用 includeHTML function

    library(shiny)
    ui <- shinyUI(
      fluidPage(
        includeHTML(rmarkdown::render("ShinyRMarkdownFile.Rmd"))
      )
    )
    server <- function(input, output) { }
    
    shinyApp(ui, server) 
    

    使用 includeMarkdown 函数,您只能渲染 Markdown 文件 .md,而使用 includeHTML 您可以加载渲染后的 RMarkdown 文件的 HTML 输出。
    另外,可能还有其他解决方案...

    【讨论】:

    • 呵呵,我试过你的代码,页面渲染失败。我只是尝试在普通的 rscript 中运行它。
    • 我正在使用最新版本的 R (v. 3.6.2) 和 RStudio (v.1.3.776-2 Preview);此外,rmarkdown、knitr 和闪亮包。这有帮助吗?
    【解决方案2】:

    好的,我想我明白了,感谢 Radovan Miletic。这是我用于 RMD ("test_rmd.Rmd") 文件的代码:

    ---
    title: "Theory"
    output: html_document
    runtime: shiny
    ---
    
    
    Part 1: hidden css code
    
    <style type="text/css">
    
    h1.title {
    
    text-align: center;
    color: DarkBlue;
    font-size: 38px;
    
    }
    
    p{
    
    font-size: 18pt;
    font-family: times, serif;
    }
    
    p1{
    
    font-size: 14pt;
    font-family: times, serif;
    }
    
    </style>
    
    
    <br>
    </br>
    
    ---
    
    Part2: Inline equations
    
    <p>
    This is an inline equation: $y = \frac{a}{b}$
    </p>
    
    <br>
    </br>
    
    ---
    
    Part3: Standalone equations
    
    $$y = \frac{a}{b} $$
    
    <br>
    </br>
    
    ---
    
    Part4: Embedded shiny inputs and outputs
    
    ```{r eruptions, echo=FALSE}
    shinyApp(
      ui = fluidPage(
        inputPanel(
          selectInput("n_breaks", label = "Number of bins:",
                      choices = c(10, 20, 35, 50), selected = 20),
    
          sliderInput("bw_adjust", label = "Bandwidth adjustment:",
                      min = 0.2, max = 2, value = 1, step = 0.2)
        ),
        plotOutput("eruptionsPlot")),
    
        server = function(input, output) {
          output$eruptionsPlot = renderPlot({
          hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
               xlab = "Duration (minutes)", main = "Geyser eruption duration")
    
          dens <- density(faithful$eruptions, adjust = input$bw_adjust)
          lines(dens, col = "blue")
        })
        },
        options = list(height = "600px")
    )
    ```
    

    这是我用于闪亮应用的代码:

    library(shiny)
    library(shinyWidgets)
    
    ui <- shinyUI(
      fluidPage(
        shinyWidgets::panel(
        fluidRow(
          column(12, align="center",
                 actionButton("rmd", "Test")
          )
        ))
    
    ,uiOutput("test")
      )
    )
    
    server <- function(input, output) {
                      observeEvent(input$rmd, {
                        output$test <- renderUI({
                          withMathJax(includeHTML(rmarkdown::render("test_rmd.Rmd")))
                        })
                      })
    }
    
    
    shinyApp(ui, server) 
    

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 2021-08-13
      • 2016-02-03
      • 2019-05-02
      • 2015-08-24
      • 2020-05-21
      • 2021-09-30
      • 2018-09-24
      • 2020-12-28
      相关资源
      最近更新 更多