来自 SQlite 数据库的多个表上的 rhandsontable

rhandsontable on multiple tables from SQlite database

将不胜感激。

我正在开发一个闪亮的应用程序,它涉及使用多个 SQlite 数据库和 rhandsontable 包。我在网上发现了很多关于使用这个软件包的有用信息 material,但我感到很沮丧,因为我花了 2 天时间解决一个我认为值得一问的问题。

因此下面的脚本描述了服务器和 rhandsontable 的 UI。我希望能够启用用户编辑,并保护他们修改后的 table(网上有很多介绍)但跨越多个 tables(我正在努力解决的问题)

我的代码所做的是打开第一个 table,是的,如果我进行修改,它确实安全。但是,当我尝试通过 select 输入转到另一个 table 时,另一个 table 内容立即被初始修改的内容 REPLACED

我真的希望修改是独立的而不影响其他 tables。

再次感谢您的帮助。

downloadTableUI <-  function(id) {
  ns <- NS(id)
  tagList(
    sidebarLayout(
      sidebarPanel( 
        selectInput(ns("dataset"), "Choose a dataset:",
          choices = dput(as.character(alltables[1: NROW(alltables)]))),
        radioButtons(ns("filetype"), "File type:",
          choices = c("csv", "tsv")),
        dateRangeInput(ns("daterange2"), "Date Filtration",
          start = "2017-02-17",
          end = "2017-03-07"),
        actionButton(ns("saveBtn"), "Save"),
        br(),
        downloadButton(ns('downloadData'), 'Download File', class = "btn-info")
      ),
      mainPanel(
        rHandsontableOutput(ns('tabletest'), width = 730, height = 600)
      ),
      position = c("left")
    )
  )
}

DownloadTable <-  function(input, output, session, pool) {
#select databases
  tableChoozer <- reactive({input$dataset})
  # dateSelector <- reactive({input$daterange2})

  # Initiate the reactive table
  p1 <- reactive({
    results <- dbGetQuery(pool, paste('select * from ', tableChoozer()))
    return (results) 
  })

  Mychanges <- reactive({

    observe({
    input$saveBtn# update database file each time the button is pressed
    if (!is.null(input$tabletest)) {#if there 's a table input
      dbWriteTable(pool, tableChoozer(),hot_to_r(input$tabletest), overwrite = TRUE, row.names = FALSE)# overwrite the database
    }
  })
#THIS IS WHERE I THINK THE PROBLEM IS
    if (is.null(input$tabletest)) {
      return (p1())
    } else if (!identical(p1(), input$tabletest)) {
      mytable <- as.data.frame(hot_to_r(input$tabletest))
      return (mytable)
    }
  })


output$tabletest <- renderRHandsontable({
    rhandsontable(Mychanges()) %>%
    hot_cols(columnSorting = TRUE, highlightCol = TRUE, highlightRow = TRUE,allowRowEdit = FALSE, allowColEdit = FALSE, exportToCsv = TRUE)
  })


  output$downloadData <- downloadHandler(
    filename = function() {
      paste("table.csv")
    },
    content = function(file) {
      sep <- switch (input$filetype, "csv" = ",", "tsv" = "\t")

      write.table(p1(), file, sep = sep, row.names = FALSE)
    }
  )
}

此代码未经测试,但希望它能工作。将以下内容放在 server.R 文件的顶层

observeEvent( input$saveBtn, 
  {
    # update database file each time the button is pressed
    if (!is.null(input$tabletest)) {
      #if there 's a table input
      dbWriteTable(pool, tableChoozer(),
        hot_to_r(input$tabletest), overwrite = TRUE, row.names = FALSE)
        # overwrite the database
   },
   ignoreInit = TRUE
)

使用 observeEvent 而不是 observe 可以防止对 tableChoozerinput$tabletest 的反应性依赖,这似乎是你的问题。 ignoreInit 使得在保存按钮初始化时不会触发保存事件。