繁体   English   中英

调用 function,并将值返回给 shiny app 中的 observeEvent

[英]Call function, and return the value back to observeEvent in shiny app

我需要在 observeEvent 中调用 function(基于 select 输入),我需要调用两个函数,一个是获取属性名称,另一个是获取数据集名称。 我只想知道如何在 observeEvent 中调用 function。

library(shiny)
library(shinydashboard)

ui<-
  
  selectInput("select", "LPS tolerance induction:", c("", "LPS_Stim. at 0 h vs unstim" , "LOVE_Stim. at 20 h vs unstim",
                                                      "Restimulation (stim. at 0 h and 20 h vs unstim.)" ,
                                                      "Tolerance (stim. at 0 h and 20 h vs stim. at 20 h)"))

findattribute <- reactive({
  if(grepl("LPS",input$select)){
    x <- "val1"
    return(x)
  }
  else if(grepl("LOVE", input$select)){
    x <- "val2"
    return(x)
  }
  else if(grepl("Res", input$select)){
    x <- "val3"
    return(x)
  }
})

finddataset <- reactive({
  if(grepl("LPS",input$select)){
    x <- curated_lps # dataset name
    return(x)
  }
  
})

server<- function(input, output){
  
  observeEvent(input$select, {
    
    attribute <- findattribute()
    dataset <- finddataset()
  })
}

shinyApp(ui, server)

将反应器放在服务器中,如果没有输入未找到错误将发生。

请注意,在这种情况下, observeEvent是多余的,因为findattribute()finddataset()依赖于input$select 如果它们被包裹在isolate()中,那么observeEvent()就会有所作为。

server <- function(input, output, session) {
  
  findattribute <- reactive({
    if(grepl("LPS",input$select)){
      x <- "val1"
      return(x)
    }
    else if(grepl("LOVE", input$select)){
      x <- "val2"
      return(x)
    }
    else if(grepl("Res", input$select)){
      x <- "val3"
      return(x)
    }
  })
  
  finddataset <- reactive({
    if(grepl("LPS",input$select)){
      x <- curated_lps # dataset name
      return(x)
    }
    
  })
  
  observeEvent(input$select, {
    attribute <- findattribute()
    dataset   <- finddataset()
    print(attribute)
    print(dataset)
  })
}

如果您需要将函数保留在服务器之外,您可以定义两个函数,然后使用input$select作为参数调用它们。

应用程序:

library(shiny)
library(shinydashboard)

curated_lps <- iris

ui <-
  selectInput("select", "LPS tolerance induction:", c(
    "", "LPS_Stim. at 0 h vs unstim", "LOVE_Stim. at 20 h vs unstim",
    "Restimulation (stim. at 0 h and 20 h vs unstim.)",
    "Tolerance (stim. at 0 h and 20 h vs stim. at 20 h)"
  ))


findattribute <- function(input){
  if(grepl("LPS",input)){
    x <- "val1"
    return(x)
  }
  else if(grepl("LOVE", input)){
    x <- "val2"
    return(x)
  }
  else if(grepl("Res", input)){
    x <- "val3"
    return(x)
  }
}

finddataset <- function(input){
  if(grepl("LPS",input)){
    x <- curated_lps # dataset name
    return(x)
  }
  
}

# SERVER ------------------------------------------------------------------


server <- function(input, output, session) {
  
  observe({
    attribute <- findattribute(input$select)
    dataset   <- finddataset(input$select)
    print(attribute)
    print(dataset)
  })
}

shinyApp(ui, server)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM