简体   繁体   中英

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

I need to call a function inside observeEvent (which is based on select input), i need to call two functions, one is to get attribute name and other to get dataset name. i just wanna know how to call the function inside observeEvent.

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)

Put the reactives inside the server, if not input not found error will occur.

Note than in this case observeEvent is redundant because findattribute() and finddataset() depend on input$select . If they were wrapped in isolate() then observeEvent() would make a difference.

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

If you need to keep the functions outside server you can define two functions that and then call them with input$select as an argument.

app:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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