Home > Back-end >  Independent reactions are interacting in a Shiny App
Independent reactions are interacting in a Shiny App

Time:02-10

I'm using the sliderInput function with the animate argument to perform an automatic calculation in my shinyApp. However, using animate is affecting other reactions in my app. I would like to stop this side effect that is occurring in other reactivities.

For example, I have a dropdownMenu inside an if else structure. But, because of the animate argument, I can't click the option in the top right corner when I start the animation. It is disappearing with each calculation that the animate is doing. See:

enter image description here

I tried use isolate() in pred_1() but it stop the valueBox and the conditional structure (if else structure).

I'd like to just make animate not affect other reactivity in the app.

My app:

library(shiny)
library(shinydashboard)

header <- dashboardHeader(
  
  title = "Dashboard", 
  titleWidth = 300, 
  
  dropdownMenuOutput(
    outputId = "drop1"
  )
  
)

sidebar <- dashboardSidebar(
  
  width = 300
  
)

body <- dashboardBody(
  
  sliderInput(
    inputId = "one", 
    label = "Registro 1", 
    value = 1, 
    animate = animationOptions(
      interval = 500, loop = TRUE
    ),  
    min = 1, 
    max = 10, 
    step = 1, 
    ticks = TRUE
  ), 
  
  sliderInput(
    inputId = "two",
    label = "Registro 2", 
    value = 1, 
    animate = animationOptions(
      interval = 500, loop = TRUE
    ),  
    min = 1, 
    max = 10, 
    step = 1, 
    ticks = TRUE
  ), 
  
  sliderInput(
    inputId = "three", 
    label = "Sum 3", 
    value = 1, 
    animate = animationOptions(
      interval = 500, loop = TRUE
    ),  
    min = 1, 
    max = 10, 
    step = 1, 
    ticks = TRUE
  ), 
  
  valueBoxOutput(
    outputId = "box1"
  )
  
)

ui <- dashboardPage(
  
  header = header, 
  sidebar = sidebar, 
  body = body
  
)

server <- function(session, input, output) {
  
  fx <- function(x, y) {
    
    x   y
    
  }
  
  fy <- function(x) {
    
    x
    
  }
  
  reac_0 <- reactive({
    
    tibble::tibble(
      one = input$one, 
      two = input$two, 
      three = input$three
    )
    
  })
  
  chuveiro <- reactive({
    
    temp <- reac_0()
    fx(
      x = temp$one, 
      y = temp$two
    )
    
  })
  
  luz <- reactive({
    
    temp <- reac_0()
    fy(
      x = temp$three
    )
    
  })
  
  fdrop <- function(x) {
    
    if (x <= 5) {
        
      dropdownMenu(
        type = "notifications",
        badgeStatus = NULL,
        headerText = "Not 1", 
        notificationItem(
          text = HTML(
            "<text style='color:#020202;'>Without.</text>"
          ),
          status = "danger",
          icon = icon("times"),
          href = NULL
        )
      )
      
    } else (
      dropdownMenu(
        type = "notifications",
        badgeStatus = "danger",
        headerText = "Not 2", 
        notificationItem(
          text = HTML(
            "<a style='color:#020202;'
          href='https://www.instagram.com' target='_blank' title='A'>
          With</a>"
          ),
          status = "success",
          icon = icon("tags")
        )
      )
    )
  }
  
  output$drop1 <- renderMenu({
    
    expr = fdrop(x = luz())
    
  })
  
  output$box1 <- renderValueBox({
    
    expr = valueBox(
      value = chuveiro(), 
      subtitle = "Sum"
    )
    
  })
  
}

shinyApp(ui, server) 

So how can I make one reactivity effect another in a shinyApp?

I would like to click normally on the dropdownMenu notification with the sliderInput working with animate.

I tried isolating each argument of sliderInput, but not work.

CodePudding user response:

The reac0() reactive is triggered whenever an input changes. Then it triggers every reactive that includes it, even if the value used in that reactive has not changed.

Your code :

reac_0 <- reactive({
  tibble::tibble(
    one = input$one, 
    two = input$two, 
    three = input$three
  )
})
 
luz <- reactive({
  temp <- reac_0()
  fy(
    x = temp$three
  )
})

triggers luz() whenever one, two or three changes.

An alternative would be to use input$three directly:

luz <- reactive({
  fy(
    x = input$three
  )
})

This way, the change or animation of slider one and two won't trigger luz() which won't trigger the menu rendering.

Of course, since the value of slider three is used to render the menu, any update of its value has to trigger the menu rendering.

  • Related