Created
August 14, 2013 20:58
-
-
Save ijlyttle/6235539 to your computer and use it in GitHub Desktop.
closure to automate reactivity functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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