如何将 'learnr' 中的教程问题嵌入到完整闪亮的应用程序中?

How do I embed tutorial questions from 'learnr' into a full shiny app?

我正在尝试将 learnr 包中的教程 Rmd 嵌入到一个完整的闪亮应用程序中。但是,学习者使用 shiny_prerendered 运行 时间,我无法在我的应用程序中调用它。如何在我闪亮的应用程序中获得 运行 的交互式教程?

我现在有三个文件:ui.R、server.R 和 tutorial.Rmd。

我的教程是这样的(为了格式化删除了一个`)

---
title: "my tutorial"
tutorial:
  id: "com.example.tutorials.a-tutorial"
  version: 1.0
output: learnr::tutorial
runtime: shiny_prerendered
---

``{r setup, include=FALSE}
library(learnr)
knitr::opts_chunk$set(echo = FALSE)
``

### Exercise Example
An R code question
``{r add-function, exercise=TRUE, exercise.lines = 5}
add <- function() {

}
``

### Quiz
R Quiz Question
``{r quiz}
quiz(
  question("Question 1",
    answer("wrong"),
    answer("also wrong"),
    answer("right", correct = TRUE),
    answer("wrong again")
  )
)
``

当我尝试从 ui.R 渲染此文件的输出时,如下所示:

ui <- tagList(
    fluidPage(theme = shinytheme("cosmo")),
    navbarPage(
       "appTitle",
       tabPanel("Embedding Tutorials?", 
          includeMarkdown("tutorial.Rmd")
       ),
    )
)

它(我相信是正确的)将其显示为常规的旧 Rmd 文件,而不是交互式教程。

我也尝试过使用 rmarkdown::render("tutorial.Rmd"),它只是将文件路径呈现给 Rmd (/Users/me/app/tutorial.html) 生成的 html 文件。

当我尝试使用 run_tutorial("hello", package="learnr") 渲染任何教程时,它(再次正确地)给出了错误 ERROR: Can't call运行App()from within运行App(). If your application code contains运行App(), please remove it.

我已经发现我可以使用 learnr 中的 question() 函数创建问题块,方法如下:

ui <- tagList(
    fluidPage(theme = shinytheme("cosmo")),
    navbarPage(
       "appTitle",
       tabPanel("Tutorial", 
             quiz(
               question("Quiz question",
                        answer("1"),
                        answer("2"),
                        answer("3", correct = TRUE),
                        answer("4"),
                        allow_retry = TRUE
               )
       ),
    )
)

但这不允许在应用程序中创建可以 运行 的 R 代码块的功能。

我想要的是一个完全交互式的学习者教程,可以从 ui.R 文件中为闪亮的应用程序呈现。这可能吗?

除了我建议将您的额外 material 合并到 learnr 教程中,我还让 <iframe> 嵌入工作。使用以下内容创建 app.R

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("learnr tutorial"),

    # Show a plot of the generated distribution
    mainPanel(fluidRow(
       htmlOutput("frame")
    ))
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    output$frame <- renderUI({
        tags$iframe(
            src="https://jjallaire.shinyapps.io/learnr-tutorial-03a-data-manip-filter/", width=1280, height=720
        )
    })
}

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

现在当你 Run App 这应该嵌入来自 https://rstudio.github.io/learnr/

的示例教程

似乎有必要将教程渲染并发布到 shinyapps.io,等等:我无法仅从渲染的 html 文件中让它工作。所以,

  1. 创建教程
  2. 发布教程
  3. 嵌入教程

似乎是前进的方向。

一般来说,有两种方法可以在闪亮的应用程序中嵌入交互式 RMarkdown 文档。

(1) 通常的方法(由@Phil 提出)是让一个 R 服务器 运行 嵌入嵌入式教程,另一个 运行 应用程序。这可以通过先通过 shinyapps.io 或 shiny-server 部署教程然后使用 iframe 来存档。或者,您可以在本地后台进程中使用 callr::r_bg() 到 运行 教程。无论如何,这将使 Rmd 文档无法与闪亮的应用程序交互。如果这对您的用例可行,我建议您选择此选项。

(2) 罗列了一个比较绕的方法here by the maintainer of the shinyAce package. It uses the same R server for the main application and the embedded Rmd document. See also 。 AFAIK,这仅适用于 knitr::knit2html ,它依赖于 RMarkdown 的过时版本。此外,以这种方式可用的 render*output* 函数的数量是有限的,除非您确保某些 JavaScript 和 CSS 资源正确包含在您的 ui 中定义。

Quite 自从我开始思考这个话题以来已经过去了一段时间,但我当时的印象是,(2) 需要 quite 做很多工作而且真的限制你的选择。