来自SQlite数据库

时间:2017-09-15 15:27:43

标签: r shiny rhandsontable

非常感谢协助。

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

因此下面的脚本描述了服务器和rhandsontable的UI。我希望能够启用用户编辑,并保护他们修改过的表(在线很多),但是跨多个表(我正在努力解决的问题)

我的代码所做的是它打开第一个表,是的如果我进行修改它确实安全。但是当我尝试通过选择输入转到另一个表时,其他表格内容会立即被初始修改后的内容获得 REPLACED

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

再次,我们将非常感谢您的帮助。

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)
    }
  )
}

1 个答案:

答案 0 :(得分:1)

此代码未经测试,但希望它能够正常运行。将以下内容放在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使得在保存按钮初始化时不会触发保存事件。