2017-09-15 38 views
0

援助将不胜感激。来自SQlite数据库的多个表格的可用性

我正在研究一个闪亮的应用程序,它涉及多个SQlite数据库和rhandsontable包的使用。我在网上发现了很多有用的资料,但是我在使用这个软件包方面遇到了一些挫折,因为我花了两天的时间来解决一个我认为值得问的问题。

所以下面的脚本描述了服务器和rhandsontable的用户界面。我想能够使用户编辑,并安全地修改他们的表格(这是涵盖了很多网上),但跨越多个表(我正在努力)

我的代码做的是,它打开第1表,是的如果我做了修改它确实安全。但是,当我尝试通过选择输入转到另一个表格时,其他表格内容立即通过最初修改的表格获取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) 
    } 
) 
} 

回答

0

此代码未经测试,但希望它能正常工作。把下面为您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使得它在savebutton的初始化时不会触发save事件。

+0

感谢您回复我。我意识到我没有提供可复制的代码,因此不会对任何人或我自己有所帮助,因此可以对其进行测试以获得适当的答案。因此,我使用代码https://对问题进行了回购。 github.com/BrianMaja/EDSS_table_issue/tree/master/EDSS,我相信这会帮助你测试它,以便你能够帮助你。再次感谢! – nybre

+0

Gregor de Cillia,谢谢!! ..... it works !! ...在github链接上发布解决方案 – nybre