![](/img/trans.png)
[英]Why do my observeEvent() calls in R Shiny not do anything once they have reactive expressions inside of them?
[英]R Shiny: Check condition based on reactive expressions in observeEvent
我想構建一個帶有兩個選項卡的 Shiny 應用程序:
在一個選項卡中,輸入一些值作為輸入。 在下一個選項卡中,用戶可以找到基於在第一個選項卡中輸入的值的 output。
但是,在繼續 output 之前,我想檢查三個條目的總和是否會給出第四個條目。 為此,我想使用包含不同條目值的反應式表達式。
這是我想做的一個例子:
# clean environment
rm(list = ls(all = TRUE))
library(shiny)
# Create user interface (UI)
u <- tagList(
navbarPage(
# UI for input
title = "",
id = "Example_App",
tabPanel("Model input",
fluidRow(
column(11, offset = 0,
br(),
h4("Model input"),
br(),
sidebarPanel(
div(textInput('str_Input1', 'Input 1\n', "",
placeholder = "5.6, 6.7, 4.1"), class = "subheading"),
div(textInput('str_Input2', 'Input 2\n', "",
placeholder = "5.6, 6.7, 4.1"), class = "subheading"),
div(textInput('str_Input3', 'Input 3\n', "",
placeholder = "5.6, 6.7, 4.1"), class = "subheading"),
div(textInput('str_Input4', 'Input 4\n', "",
placeholder = "5.6, 6.7, 4.1"), class = "subheading"),
actionButton('jumpToModelOutput', 'Run')),
mainPanel(
h4('You entered'),
verbatimTextOutput("oid_Input1"),
verbatimTextOutput("oid_Input2"),
verbatimTextOutput("oid_Input3"),
verbatimTextOutput("oid_Input4"))))),
# UI for output
tabPanel("Model output",
fluidRow(
column(11, offset = 0,
br(),
h4('Your output will be here.'))
))))
# Define server output
s <- shinyServer(function(input, output, session) {
# Define reactive expressions
num_Input1 <- reactive(as.numeric(unlist(strsplit(input$str_Input1,","))))
num_Input2 <- reactive(as.numeric(unlist(strsplit(input$str_Input2,","))))
num_Input3 <- reactive(as.numeric(unlist(strsplit(input$str_Input3,","))))
num_Input4 <- reactive(as.numeric(unlist(strsplit(input$str_Input4,","))))
# Define server output for input check
output$oid_Input1 <- renderPrint({
cat("Input 1:\n")
print(num_Input1())
})
output$oid_Input2 <- renderPrint({
cat("Input 2:\n")
print(num_Input2())
})
output$oid_Input3 <- renderPrint({
cat("Input 3:\n")
print(num_Input3())
})
output$oid_Input4 <- renderPrint({
cat("Input 4:\n")
print(num_Input4())
})
# Check if conditions are fulfilled before switching to Model output
observeEvent(input$jumpToModelOutput, {
if(!all.equal((num_Input1() + num_Input2() + num_Input3()),num_Input4())){
showNotification("Error.", type = "error")
}else{
updateTabsetPanel(session, "Example_App",
selected = "Model output")
}})
})
# Create the Shiny app
shinyApp(u, s)
當我在所有選項卡中輸入“1,2,3”並按下按鈕時,應用程序停止並收到以下消息:“Listening on http://127.0.0.1:3925警告:錯誤:無效參數類型”
刪除:給出以下消息:警告:如果參數不可解釋為邏輯錯誤
就我收到的消息而言,反應式表達式不會被解釋為數字(?),但將它們相加並打印會給出正確的結果。
誰能幫我找到問題?
問題是all.equal
返回一個字符串,其中包含傳遞值差異的報告。 這就是文檔(參見?all.equal
)state 的原因:
不要在 if 表達式中直接使用 all.equal——如果合適,可以使用 isTRUE(all.equal(....)) 或相同。
因此,要解決您的問題,請將其包含在isTRUE
:
observeEvent(input$jumpToModelOutput, {
if (!isTRUE(all.equal(num_Input1() + num_Input2() + num_Input3(), num_Input4()))) {
showNotification("Error.", type = "error")
} else {
updateTabsetPanel(session, "Example_App",
selected = "Model output"
)
}
})
如果元素不相等, all.equal
返回一個字符串,並且不能使用!
在一個字符串上。 您可以先用isTRUE
檢查它是否為TRUE
,然后將其取反(注意:您不能使用isFALSE
因為如果它不是TRUE
, all.equal
返回一個字符串)。 如果您希望元素完全相等,則可以使用identical
的方法使事情變得更容易。
在添加它們之前,我還總結了每個輸入中的所有元素,這是你想要做的嗎?
# Check if conditions are fulfilled before switching to Model output
observeEvent(input$jumpToModelOutput, {
if(!isTRUE(all.equal((sum(num_Input1()) + sum(num_Input2()) + sum(num_Input3())),sum(num_Input4())))){
showNotification("Error.", type = "error")
}else{
updateTabsetPanel(session, "Example_App",
selected = "Model output")
}})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.