I am trying to figure out how to debug in Shiny. I would like to print a message to the console if a reactive takes a specific value. I know I can put a message inside of the reactive but I am curious to know if/how I do it from outside of the of reactive call it self. I tried to use an observeEvent()
and it seems to trigger every time the reactive changes. What is the right way to print a message based on the value in a reactive?
library(shiny)
ui <- fluidPage(
textInput("name_first", "What's your first name?"),
textInput("name_last", "What's your last name?"),
textOutput("greeting")
)
server <- function(input, output, server) {
dude <- reactive(paste(input$name_first, input$name_last))
# why does this not work?
observeEvent(dude() == "a zombie", {message("run")})
output$greeting <- renderText(dude())
}
shinyApp(ui, server)
CodePudding user response:
You can use req()
to trigger an event only if a reactive has a certain value.
observeEvent(dude(),{
req(dude() == "a zombie")
message("run")
})
For debugging shiny apps in general, you can have a look at the browser()
function
CodePudding user response:
A possibility:
isZombie <- reactive({
if(dude() == "a zombie") TRUE # no need of 'else NULL'
})
observeEvent(isZombie(), {message("run")})