Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
"Exciting" new features in Shiny 1.0.1
# ----------------------------------------------------------------- #
# ------------- "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 &mu;<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