Skip to content

Instantly share code, notes, and snippets.

@aagarw30
Last active September 18, 2020 17:47
Show Gist options
  • Save aagarw30/316a65598b048476819dce76504d154d to your computer and use it in GitHub Desktop.
Save aagarw30/316a65598b048476819dce76504d154d to your computer and use it in GitHub Desktop.
Multiple Action buttons using reactiveValues() - an example
Title: reactiveValues() function
Description: With reactiveValues(), you can create your own reactive values. reactiveValues() Creates a list of objects that can be manipulated within a reactive context (within observer or observerEvent with dependency on changes in certain input or state of an object). reactiveValues() objects are not reactive themselves and do not re-execute themselves when input value changes unlike reactive objects. Powered by R, Shiny, and RStudio.
License: GPL-3
Author: Abhinav Agrawal
DisplayMode: Showcase
Tags: R, R Shiny,reactiveValues(), observeEvent()
Type: Shiny
library(shiny)
shinyServer((function(input, output) {
values <- reactiveValues(uno = 0, dos = 0, tres = 0)
# Definining and initializing the reactiveValues object with 3 reactive values namely, uno, dos, tres.
# uno would serve for the reactive value for the first button
# dos as reactive value for second button
# tres as reactive value for third button
# Idea is to set one of the reactive values to 1 when corresponding buttons are clicked and rest to zero. Further based on if conditional statement print which button is pressed.
observeEvent(input$buttonuno, {
values$uno <- 1
values$dos <- 0
values$tres <- 0
})
observeEvent(input$buttondos, {
values$uno <- 0
values$dos <- 1
values$tres <- 0
})
observeEvent(input$buttontres, {
values$uno <- 0
values$dos <- 0
values$tres <- 1
})
output$display <- renderText(
{
if(values$uno)
paste("Button # 1 selected")
else
if(values$dos)
paste("Button # 2 selected")
else
if(values$tres)
paste("Button # 3 selected")
else
return()
}
)
}))
library(shiny)
shinyUI((fluidPage(
"Multiple Action buttons using reactiveValues() - an example",
br(),
mainPanel(
actionButton("buttonuno", "Button 1"),
actionButton("buttondos", "Button 2"),
actionButton("buttontres", "Button 3"),
br(),
textOutput("display")
)
)))
@Prateek26394
Copy link

Getting the error , instead of buttons I used selectInput
Warning: Error in eventReactive: unused arguments (input1 = 0, input2 = 0, input3 = 0, input4 = 0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment