Skip to content

Instantly share code, notes, and snippets.

@ijlyttle
Created August 14, 2013 20:59
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/6235554 to your computer and use it in GitHub Desktop.
Save ijlyttle/6235554 to your computer and use it in GitHub Desktop.
function to automate repeated groups of UI elements
library(shiny)
items <- c("seed")
shinyServer(function(input, output) {
# closure to generate reactive function to add items
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){
# get the (isolated) value of the UI element
new_item <- isolate(input[[name_value]])
# add it to the global vector
items <<- c(items, new_item)
}
# no need to return anything
return()
})
# return the "completed" function
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 the global vector
return(items)
})
# print out the contents of global vector
output$show_items <- renderPrint(get_items())
})
library(shiny)
text_button <- function(inputId_text, inputId_button,
label_text="", value_text="", label_button="Go!"){
text <- textInput(inputId=inputId_text, label=label_text, value=value_text)
button <- actionButton(inputId=inputId_button, label=label_button)
return(list(text, button))
}
shinyUI(pageWithSidebar(
# Application title
headerPanel("Test of reactivity and closures, combining UI elements"),
sidebarPanel(
text_button(inputId_text="item_add_a", inputId_button="item_add_a_go",
label_text="Add an item here", value_text=""),
text_button(inputId_text="item_add_b", inputId_button="item_add_b_go",
label_text="Or, add an item here", value_text="")
),
mainPanel(
verbatimTextOutput(outputId="show_items")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment