0

在下面的代码中,我有两个反应函数,比如 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"
        )
      )
    ))

非常感谢你的帮助。

4

1 回答 1

1

output为了展示
这里的插入和删除更像是副作用。那么你应该使用observeEvent

下面我创建了 2 个函数,insert并基于两个函数delete调用它们。observeEventactionButton

服务器.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)
    })


})

用户界面

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"
    )
  )
))
于 2017-10-20T09:03:35.537 回答