簡體   English   中英

有兩個操作按鈕,但是只有第一個操作按鈕(寫在服務器文件中)有效嗎?

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

在下面的代碼中,我有兩個反應性函數,例如A和B(我想通過隔離函數和兩個操作按鈕進行控制),但只有A可以工作。 如果我更改功能的順序(我的意思是,我先寫B,然后寫A),那么B是唯一起作用的函數。

該程序正在讀取和編輯一個postgreSQL數據庫。 函數B在表中插入新行。 函數A刪除同一表的一行。

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

        }) 


    })

用戶界面如下:

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

非常感謝你的幫助。

output要顯示的
在這里,插入和刪除更像是副作用 那你應該使用observeEvent

在下文中,我創建了兩個函數, observeEvent基於兩個actionButton insertdelete和從observeEvent調用它們。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM