New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updating a reactive value triggers observer of another one #1710

Closed
stla opened this Issue May 20, 2017 · 3 comments

Comments

Projects
None yet
3 participants
@stla

stla commented May 20, 2017

Hello,
The app below has the following behavior: when the user clicks on "-1", the observer of value2 is triggered (value2() is printed). However the click on "-1" triggers input$minus and this changes value1 only.
I don't know whether this could be problematic but that looks unexpected.

ui <- fluidPage(
  actionButton("minus", "-1"),
  actionButton("plus", "+1")
)

server <- function(input, output, session) {
  value1 <- reactiveVal(0)
  value2 <- reactiveVal(0)

  observeEvent(input$minus, {
    value1(value1()-1)
  })

  observeEvent(input$plus, {
    value2(value2()+1)
  })

  observe({ # this is triggered after click on "minus"
    print(value2())
  })
}

shinyApp(ui, server)
@stla

This comment has been minimized.

stla commented May 20, 2017

Below is the equivalent app with reactiveValues instead of reactiveVal. This behavior does not occur here.

ui <- fluidPage(
  actionButton("minus", "-1"),
  actionButton("plus", "+1")
)

server <- function(input, output, session) {
  RV <- reactiveValues(value1=0, value2=0)

  observeEvent(input$minus, {
    RV[["value1"]] <- RV[["value1"]]-1
  })

  observeEvent(input$plus, {
    RV[["value2"]] <- RV[["value2"]]+1
  })

  observe({
    print(RV[["value2"]])
  })
}

shinyApp(ui, server)
@wch

This comment has been minimized.

Collaborator

wch commented May 22, 2017

Hm, that does look strange. cc @jcheng5

Here's a stripped-down example:

library(shiny)

x <- reactiveVal(0)
y <- reactiveVal(0)

observe({
  print(paste("y is ", y()))
})

x(1)
shiny:::flushReact()
#  [1] "y is  0"

x(2)
shiny:::flushReact()
#  [1] "y is  0"

wch added a commit that referenced this issue May 22, 2017

@wch wch self-assigned this May 22, 2017

@wch wch added the review label May 22, 2017

@jcheng5 jcheng5 removed the review label May 22, 2017

@stla

This comment has been minimized.

stla commented May 23, 2017

I don't know whether this could be problematic

It could. Thank you for the fix, it works fine now.

dmpe added a commit to dmpe/shiny that referenced this issue May 24, 2017

Give each ReactiveVal separate dependents. Fixes rstudio#1710
Update links in CONTRIBUTING.md

Update the links to contributor agreements to reflect new versions that use my current email rather than rstudio.org based one.

I've made the same change in the rstudio and rmarkdown repos, we should make it in other repos that have a contributor agreement as well.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment