gpt4 book ai didi

r - 是否可以在 Shiny 的应用程序中使用操作按钮呈现 "runtime:shiny"rmarkdown (.rmd) 文件?

转载 作者:行者123 更新时间:2023-12-04 14:16:18 25 4
gpt4 key购买 nike

假设我们有这个在 RStudio 上构建的“runtime: shiny”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 文件,然后在构建我 Shiny 的应用程序时引用它:


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)

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

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

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

最佳答案

您的代码需要一些调整(如果您不介意的话)。
最重要的是,您需要使用 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")
)
```

现在,要在 shiny 应用程序中呈现 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 输出。
此外,可能还有其他解决方案...

关于r - 是否可以在 Shiny 的应用程序中使用操作按钮呈现 "runtime:shiny"rmarkdown (.rmd) 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59959348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com