我正在尝试将flexdashboard与Shiny状态书签结合起来。单独使用时(例如来自文档)Shiny app工作正常,但当放入flexdasboard时,url不会更新:
['apple.com', 'google.com', 'yahoo.com', 'aol.com', 'disney.com', 'mydomain.com', 'hotmail.com']
这甚至可能吗?与独立执行相比:
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart A
```{r}
shinyApp(
ui=function(req) {
fluidPage(
textInput("txt", "Text"),
checkboxInput("chk", "Checkbox")
)
},
server=function(input, output, session) {
observe({
# Trigger this observer every time an input changes
reactiveValuesToList(input)
session$doBookmark()
})
onBookmarked(function(url) {
updateQueryString(url)
})
},
enableBookmarking = "url"
)
```
看起来shinyApp(
ui=function(req) {
fluidPage(
textInput("txt", "Text"),
checkboxInput("chk", "Checkbox")
)
},
server=function(input, output, session) {
observe({
# Trigger this observer every time an input changes
reactiveValuesToList(input)
session$doBookmark()
})
onBookmarked(function(url) {
updateQueryString(url)
})
},
enableBookmarking = "url"
)
(以及onBookmarked
,onBookmark
和onRestore
等类似事件)永远不会被触发。
答案 0 :(得分:6)
R Markdown文档中嵌入的Shiny应用程序不支持书签。
请参阅此处的讨论:https://github.com/rstudio/shiny/pull/1209#issuecomment-227207713
听起来技术上可行,但很难做到。例如,如果文档中嵌入了多个应用程序,会发生什么?此外,应用程序嵌入为iframe,因此必须进行一些连线以允许这些应用程序访问/修改其父窗口的URL。
但是,书签确实适用于嵌入式Shiny组件(而不是完整的应用程序)。
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
enableBookmarking("url")
```
```{r, include=FALSE}
observe({
reactiveValuesToList(input)
session$doBookmark()
})
onBookmarked(function(url) {
updateQueryString(url)
})
output$content <- renderUI({
tagList(
textInput("txt", "Text"),
checkboxInput("chk", "Checkbox")
)
})
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart A
```{r}
fluidPage(
uiOutput("content"),
selectInput("sel", label = "Select", choices = c(10, 20, 30), selected = 10)
)
```
您也可以使用Prerendered Shiny Documents,但由于预呈现UI,因此书签不会100%相同。任何静态UI都必须使用bookmarking callbacks手动恢复,但动态UI可以恢复正常。
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny_prerendered
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
enableBookmarking("url")
```
```{r, context="server"}
observe({
reactiveValuesToList(input)
session$doBookmark()
})
onBookmarked(function(url) {
updateQueryString(url)
})
# Static inputs are pre-rendered, and must be manually restored
onRestored(function(state) {
updateSelectInput(session, "sel", selected = state$input$sel)
})
# Dynamic inputs will be restored with no extra effort
output$content <- renderUI({
tagList(
textInput("txt", "Text"),
checkboxInput("chk", "Checkbox")
)
})
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart A
```{r}
fluidPage(
uiOutput("content"),
selectInput("sel", label = "Select", choices = c(10, 20, 30), selected = 10)
)
```