Skip to content

Instantly share code, notes, and snippets.

@ijlyttle
Created August 14, 2013 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ijlyttle/6235539 to your computer and use it in GitHub Desktop.
Save ijlyttle/6235539 to your computer and use it in GitHub Desktop.
closure to automate reactivity functions
library(shiny)
items <- c("seed")
shinyServer(function(input, output) {
cl_add_item <- function(name_value, name_button){
# function to add item
add_item <- reactive({
# register button
n_click <- input[[name_button]]
if (n_click > 0){
new_item <- isolate(input[[name_value]])
items <<- c(items, new_item)
}
return()
})
return(add_item)
}
add_item_a <- cl_add_item("item_add_a", "item_add_a_go")
add_item_b <- cl_add_item("item_add_b", "item_add_b_go")
get_items <- reactive({
# register "adders"
add_item_a()
add_item_b()
return(items)
})
output$show_items <- renderPrint(get_items())
})
library(shiny)
shinyUI(pageWithSidebar(
# Application title
headerPanel("Test of reactivity and closures"),
sidebarPanel(
# add item & go button
textInput(inputId="item_add_a", label="Add an item here", value=""),
actionButton(inputId="item_add_a_go", label="Go!"),
# add item
textInput(inputId="item_add_b", label="Or, add an item here", value=""),
actionButton(inputId="item_add_b_go", label="Go!")
),
mainPanel(
verbatimTextOutput(outputId="show_items")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment