Skip to content

Instantly share code, notes, and snippets.

@daattali
Last active February 1, 2016 00:16
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 daattali/b95fe2376c9e16f35f15 to your computer and use it in GitHub Desktop.
Save daattali/b95fe2376c9e16f35f15 to your computer and use it in GitHub Desktop.
Little shiny apps used in my shinyjs tutorial http://bit.ly/shinyjs-slides
# See http://daattali.com/shiny/colourInput/ for more demos
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
colourInput("col", "Colour", "blue")
# colourInput("col", "Colour", "#ff0000",
# palette = "limited", showColour = "background")
)
server <- function(input, output, session) {
observe({
cat(input$col, "\n")
})
}
shinyApp(ui = ui, server = server)
# See http://daattali.com/shiny/shinyjs-demo/ for more demos
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
actionButton("btn", "Hide"),
textInput("text", "Text")
)
server <- function(input, output) {
observeEvent(input$btn, {
hide("text")
})
}
shinyApp(ui = ui, server = server)
# See http://daattali.com/shiny/shinyjs-demo/ for more demos
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
actionButton("btn", "Hide"),
actionButton("btn2", "Show"),
textInput("text", "Text")
)
server <- function(input, output) {
observeEvent(input$btn, {
hide("text")
})
observeEvent(input$btn2, {
show("text")
})
}
shinyApp(ui = ui, server = server)
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
inlineCSS("#error { color: red; }"),
div(id = "form",
textInput("name", "Name:", "Dean"),
numericInput("age", "Age:", 27),
checkboxInput("terms", "I agree to the terms and conditions", FALSE),
actionButton("submit", "Submit")
),
hidden(
span(id = "error",
"Error:",
span(id = "errormsg")
),
h3(id = "thankyou", "Thank you")
)
)
server <- function(input, output, session) {
observe({
toggleState(id = "submit", condition = (input$terms && nzchar(input$name)))
})
observeEvent(input$submit, {
hide("error")
hide("thankyou")
if (input$age < 13) {
show("error")
html("errormsg", "Sorry, we don't like kids")
logjs("For undisclosed reasons, we don't allow anyone under the age of 13")
return()
}
reset("form")
show("thankyou")
delay(2000, hide("thankyou", anim = TRUE, animType = "fade"))
})
}
shinyApp(ui = ui, server = server)
---
runtime: shiny
---
```{r, echo=FALSE}
library(shinyjs)
useShinyjs(rmd = TRUE)
actionButton("button", "Click me")
div(id = "hello", "Hello!")
observeEvent(input$button, {
toggle("hello")
})
```
library(shiny)
library(shinyjs)
jscode <- '
shinyjs.foo = function(params) {
alert(params);
}
'
ui <- fluidPage(
useShinyjs(),
extendShinyjs(text = jscode),
textInput("text", "Text"),
actionButton("btn", "Go")
)
server <- function(input, output, session) {
observeEvent(input$btn, {
js$foo(input$text)
})
}
shinyApp(ui = ui, server = server)
library(shiny)
library(shinyjs)
jscode <- '
shinyjs.foo = function(params) {
var defaultParams = {
name : "Dean",
age : 27
}
params = shinyjs.getParams(params, defaultParams);
alert(params.name + " is " + params.age);
}
'
ui <- fluidPage(
useShinyjs(),
extendShinyjs(text = jscode),
textInput("name", "Name", "Dean"),
numericInput("age", "Age", 27),
actionButton("btn", "Go")
)
server <- function(input, output, session) {
observeEvent(input$btn, {
js$foo(age = input$age, name = input$name)
})
}
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment