r - 将 R Shiny 书签保存到表中并检索它
问题描述
我正在尝试将闪亮应用程序中的书签 URL 保存到表中,因此如果用户单击保存的 URL 以检索应用程序的书签状态。当您单击书签按钮时,您会获得一个 URL,如何将其插入到表格中。当保存在表格中时,带有每个已保存书签的查看按钮将允许用户查看已保存的书签状态。
ui <- function(request) {
fluidPage(
plotOutput("plot"),
sliderInput("n", "Number of observations", 1, nrow(faithful), 100),
bookmarkButton()
)
}
server <- function(input, output, session) {
output$plot <- renderPlot({
hist(faithful$eruptions[seq_len(input$n)], breaks = 40)
})
}
enableBookmarking(store = "url")
shinyApp(ui, server)
解决方案
这是我将如何处理这个问题:
编辑:现在使用 sqlite 在不同的会话中保留更改,也避免了重复。
第二次编辑:添加了书签的描述输入。
library(shiny)
library(RSQLite)
library(data.table)
ui <- function(request) {
fluidPage(
plotOutput("plot"),
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;}")
)
}
server <- function(input, output, session) {
con <- dbConnect(RSQLite::SQLite(), "bookmarks.db", overwrite = FALSE)
myBookmarks <- reactiveValues(urlDF = NULL)
observeEvent(input$bookmarkBtn, {
session$doBookmark()
})
if(dbExistsTable(con, "Bookmarks")){
tmpUrlDF <- data.table(dbReadTable(con, "Bookmarks"))
myBookmarks$urlDF <- tmpUrlDF[, Timestamp := as.POSIXct(Timestamp, origin="1970-01-01 00:00")]
} else {
myBookmarks$urlDF <- NULL
}
session$onSessionEnded(function() {
tmpUrlDF <- isolate({myBookmarks$urlDF})
if(!is.null(tmpUrlDF)){
dbWriteTable(con, "Bookmarks", tmpUrlDF, overwrite = TRUE)
}
dbDisconnect(con)
})
setBookmarkExclude(c("bookmarkBtn", "description", "urlTable_cell_clicked", "urlTable_rows_all", "urlTable_rows_current", "urlTable_rows_selected", "urlTable_search", "urlTable_state", "urlTable_row_last_clicked"))
output$plot <- 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({
myBookmarks$urlDF
}, escape=FALSE)
}
enableBookmarking(store = "url")
shinyApp(ui, server)
您可能想考虑将书签表保存到文件中(例如,使用saveRDS()
)并全局加载它,以便它可用于新会话。有关详细信息,请参阅此。
推荐阅读
- javascript - 如何使用javascript在对象和数组的深层嵌套数组中获取具有特定值的孩子的父母?
- python - 如何根据反转的字典值修改 Pandas DataFrame?
- python - 从搁置文件中删除所有键(使用 Python 自动化无聊的东西 - 第 9 章 - 扩展多剪贴板)
- android - 无法解析很多android库?
- python - 方法内部的 Pickle 类实例
- python - 如何将 numpy.ndarray 中的值写入 Dataframe?
- javascript - 在 React js 中出现此错误的原因是什么 - TypeError: this.state.data.map is not a function
- python - 如何将另一个 python 项目作为子模型添加到我的项目中?
- electron - 如何在安装后禁用电子应用程序的自动启动?
- file - 如何最好地删除文件夹及其子文件夹中的所有文件