[英]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.我需要在 observeEvent 中调用 function(基于 select 输入),我需要调用两个函数,一个是获取属性名称,另一个是获取数据集名称。 i just wanna know how to call the function inside observeEvent.我只想知道如何在 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)
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
.请注意,在这种情况下, observeEvent
是多余的,因为findattribute()
和finddataset()
依赖于input$select
。 If they were wrapped in isolate()
then observeEvent()
would make a difference.如果它们被包裹在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)
})
}
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.如果您需要将函数保留在服务器之外,您可以定义两个函数,然后使用input$select
作为参数调用它们。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.