Flexdashboard 不适用于 Shiny URL 状态

Flexdashboard doesn't work with Shiny URL state

我正在尝试将 flexdashboard 与 Shiny state 书签相结合。单独使用时(来自文档的示例)Shiny 应用程序工作正常,但是当放入 flexdasboard 时,url 不会更新:

---
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(以及 onBookmarkonRestoreonRestored 等类似事件)从未被触发。

R Markdown 文档中嵌入的 Shiny 应用程序不支持书签。

在此处查看讨论:https://github.com/rstudio/shiny/pull/1209#issuecomment-227207713

听起来这在技术上是可行的,但做起来很棘手。例如,如果文档中嵌入了多个应用程序会怎样?此外,应用程序作为 iframe 嵌入,因此必须进行一些连接才能允许这些应用程序 access/modify 它们的父级 window 的 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, although bookmarking would not work 100% the same since the UI is pre-rendered. Any static UI would have to be manually restored with 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)
)
```