Shiny feedbackDanger doesnt work inside eventReactive where function is called

Viewed 40

I am begginer in shiny an I am stucked adding feedback in my app.

I have tried a few things like write this code inside the eventReactive function like use the function feedBackDanger. Below, there is a simplified full code with the ui, the idea is that i need the user get some Error (but not the console Error) if he set 'zero' in kind variable when mean is 3,6 or 9.

Also the actionButton 'simulate' should be disable when this condition is selected.

ui <- shinyUI(fluidPage(
 

  titlePanel(h1("Simulation", align = 'center')),
 
  sidebarLayout(
    
    
    
    sidebarPanel(
      
      
            numericInput(inputId = "n", label = "Size of Sample", min = 1,
                         step = 1, value = 1),
            numericInput(inputId = "mean", label = "Mean of Sample", min = 1,
                         step = 1, value = 1),
      
    
            numericInput(inputId = "var", label = "Variance", min = 1,
                         step = 0.25, value = 1),
        
       
            radioButtons("kind", "Sample kind", choices = c("two", "zero")),
        
      
      
      
            actionButton("simulate", "Simulate"),
        
        
      
      
      
      width = 200
      
    ),
    
    
    mainPanel(
      
        plotOutput("distPlot", width = 500, height = 500)
    
  )
)
))


# Define server logic required to draw a histogram
server <- shinyServer(function(input, output, session) {
  
  s_fin <-function(n,
                   mean,
                   var,
                   kind){
    
    a <- rnorm(n, mean, var)
    if(kind == 'two'){
      
    a <-  a + 2
    
    }
    if(kind == 'zero'& mean %in% c(3,6,9)){
      print('ERROR: function error')
      stop(call. = T)}
    return(a)
  }
  
  
  simulation <- eventReactive(input$simulate,{

  
    s_fin(n = input$n,
          mean = input$mean,
          var = input$var,
          kind = input$kind)

  })
  
  
  output$distPlot <- renderPlot({
    
    hist(simulation())
    
  })

  
})



shinyApp(ui, server)
1 Answers

The next code works to me (a just add useShinyFeedback() in ui.R, and put the error function instead of print):

library(shinyFeedback)
ui <- shinyUI(fluidPage(
  useShinyFeedback(),
  
  titlePanel(h1("Simulation", align = 'center')),
  
  sidebarLayout(
    
    
    
    sidebarPanel(
      
      
      numericInput(inputId = "n", label = "Size of Sample", min = 1,
                   step = 1, value = 1),
      numericInput(inputId = "mean", label = "Mean of Sample", min = 1,
                   step = 1, value = 1),
      
      
      numericInput(inputId = "var", label = "Variance", min = 1,
                   step = 0.25, value = 1),
      
      
      radioButtons("kind", "Sample kind", choices = c("two", "zero")),
      
      
      
      
      actionButton("simulate", "Simulate"),
      
      
      
      
      
      width = 200
      
    ),
    
    
    mainPanel(
      
      plotOutput("distPlot", width = 500, height = 500)
      
    )
  )
))


# Define server logic required to draw a histogram
server <- shinyServer(function(input, output, session) {
  
  s_fin <-function(n,
                   mean,
                   var,
                   kind){
    
    a <- rnorm(n, mean, var)
    if(kind == 'two'){
      
      a <-  a + 2
      
    }
    if(kind == 'zero'& mean %in% c(3,6,9)){
      showFeedbackDanger(
        inputId = "mean",
        text = "Not use mean 3, 6 or 9"
      ) 
      shinyjs::disable("simulate")
    }else{
      hideFeedback("mean")
      shinyjs::enable("simulate")
    }
    return(a)
  }
  
  
  simulation <- eventReactive(input$simulate,{
    
    
    s_fin(n = input$n,
          mean = input$mean,
          var = input$var,
          kind = input$kind)
    
  })
  
  
  output$distPlot <- renderPlot({
    
    hist(simulation())
    
  })
  
  
})



shinyApp(ui, server)
Related