Created
March 30, 2017 17:21
-
-
Save bborgesr/5eeb3004d365f4042f578f271148ff8c to your computer and use it in GitHub Desktop.
"Exciting" new features in Shiny 1.0.1
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
# ----------------------------------------------------------------- # | |
# ------------- "Exciting" new features in Shiny 1.0.1 ------------ # | |
# ----------------------------------------------------------------- # | |
library(shiny) | |
# 1. ----------------------------------------------- # | |
# ------------------ reactiveVal() ----------------- # | |
# -------------------------------------------------- # | |
ui <- fluidPage( | |
actionButton("minus", "-1"), | |
actionButton("plus", "+1"), | |
br(), | |
textOutput("value") | |
) | |
# The comments below show the equivalent logic using reactiveValues() | |
server <- function(input, output, session) { | |
value <- reactiveVal(0) # rv <- reactiveValues(value = 0) | |
observeEvent(input$minus, { | |
newValue <- value() - 1 # newValue <- rv$value - 1 | |
value(newValue) # rv$value <- newValue | |
}) | |
observeEvent(input$plus, { | |
newValue <- value() + 1 # newValue <- rv$value + 1 | |
value(newValue) # rv$value <- newValue | |
}) | |
output$value <- renderText({ | |
value() # rv$value | |
}) | |
} | |
shinyApp(ui, server, options = list(launch.browser = rstudioapi::viewer)) | |
# 2. ----------------------------------------------- # | |
# ---------- choiceNames and choiceValues ---------- # | |
# -- for radioButtons() and checkoboxGroupinput() -- # | |
# -------------------------------------------------- # | |
ui <- fluidPage( | |
sidebarLayout( | |
sidebarPanel( | |
radioButtons("normal", "Normal radio buttons:", | |
c("Normal" = "norm", "Uniform" = "unif", | |
"Log-normal" = "lnorm", "Exponential" = "exp")), | |
hr(), | |
radioButtons("fancy", "Fancy radio buttons:", | |
choiceNames = list(tags$span(HTML("Normal μ<sub>1</sub>")), | |
icon("calendar", "fa-3x"), | |
icon("bed"), | |
icon("cog", lib = "glyphicon"), | |
img(src = "https://www.rstudio.com/wp-content/uploads/2016/09/RStudio-Logo-Blue-Gray-250.png")), | |
choiceValues = c("html", "icon1", "icon2", "icon3", "img"))), | |
mainPanel(textOutput("rb1"), textOutput("rb2")) | |
) | |
) | |
server <- function(input, output, session) { | |
output$rb1 <- renderText({ | |
paste("You chose", input$normal) | |
}) | |
output$rb2 <- renderText({ | |
paste("You chose", input$fancy) | |
}) | |
} | |
shinyApp(ui, server, options = list(launch.browser = rstudioapi::viewer)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment