Learn R Programming

spsComps (version 0.3.3.0)

incRv: In-line numeric operation for reactiveVal

Description

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.

Usage

incRv(react, value = 1)

multRv(react, value = 2)

diviRv(react, value = 2)

Value

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

Arguments

react

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

value

the numeric value to do the operation on react

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

Run this code
reactiveConsole(TRUE)
rv <- reactiveVal(0)
incRv(rv) # add 1
rv()
incRv(rv) # add 1
rv()
incRv(rv, -1) # minus 1
rv()
incRv(rv, -1) # minus 1
rv()
rv2 <- reactiveVal(1)
multRv(rv2) # times 2
rv2()
multRv(rv2) # times 2
rv2()
diviRv(rv2) # divide 2
rv2()
diviRv(rv2) # divide 2
rv2()
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)
}

Run the code above in your browser using DataLab