我正在尝试将learnr包中的教程Rmd嵌入到一个完整的闪亮应用程序中。然而,learnr使用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文件中。
当我尝试使用ERROR: Can't callrunApp()from withinrunApp(). If your application code containsrunApp(), please remove it.呈现任何教程时,它(同样,正确地)会给出错误的run_tutorial("hello", package="learnr")。
我已经发现,我可以使用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文件中呈现。这个是可能的吗?
发布于 2019-04-09 20:54:00
除了我的建议,把你的额外材料纳入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文件开始工作。所以,
似乎是前进的方向。
发布于 2019-04-09 22:53:49
一般来说,在闪亮的应用程序中嵌入交互式RMarkdown文档有两种方法。
(1)通常的方法是让一个R服务器运行嵌入式教程,另一个服务器运行应用程序。可以先通过shinyapps.io或闪亮服务器部署教程,然后使用iframe对其进行归档。或者,您可以使用callr::r_bg()在本地后台进程中运行本教程。无论如何,这将使Rmd文档不能与闪亮的应用程序交互。如果这对你的使用来说是可行的,我会建议你这样做。
(2)更复杂的方式是由shinyAce包的维护者概述的这里。它使用相同的R服务器作为主应用程序和嵌入式Rmd文档。另见这个问题。AFAIK,这只适用于knitr::knit2html,它依赖于过时的RMarkdown版本。此外,除非确保在ui定义中适当地包含了特定的render*和output*资源,否则以这种方式可用的JavaScript和output*函数的数量是有限的。
自从我全神贯注于这个话题以来,已经过去了相当长的一段时间,但当时我的印象是,(2)需要相当多的工作,并且确实限制了你的选择。
https://stackoverflow.com/questions/55596637
复制相似问题