Search code examples
rpostgresqlshinyaction-buttonshiny-reactivity

Two action buttons, but only the first one, that is written in the server file, works?


In the following code I have two reactive functions, say A and B (which I want to control via an isolate function and two action buttons), but only A works at all. If I change the order of the functions (I mean, I first write B and then A), then B is the only one that works.

The program is reading and editing a postgreSQL database. Function B inserts a new line in the table. Function A deletes a line of the same table.

    shinyServer(

    function(input, output) {

        pool <- dbPool(
          drv = dbDriver("PostgreSQL", max.con = 100),
          dbname = "postgres",
          host = "localhost",
          user = "postgres",
          password = "123",
          idleTimeout = 36000
        )

        # Show table in the ui #
        output$view <- renderDataTable({

          dbTable<-dbGetQuery(pool, "SELECT * FROM Table")
          dbTable <- select(dbTable, name, lastname)
          names(dbTable)<-c('Name', 'Lastname')
          dbTable

        }) 

        # show droplist in the ui #
        output$selector <- renderUI({

          droplist  <- dbGetQuery(pool, "SELECT name FROM Table")
          selectInput("paria",  "Delete row", droplist$name) 

        }) 

        # Delete Row # Function A #
        output$val1 <- renderText({

          delete <- sprintf(
            "DELETE FROM %s WHERE %s = '%s'",
            "Table",
            "name",
            input$paria
          )

          if (input$action1 == 0)
            return()
          isolate({
            dbGetQuery(pool, delete) 
            print("Deletion Successful")
          })

        }) 

        # Insert row # Function B #
        output$val1 <- renderText({

          query <- sprintf(
            "INSERT INTO %s (%s, %s) VALUES ('%s', '%s')",
            "Table", 
            "name",
            "lastname",
            input$name,
            input$lastname
          )

          if (input$action2 == 0)
            return()
          isolate({
            dbGetQuery(pool, query) 
            print("Update Successful")
          })

        }) 


    })

The ui is the following:

    shinyUI(pageWithSidebar(

      headerPanel("Little Program"),

      sidebarPanel(
        conditionalPanel(condition="input.conditionedPanels==1",
                         textInput("name", "Name"),
                         textInput("lastname", "Lastname"),
                         actionButton("action", "Save new person"),
                         br(),br(),
                         uiOutput("selector"),
                         actionButton("action2", "Delete existing person")
        ),
        conditionalPanel(condition="input.conditionedPanels==2",
                         helpText("Content Panel 2")
        ) 
      ),
      mainPanel(
        tabsetPanel(
          tabPanel("Table", value=1, verbatimTextOutput("val1"), dataTableOutput('view')), 
          tabPanel("Panel 2", value=2)
          , id = "conditionedPanels"
        )
      )
    ))

Thank you very much for the help.


Solution

  • output is to display.
    Here the insert and delete are more like side effects. Then you should use observeEvent

    Herebelow I created 2 functions, insert and delete and call them from observeEvent based on two actionButton.

    server.R

    shinyServer(
    
    function(input, output) {
    
        pool <- dbPool(
          drv = dbDriver("PostgreSQL", max.con = 100),
          dbname = "postgres",
          host = "localhost",
          user = "postgres",
          password = "123",
          idleTimeout = 36000
        )
    
        # Show table in the ui #
        output$view <- renderDataTable({
    
          dbTable<-dbGetQuery(pool, "SELECT * FROM Table")
          dbTable <- select(dbTable, name, lastname)
          names(dbTable)<-c('Name', 'Lastname')
          dbTable
    
        }) 
    
        # show droplist in the ui #
        output$selector <- renderUI({
    
          droplist  <- dbGetQuery(pool, "SELECT name FROM Table")
          selectInput("paria",  "Delete row", droplist$name) 
    
        }) 
    
        # Delete function
        delete <- function(paria) {
            queryDelete <- sprintf(
                "DELETE FROM %s WHERE %s = '%s'",
                "Table",
                "name",
                paria
            )
            dbGetQuery(pool, queryDelete) 
            print("Deletion Successful")
        }
    
        # Insert function
        insert <- function(name, lastname) {
            queryInsert <- sprintf(
                "INSERT INTO %s (%s, %s) VALUES ('%s', '%s')",
                "Table", 
                "name",
                "lastname",
                name,
                lastname
            )
            dbGetQuery(pool, queryInsert) 
            print("Insert Successful")
        }
    
        # when delete
        observeEvent(input$delete, {
            delete(paria = input$paria)
        })
    
        # When insert 
        observeEvent(input$insert, {
            insert(name = input$name, lastname = input$lastname)
        })
    
    
    })
    

    ui.R

    shinyUI(pageWithSidebar(
    
      headerPanel("Little Program"),
    
      sidebarPanel(
        conditionalPanel(condition="input.conditionedPanels==1",
                         textInput("name", "Name"),
                         textInput("lastname", "Lastname"),
                         actionButton("action", "Save new person"),
                         actionButton("delete", "DELETE !"),
                         actionButton("insert", "INSERT !")
                         br(),br(),
                         uiOutput("selector")
        ),
        conditionalPanel(condition="input.conditionedPanels==2",
                         helpText("Content Panel 2")
        ) 
      ),
      mainPanel(
        tabsetPanel(
          tabPanel("Table"
            ,dataTableOutput('view')
          ), 
          tabPanel("Panel 2", value=2)
          , id = "conditionedPanels"
        )
      )
    ))