URL书签R闪亮

时间:2019-01-31 16:29:06

标签: r shiny

我有一个带有多个标签的闪亮应用。每个选项卡都有数据表,图表。在一个标签中,我尝试使用URL书签功能。当我将此书签用作单独的闪亮标签时,我可以单击加书签的URL,它将进入加书签的状态。但是在这个更大的应用程序中,当我使用相同的代码时,URL相当长,并且不会重定向到已标记状态。这就是加书签的URL的样子

library(shiny)
library(ggplot2)
library(DT)
library(shinyjqui)
library(shinydashboard)
library(shinydashboardPlus)
library(data.table)

ui <- navbarPage(
  "Navbar!",
  tabPanel("Plot",
           sidebarLayout(
             sidebarPanel(radioButtons(
               "plotType", "Plot type",
               c("Scatter" = "p", "Line" = "l")
             )),
             mainPanel(plotOutput("plot"))
           )),
  tabPanel(
    "Summary",
    fluidPage(
      plotOutput("bookmarkplot"),
      sliderInput("n", "Number of observations", 1, nrow(faithful), 100),
      fluidRow(column(
        2,
        textInput(
          inputId = "description",
          label = "Bookmark description",
          placeholder = "Data Summary"
        )
      ), column(2, bookmarkButton(id = "bookmarkBtn"))),
      DT::dataTableOutput("urlTable", width = "100%"),
      tags$style(type = 'text/css', "#bookmarkBtn { width:100%; margin-top: 25px;}")
    )
  ),
  navbarMenu(
    "More",
    tabPanel("Table",
             DT::dataTableOutput("table")),
    tabPanel("About",
             fluidRow(column(
               3,
               img(
                 class = "img-polaroid",
                 src = paste0(
                   "http://upload.wikimedia.org/",
                   "wikipedia/commons/9/92/",
                   "1919_Ford_Model_T_Highboy_Coupe.jpg"
                 )
               ),
               tags$small(
                 "Source: Photographed at the Bay State Antique ",
                 "Automobile Club's July 10, 2005 show at the ",
                 "Endicott Estate in Dedham, MA by ",
                 a(href = "http://commons.wikimedia.org/wiki/User:Sfoskett",
                   "User:Sfoskett")
               )
             )))
  )
)

server <- function(input, output, session) {
  output$plot <- renderPlot({
    plot(cars, type = input$plotType)
  })

  output$summary <- renderPrint({
    summary(cars)
  })

  output$table <- DT::renderDataTable({
    DT::datatable(cars)
  })

  #BOOKMARK AND SAVING THEM
  myBookmarks <- reactiveValues(urlDF = NULL)
  observeEvent(input$bookmarkBtn, {
    session$doBookmark()
  })

  if (file.exists("bookmarks.rds")) {
    myBookmarks$urlDF <- readRDS("bookmarks.rds")
  } else {
    myBookmarks$urlDF <- NULL
  }

  session$onSessionEnded(function() {
    tmpUrlDF <- isolate({
      myBookmarks$urlDF
    })
    if (!is.null(tmpUrlDF)) {
      saveRDS(tmpUrlDF, "bookmarks.rds")
    }
  })

  setBookmarkExclude(
    c(
      "bookmarkBtn",
      "data_table_rows_all",
      "data_table_rows_current",
      "data_table_rows_selected",
      "data_table_rows_search",
      "data_table_rows_state",
      "data_table_rows_last_clicked",
      "bar",
      "navbar",
      "Scenario",
      "description",
      "urlTable_cell_clicked",
      "urlTable_rows_all",
      "urlTable_rows_current",
      "urlTable_rows_selected",
      "urlTable_search",
      "urlTable_state",
      "urlTable_row_last_clicked"
    )
  )

  output$bookmarkplot <- renderPlot({
    hist(faithful$eruptions[seq_len(input$n)], breaks = 40)
  })

  onBookmarked(
    fun = function(url) {
      if (!url %in% myBookmarks$urlDF$URL) {
        if (is.null(myBookmarks$urlDF)) {
          myBookmarks$urlDF <-
            unique(
              data.table(
                Description = input$description,
                URL = paste0("<a href='", url, "'>", url, "</a>"),
                Timestamp = Sys.time(),
                Session = session$token
              ),
              by = "URL"
            )
        } else {
          myBookmarks$urlDF <-
            unique(rbindlist(list(
              myBookmarks$urlDF,
              data.table(
                Description = input$description,
                URL = paste0("<a href='", url, "'>", url, "</a>"),
                Timestamp = Sys.time(),
                Session = session$token
              )
            )), by = "URL")
        }
      }
    }
  )

  output$urlTable = DT::renderDataTable({
    req(myBookmarks$urlDF)
    myBookmarks$urlDF
  }, escape = FALSE)

  enableBookmarking(store = "url")
}
shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:2)

根据您的描述,我猜对于更复杂的应用,您正在达到此article中提到的编码状态URL的浏览器限制:

  

在编码状态下,如果值很多,URL可能会很长。某些浏览器的URL长度限制为大约2,000个字符,因此,如果书签URL的长度超出限制,则在这些浏览器中将无法正常工作。

因此,您应该通过设置开始使用保存到服务器的书签

enableBookmarking(store = "server")

代替:

enableBookmarking(store = "url")

编辑:同样要使此功能正常工作,您的UI代码必须包装在以request作为参数的函数中:

第二次编辑:将id = "myNavbarPage"添加到了navbarPage-因此它将被识别为书签的输入(并相应地恢复了)。

library(shiny)
library(ggplot2)
library(DT)
library(shinyjqui)
library(shinydashboard)
library(shinydashboardPlus)
library(data.table)

ui <- function(request) {navbarPage(
  "Navbar!", id = "myNavbarPage",
  tabPanel("Plot",
           sidebarLayout(
             sidebarPanel(radioButtons(
               "plotType", "Plot type",
               c("Scatter" = "p", "Line" = "l")
             )),
             mainPanel(plotOutput("plot"))
           )),
  tabPanel(
    "Summary",
    fluidPage(
      plotOutput("bookmarkplot"),
      sliderInput("n", "Number of observations", 1, nrow(faithful), 100),
      fluidRow(column(
        2,
        textInput(
          inputId = "description",
          label = "Bookmark description",
          placeholder = "Data Summary"
        )
      ), column(2, bookmarkButton(id = "bookmarkBtn"))),
      DT::dataTableOutput("urlTable", width = "100%"),
      tags$style(type = 'text/css', "#bookmarkBtn { width:100%; margin-top: 25px;}")
    )
  ),
  navbarMenu(
    "More",
    tabPanel("Table",
             DT::dataTableOutput("table")),
    tabPanel("About",
             fluidRow(column(
               3,
               img(
                 class = "img-polaroid",
                 src = paste0(
                   "http://upload.wikimedia.org/",
                   "wikipedia/commons/9/92/",
                   "1919_Ford_Model_T_Highboy_Coupe.jpg"
                 )
               ),
               tags$small(
                 "Source: Photographed at the Bay State Antique ",
                 "Automobile Club's July 10, 2005 show at the ",
                 "Endicott Estate in Dedham, MA by ",
                 a(href = "http://commons.wikimedia.org/wiki/User:Sfoskett",
                   "User:Sfoskett")
               )
             )))
  )
)}

server <- function(input, output, session) {
  output$plot <- renderPlot({
    plot(cars, type = input$plotType)
  })

  output$summary <- renderPrint({
    summary(cars)
  })

  output$table <- DT::renderDataTable({
    DT::datatable(cars)
  })

  #BOOKMARK AND SAVING THEM
  myBookmarks <- reactiveValues(urlDF = NULL)
  observeEvent(input$bookmarkBtn, {
    session$doBookmark()
  })

  if (file.exists("bookmarks.rds")) {
    myBookmarks$urlDF <- readRDS("bookmarks.rds")
  } else {
    myBookmarks$urlDF <- NULL
  }

  session$onSessionEnded(function() {
    tmpUrlDF <- isolate({
      myBookmarks$urlDF
    })
    if (!is.null(tmpUrlDF)) {
      saveRDS(tmpUrlDF, "bookmarks.rds")
    }
  })

  setBookmarkExclude(
    c(
      "bookmarkBtn",
      "data_table_rows_all",
      "data_table_rows_current",
      "data_table_rows_selected",
      "data_table_rows_search",
      "data_table_rows_state",
      "data_table_rows_last_clicked",
      "bar",
      "navbar",
      "Scenario",
      "description",
      "urlTable_cell_clicked",
      "urlTable_rows_all",
      "urlTable_rows_current",
      "urlTable_rows_selected",
      "urlTable_search",
      "urlTable_state",
      "urlTable_row_last_clicked"
    )
  )

  output$bookmarkplot <- renderPlot({
    hist(faithful$eruptions[seq_len(input$n)], breaks = 40)
  })

  onBookmarked(
    fun = function(url) {
      if (!url %in% myBookmarks$urlDF$URL) {
        if (is.null(myBookmarks$urlDF)) {
          myBookmarks$urlDF <-
            unique(
              data.table(
                Description = input$description,
                URL = paste0("<a href='", url, "'>", url, "</a>"),
                Timestamp = Sys.time(),
                Session = session$token
              ),
              by = "URL"
            )
        } else {
          myBookmarks$urlDF <-
            unique(rbindlist(list(
              myBookmarks$urlDF,
              data.table(
                Description = input$description,
                URL = paste0("<a href='", url, "'>", url, "</a>"),
                Timestamp = Sys.time(),
                Session = session$token
              )
            )), by = "URL")
        }
      }
    }
  )

  output$urlTable = DT::renderDataTable({
    req(myBookmarks$urlDF)
    myBookmarks$urlDF
  }, escape = FALSE)

  enableBookmarking(store = "server")
}
shinyApp(ui = ui, server = server)

请参见?enableBookmarking或更早的answer

相关问题