In-place operations like i += 1, i -= 1 is not support in R. These functions implement these operations in R. This set of functions will apply this kind of operations on [shiny::reactiveVal] objects.

incRv(react, value = 1)

multRv(react, value = 2)

diviRv(react, value = 2)

Arguments

react

reactiveVal object, when it is called, should return an numeric object

value

the numeric value to do the operation on react

Value

No return, will directly change the reactiveVal object provided to the react argument

Details

incRv(i) is the same as i <- i + 1. incRv(i, -1) is the same as i <- i - 1. multRv(i) is the same as i <- i * 2. diviRv(i) is the same as i <- i / 2.

See also

If you want shiny::reactiveValues version of these operators or just normal numeric objects, use spsUtil::inc, spsUtil::mult, and spsUtil::divi.

Examples

reactiveConsole(TRUE)
rv <- reactiveVal(0)
incRv(rv) # add 1
rv()
#> [1] 1
incRv(rv) # add 1
rv()
#> [1] 2
incRv(rv, -1) # minus 1
rv()
#> [1] 1
incRv(rv, -1) # minus 1
rv()
#> [1] 0
rv2 <- reactiveVal(1)
multRv(rv2) # times 2
rv2()
#> [1] 2
multRv(rv2) # times 2
rv2()
#> [1] 4
diviRv(rv2) # divide 2
rv2()
#> [1] 2
diviRv(rv2) # divide 2
rv2()
#> [1] 1
reactiveConsole(FALSE)
# Real shiny example
if(interactive()){
  ui <- fluidPage(
    textOutput("text"),
    actionButton("b", "increase by 1")
  )
  server <- function(input, output, session) {
    rv <- reactiveVal(0)
    observeEvent(input$b, {
      incRv(rv)
    })
    output$text <- renderText({
      rv()
    })
  }
  shinyApp(ui, server)
}