根据不同的Shiny textInput和按钮触发不同的代码段

时间:2019-01-09 15:32:00

标签: r shiny shiny-server

我有两个textInputs,每个都有自己的actionButton。我想有一个eventReactive(s)函数,可以同时监听这两者并根据按下哪个按钮来触发单个代码。我的用户界面是:

# load umls MRCONSO and MRREL tables

ui2<-  shinyUI(fluidPage(

    # Application title
    titlePanel("UMLS Browser"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
      sidebarPanel(
        radioButtons("rd",
                     label = "Query type:",
                     choices = list("Child of" = "CHD",
                                    "Parent of" = "PAR"),
                     selected = "CHD"),
        textInput("code", "Code",""),
        actionButton("do1", "Search"),
        textInput("term", "Term",""),
        actionButton("do2", "Search")

      ),
      mainPanel(
        tableOutput(list("table","table2"))
      )
    )
  )
)



#setwd('/srv/shiny_server/umlsbrowser')
#deployApp()
rm(list = ls())
library(shiny)
library(dplyr)
umls <- dbConnect(drv=RSQLite::SQLite(), 
                  dbname="umls_browser.sqlite3")

和server.r

shinyServer(function(input, output) {

  source('ui1.R') #login page
  source('ui2.R') # UMLS page



  output$page <- renderUI({ ui1 })

  # code here from Untitled2
  observeEvent(input$login,{
    output$page <- renderUI({ ui1 })
    z<-system(paste("perl", "umls_auth.pl",
                    paste0("'",input$user,"'"), paste0("'",input$password,"'")),intern=TRUE)
    if (grepl("false",z[22])) {
      #output$page <- renderUI({ ui1 })
      renderText("Incorrect credentials")
    }
    if (grepl("true",z[22])) 
    {
      output$page <- renderUI({ ui2 })
      res <- eventReactive(list(input$do1,input$do2), {
        return(as.data.frame(dbGetQuery(umls, 
                                        paste0("SELECT DISTINCT B.CUI2, C.SAB, C.CODE, C.STR FROM MRCONSO A 
                                           INNER JOIN MRREL B ON A.CUI = B.CUI1 
                                           INNER JOIN MRCONSO C ON B.CUI2 = C.CUI 
                                           WHERE B.REL = '", input$rd ,"'","AND A.STR ='", input$term ,"'","LIMIT 100",sep=""))))

        return(as.data.frame(dbGetQuery(umls, 
                                        paste0("SELECT DISTINCT B.CUI2, C.SAB, C.CODE, C.STR FROM MRCONSO A 
                                           INNER JOIN MRREL B ON A.CUI = B.CUI1 
                                           INNER JOIN MRCONSO C ON B.CUI2 = C.CUI 
                                           WHERE B.REL = '", input$rd ,"'","AND A.STR ='", input$code ,"'","LIMIT 100",sep=""))))
      })
      output$table <- renderTable({res()})
    }
  })

})

如您所见,我有两个预定义的sql,但是每个textInputs作为变量,并使用单独的actionButton触发。根据要设置的textInput和按下的actionButton,我想触发适当的sql语句。

当前,以上代码仅触发actionButton input$do2。按input$do1不会导致任何事情。如果有的话,那就太棒了。

0 个答案:

没有答案