在单击按钮 我需要什么?

问题描述

大家好,

我计划在执行更新查询后(通过按下相应的按钮)使我的数据表自动更新。所以现在发生的是以下情况:

  1. 用户将单击导入数据库按钮将数据直接导入数据库
  2. 然后,用户将单击相应的按钮,如下所示(完成,进行中和完成)。单击任何这些按钮将导致更新查询以更新sql数据库

    enter image description here

3。然后,用户将必须单击“导入数据”以刷新数据表。

我需要什么?

我打算省略用户再次单击导入数据以刷新数据表的需要。相反,只要单击按钮,数据表就会自动刷新。

标签的UI部分:

            # Extract data from server,edit and reupload 
            tabItem(
                
                tabName="data_edit",fluidRow(
                    
                    Box(width=2,background = "blue",h4("Step 1: Import sql data"),# Step 1: Import sql data
                        
                        actionBttn(inputId = "import_button",label = "Import data",color = "primary",style = "material-flat"),br(),h4("Step 2: Make adjustment on main panel and choose status:"),actionBttn(inputId = "completed_button",label = "Completed",color = "success",actionBttn(inputId = "in_progress_button",label = "In Progress",color = "warning",actionBttn(inputId = "not_done_button",label = "Not Done",color = "danger",h4("Step 3: Click on 'IMPORT DATA' again to refresh!")
                    
                        ),DT::dataTableOutput({
                        "sql_file"
                    }),

此部分的Server.R文件

server <- function(input,output,session) {
  observeEvent(input$import_button,{
    pw <- {
      "pw"
    }
    
    # loads the Postgresql driver
    drv <- dbDriver("Postgresql")
    
    # creates a connection to the postgres database
    # note that "con" will be used later in each connection to the database
    con <- dbConnect(drv,dbname = "test_db",host = "10.22.71.121",port = 5432,user = "user",password = pw)
    
    #query the database and store the data in datafame
    getQuery <- reactive({
    
    sql_df <- dbGetQuery(con,"SELECT * from apple ;")
    return(sql_df)
    })

    # Convert sql data to datatable
    output$sql_file <- DT::renderDataTable({

      DT::datatable(getQuery(),style = "default",filter = 'top',options = list(pageLength = 16,autoWidth = FALSE,scrollY = 650,scrollX = 500))

    })
  
    
    # Exporting changes to sql database
    
    # Completed button
    observeEvent(input$completed_button,{
      s = getQuery()$row[input$sql_file_rows_selected]
      
      # Change the "status" column in the dataframe as "Done"
      for (i in s){
        query <- sprintf("UPDATE public.apple SET status='completed' WHERE row = %s",i)
        dbSendQuery(con,query)
        cat("\nUpdated as completed for: ",i)
        session$reload()
      }

    })
    
    # In Progress button
    observeEvent(input$in_progress_button,{
      s = getQuery()$row[input$sql_file_rows_selected]
      
      # Change the "Status" column in the dataframe as "Done"
      for (i in s){
        query <- sprintf("UPDATE public.apple SET status='in_progress' WHERE row = %s",query)
        cat("\nUpdated as in progress for: ",i)
        session$reload()
      }
      
    })
    
    # Not done button
    observeEvent(input$not_done_button,{
      s = getQuery()$row[input$sql_file_rows_selected]
      
      # Change the "Status" column in the dataframe as "Done"
      for (i in s){
        query <- sprintf("UPDATE public.apple SET status='not_done' WHERE row = %s",query)
        cat("\nUpdated as not done for: ",i)
        session$reload()
      }
      
    })
    
    
    # disconnect the database after user exit the application. Best solution to resolve connection expire issue.
    session$onSessionEnded(function(){
      dbdisconnect(con)
    }) 
  })
}

解决方法

使用eventReactive应该可以满足您的需求。试试这个

server <- function(input,output,session) {
  observeEvent(input$import_button,{
    pw <- {
      "pw"
    }
    
    # loads the PostgreSQL driver
    drv <- dbDriver("PostgreSQL")
    
    # creates a connection to the postgres database
    # note that "con" will be used later in each connection to the database
    con <- dbConnect(drv,dbname = "test_db",host = "10.22.71.121",port = 5432,user = "user",password = pw)
    
    #query the database and store the data in datafame
    getQuery <- reactive({
      sql_df <- dbGetQuery(con,"SELECT * from apple ;")
      return(sql_df)
    })
    DF1 <- reactiveValues(data=getQuery())
    
    # Convert sql data to datatable
    output$sql_file <- DT::renderDataTable({
      
      DT::datatable(DF1$data,style = "default",filter = 'top',options = list(pageLength = 16,autoWidth = FALSE,scrollY = 650,scrollX = 500))
      
    })
    
    # Exporting changes to SQL database
    
    # Completed button
    observeEvent(input$completed_button,{
      s = getQuery()$row[input$sql_file_rows_selected]
      
      # Change the "status" column in the dataframe as "Done"
      for (i in s){
        query <- sprintf("UPDATE public.apple SET status='completed' WHERE row = %s",i)
        dbSendQuery(con,query)
        cat("\nUpdated as completed for: ",i)
        session$reload()
      }
      
    })
    DF1C <- eventReactive(input$completed_button,{
      sql_dfc <- dbGetQuery(con,"SELECT * from apple ;")
      return(sql_dfc)
    })
    DF1$data <- DF1C
    
    # In Progress button
    observeEvent(input$in_progress_button,{
      s = getQuery()$row[input$sql_file_rows_selected]
      
      # Change the "Status" column in the dataframe as "Done"
      for (i in s){
        query <- sprintf("UPDATE public.apple SET status='in_progress' WHERE row = %s",query)
        cat("\nUpdated as in progress for: ",i)
        session$reload()
      }
      
    })
    DF1P <- eventReactive(input$in_progress_button,{
      sql_dfp <- dbGetQuery(con,"SELECT * from apple ;")
      return(sql_dfp)
    })
    DF1$data <- DF1P
    
    # Not done button
    observeEvent(input$not_done_button,{
      s = getQuery()$row[input$sql_file_rows_selected]
      
      # Change the "Status" column in the dataframe as "Done"
      for (i in s){
        query <- sprintf("UPDATE public.apple SET status='not_done' WHERE row = %s",query)
        cat("\nUpdated as not done for: ",i)
        session$reload()
      }
      
    })
    DF1ND <- eventReactive(input$not_done_button,{
      sql_dfnd <- dbGetQuery(con,"SELECT * from apple ;")
      return(sql_dfnd)
    })
    DF1$data <- DF1ND
    
    # Disconnect the database after user exit the application. Best solution to resolve connection expire issue.
    session$onSessionEnded(function(){
      dbDisconnect(con)
    }) 
  })
}