Quarto文档中的交互式图表在R Shiny中使用iframe无法呈现,如何在R Shiny应用程序中呈现Quarto“.qmd”文件?

5

目标 我想在现有的R Shiny应用程序中呈现多个Quarto文件(.qmd)

当前解决方案 我的当前解决方案是将Quarto文件呈现为html,并在R Shiny应用程序中显示html文件,但这对于交互式图表不起作用。我知道我可以在Quarto文档中使用shiny,但我想在R Shiny中呈现带有其功能的quarto。

可重复示例代码

app.R

library(shiny)
library(quarto)

ui <- fluidPage(titlePanel("Reproducable Example"),
                
                sidebarLayout(
                  sidebarPanel(
                    textInput(inputId = "user_argument", label = "Argument"),
                    br(),
                    actionButton("render_button", "Render Quarto ")
                  ),
                  mainPanel(uiOutput("quarto_output"))
                ))

server <- function(input, output) {
  observeEvent(input$render_button, {
    quarto::quarto_render("example.qmd",
                          execute_params = list(user_arg = input$user_argument))
    html <-  readLines("qmd_output.html")
    # Display html in output
    output$quarto_output <- renderUI({
      tags$iframe(srcdoc = html,
                  height = "500px",
                  width = "100%")
    })
  })
}

# Run the application
shinyApp(ui = ui, server = server)

example.qmd

---
title: "Quarto HTML Basics"
format:
  html:
    code-tools: true
    self-contained: true
    output-file: "qmd_output.html"
    theme: cosmo
execute:
  warning: false
params:
  user_arg: NA
---



```{r}
#| label: fig-temperatures
#| fig-cap: "New Haven Temperatures"

library(dygraphs)
dygraph(nhtemp) %>% 
  dyRangeSelector(dateWindow = c("1920-01-01", "1960-01-01"))
```

## Argument

My name is `r params$user_arg`.

观察

  1. 当quarto渲染html时,交互式图表可以正常工作,但在R Shiny中呈现相同的html时无法正常工作。
  2. 我使用iframe是因为直接将html插入到R Shiny中也会覆盖插入的html的css。

帮助

  1. 是否有其他方法在R Shiny应用程序中呈现Quarto文件?
  2. 如果没有,是否有任何选项可以从html中呈现Shiny的交互式图表?

1
同样,如何在另一个R Shiny App中运行一个R Shiny App? - Pawan Rama Mali
1
同样,如何在另一个R Shiny App中运行一个R Shiny App? - Pawan Rama Mali
1
同样,如何在另一个R Shiny App 中运行一个R Shiny App? - undefined
1个回答

1

新答案

经过一点点调整,我成功让 iframe 正常工作了,并且 CSS 不会溢出。以下是我所做的步骤:
首先,在 app.R 所在的目录中创建一个名为 www 的文件夹。如果将网页资源放置在这个文件夹中,它们可以很容易地被使用。
接下来,将 example.qmd 移动到 www 文件夹中。最后,将 observeEvent 更改为以下内容:

observeEvent(input$render_button, {
    quarto::quarto_render("www/example.qmd", #note the www/
                          execute_params = list(user_arg = input$user_argument)
                          )
    html <-  "qmd_output.html" # WITHOUT www! Because www/ is implicit in src calls
    # Display html in output
    output$quarto_output <- renderUI({
      
      tags$iframe(src = html,
                  height = "500px",
                  width = "100%")
    })
  })

原始回复

这不是一个完美的答案,但可以让您的Quarto文档显示出来:

将observeEvent更改为以下内容:

observeEvent(input$render_button, {
    quarto::quarto_render("example.qmd",
                          execute_params = list(user_arg = input$user_argument))
    html <-  "qmd_output.html" #CHANGED you defined this name in example.qmd
    # Display html in output
    output$quarto_output <- renderUI({
      # I didn't get iframe to work, but using the next line will load the generated HTML
      includeHTML(html)
  })

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接