我试图从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
runApp()from within
runApp(). If your application code contains
runApp(), please remove it.
我已经发现我可以使用question()
中的learnr
函数创建问题块,使用以下内容:
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
文件中呈现一个闪亮的应用程序。这可能吗?
除了我的建议将你的额外材料纳入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
文件中使用它。所以,
似乎是前进的方向。
一般来说,有两种方法可以在闪亮的应用程序中嵌入交互式RMarkdown文档。
(1)通常的方式(由@Phil提出)是让一个R服务器运行嵌入式教程,另一个运行应用程序。这可以通过首先通过shinyapps.io或shiny-server部署教程然后使用iframe来存档。或者,您可以使用callr::r_bg()
在本地后台进程中运行教程。无论如何,这将使Rmd文档无法与闪亮的应用程序交互。如果这对您的用例可行,我建议使用此选项。
(2)here包的维护者概述shinyAce
更复杂的方式。它为主应用程序和嵌入式Rmd文档使用相同的R服务器。另见this SO question。 AFAIK,这只适用于knitr::knit2html
,它依赖于过时版本的RMarkdown。此外,以这种方式可用的render*
和output*
函数的数量是有限的,除非您确保在您的ui定义中正确包含某些JavaScript和CSS资源。
很长一段时间以来,我一直围绕这个主题,但我当时的印象是,(2)需要做很多工作,并且真的限制了你的选择。