【发布时间】:2020-05-19 18:49:10
【问题描述】:
我有两个 rmarkdown 文件。 Rmd #1:
---
title: "RMD1"
output: html_document
runtime: shiny
---
<style type="text/css">
h1.title {
text-align: center;
color: DarkBlue;
font-size: 38px;
}
</style>
```{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")
)
```
Rmd #2:
---
title: "RMD2"
output: html_document
runtime: shiny
---
<style type="text/css">
h1.title {
text-align: center;
color: DarkBlue;
font-size: 38px;
}
</style>
我将 RMD1 和 RMD2 绑定到下面闪亮应用代码中的两个操作按钮。我的问题:如何在页面加载时自动呈现 RMD1(就像自动点击操作按钮一样)?
额外问题:当我第一次运行应用程序时,我需要单击一个操作按钮两次来呈现 rmarkdown 文件。我通过在下面添加一行来解决这个问题,该行未连接到任何操作按钮并输出 rmarkdown 文件。不知道为什么会这样,或者是否有更合乎逻辑/更优雅的解决方案。
library(shiny)
library(shinyWidgets)
rmd_list <- list("rmd1.Rmd", "rmd2.Rmd")
ui <- shinyUI(fluidPage(
shinyWidgets::panel(
fluidRow(
column(12, align="center",
actionButton("rmd1", "RMD1"),
actionButton("rmd2", "RMD2")
)
)),
,uiOutput("uioutput")
))
# Define server logic ----
server <- function(input, output, session) {
#So that I dont need to double click on an action button to render any rmarkdown. No idea why this happens, but this is my solution to the "double click" problem.
output$uioutput <- renderUI({
withMathJax(includeHTML(rmarkdown::render(rmd_list[[1]])))
observeEvent(input$rmd1, {
output$uioutput <- renderUI({
withMathJax(includeHTML(rmarkdown::render(rmd_list[[1]])))
})
})
observeEvent(input$rmd2, {
output$uioutput <- renderUI({
withMathJax(includeHTML(rmarkdown::render(rmd_list[[2]])))
})
})
}
# Run the app ----
shinyApp(ui = ui, server = server)
【问题讨论】:
标签: r shiny r-markdown