2017-02-10 47 views
0

我有一個數據庫中的大量數據,我可以使用shiny中的反應函數調用。 我想用rhandsontable顯示選定的數據,根據需要進行更新並將數據發送回數據庫。來自rhandsontable數據庫的反應性閃亮對象

我在嘗試選擇另一個反應性對象內的反應性對象時遇到了困難。 我知道如何根據this example對內存中的數據做到這一點,但正如我所說,我有很多數據不適合內存。

見重複的例子,下面,我只是想選擇不同choice選項和t4值設置爲F,但是當我選擇下拉菜單中的新反應的數據表不更新。

library(shiny) 
library(rhandsontable) 
library(dplyr) 
library(magrittr) 
library(RSQLite) 
library(DBI) 


## create data : 
dat <- data.frame("id" = 1:10, 
       "choice" = rep(c("option 1", "option 2"), each = 5), 
       "t1" = sample(1:100, 10), 
       "t2" = sample(1:100, 10), 
       "t3" = sample(1:100, 10), 
       "t4" = rep("T", 10)) 

## define database 
test_db <- src_sqlite("test_db.sqlite3", create = T) 

## copy to database: 
test_sqlite <- copy_to(test_db, dat, temporary = FALSE, indexes = list(
    c("choice"),"t1", "t2", "t3", "t4")) 

## test data is loaded: 
dbGetQuery(test_db$con, paste0("SELECT * FROM dat")) 


## build shiny app: 

shinyApp(
    shinyUI(
    fluidRow(
    selectInput("select", label = h3("Select box"), 
       choices = list("option 1", "option 2"), 
       selected = "option 1"), 
    rHandsontableOutput("hot"), 
    actionButton("to_db", label = "Send to Database"), 
    verbatimTextOutput("to_db_text") 
)), 

shinyServer(function(input, output, session) { 


## define data to select 
select_dat <- eventReactive(input$select, { 
    dbGetQuery(test_db$con, paste0("SELECT * FROM dat WHERE choice = '", input$select, "'")) 
    }) 

# debugging 
observe({print(input$select)}) 
observe({print(select_dat())}) 

values = reactiveValues() 

data = reactive({ 
    if (!is.null(input$hot)) { 
    DF = hot_to_r(input$hot) 
    } else { 
    if (is.null(values[["DF"]])) 
     DF = select_dat() 
    else 
     DF = values[["DF"]] 
    } 
    values[["DF"]] = DF 
    DF 
}) 

output$hot <- renderRHandsontable({ 
    DF = data() 
    if (!is.null(DF)) 
    rhandsontable(DF, stretchH = "all", selectCallback = TRUE, readOnly = T) %>% 
    hot_col("t4", readOnly = F, type = "dropdown", source = c("T","F")) 
}) 

## debugging  
observe({print(data())}) 

ntext <- eventReactive(input$to_db, { 
    ids <- data() %>% filter(t4 == "F") %>% dplyr::select(id) %>% extract2(1) 
    sql_str <- paste0("UPDATE dat SET t4 = 'F' WHERE id IN (", paste(ids, collapse=","),")") 
    dbExecute(test_db$con, sql_str) 

}) 

observe({print(ntext())}) 

}) 
) 

任何幫助,這將不勝感激!

非常感謝

+0

當我運行您的代碼時,我得到了該警告(警告:eventReactiveHandler中的錯誤:無法找到函數「dbExecute」);加載DBI後,它似乎工作。它在控制檯中打印表格 – MLavoie

+0

謝謝MLavoie,我已經將DBI添加到了包中:) – anniemaggs

+0

MLavoie,它將rhandsontable的輸出作爲我嵌入的調試的一部分進行打印,但是您當選擇不同的選項時,將看到它不會改變「選擇」。代碼不會出錯,但它不會執行我想要的操作,如果這樣做有道理。 – anniemaggs

回答

0

使用反應性對象,select_dat()在單獨observe({})功能回答我的問題。使用與以前相同的輸入:

## build shiny app: 

shinyApp(
    shinyUI(
    fluidRow(
     selectInput("select", label = h3("Select box"), 
        choices = list("option 1", "option 2"), 
        selected = "option 1"), 
     rHandsontableOutput("hot"), 
     actionButton("to_db", label = "Send to Database"), 
     verbatimTextOutput("to_db_text") 
    )), 

    shinyServer(function(input, output, session) { 


    ## define data to select 
    select_dat <- eventReactive(input$select, { 
     dbGetQuery(test_db$con, paste0("SELECT * FROM dat WHERE choice = '", input$select, "'")) 
    }) 

    # debugging 
#  observe({print(input$select)}) 
#  observe({print(select_dat())}) 

    ## define data to be updated in rhandsontable: 
    values = reactiveValues(data=NULL) 

    observe({ 
     input$select 
     values$data <- select_dat() 
    }) 

    observe({ 
     if(!is.null(input$hot)) 
     values$data <- hot_to_r(input$hot) 
    }) 


    output$hot <- renderRHandsontable({ 
     rhandsontable(values$data) 
    }) 

## debugging  
    observe(print(values$data)) 

## send data to database  
    ntext <- eventReactive(input$to_db, { 
     ids <- values$data %>% filter(t4 == "F") %>% dplyr::select(id) %>% extract2(1) 
     sql_str <- paste0("UPDATE dat SET t4 = 'F' WHERE id IN (", paste(ids, collapse=","),")") 
     dbExecute(test_db$con, sql_str) 

    }) 

    observe({print(ntext())}) 

    }) 
) 
相關問題