Flexdashboard不适用于Shiny URL状态

use*_*262 7 r shiny flexdashboard

我试图将flexdashboard与Shiny状态书签结合起来.单独使用时(例如来自文档)Shiny app工作正常,但是当放入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"
)

```
Run Code Online (Sandbox Code Playgroud)

这甚至可能吗?与独立执行相比:

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"
)
Run Code Online (Sandbox Code Playgroud)

它看起来像onBookmarked(和类似的事件,如onBookmark,onRestoreonRestored)永远不会被触发.

gre*_*g L 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)
)
```
Run Code Online (Sandbox Code Playgroud)

您也可以使用Prerendered Shiny Documents,尽管由于UI是预渲染的,因此书签不会100%相同.任何静态UI都必须使用书签回调手动恢复,但动态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)
)
```
Run Code Online (Sandbox Code Playgroud)