Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将书签保存到表中并检索它_R_Shiny - Fatal编程技术网

R 将书签保存到表中并检索它

R 将书签保存到表中并检索它,r,shiny,R,Shiny,我正在尝试将Shining应用程序中的书签URL保存到一个表中,以便用户单击保存的URL检索应用程序的书签状态。当您点击书签按钮时,您会得到一个URL,如何将其插入到表中。当保存在表格中时,带有每个已保存书签的视图按钮将允许用户查看已保存书签的状态 ui <- function(request) { fluidPage( plotOutput("plot"), sliderInput("n", "Number of observations", 1, nrow(fai

我正在尝试将Shining应用程序中的书签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)

ui以下是我的方法:

编辑:现在使用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)
库(闪亮)
图书馆(RSQLite)
库(数据表)

非常感谢。只是几个问题。此表可以插入到数据库中。第二,当我点击任何书签时,表就会消失。确保表可以保存在DB中(我有时间时会添加一个sqlite示例-实际上这就是我提到全局保存文件的原因)。该表将消失,因为单击该链接时,新会话将启动(请尝试右键单击->在新选项卡中打开)。谢谢。与书签URL一起,我可以用某种名称替换URL,这样用户就可以很容易地通过名称而不是URL进行检索。当然,这也是可能的。只需替换href粘贴中的第二个
未列出(myBookmarks$url)
(很抱歉,当前在手机上,因此我无法正确编辑)。因此,我添加了文本输入,正如您所看到的,我修改了您的答案。现在所有书签的名称都相同。