Last active
August 29, 2015 14:19
-
-
Save ttmmghmm/698488b10a9238f9e666 to your computer and use it in GitHub Desktop.
shiny examples
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
# https://github.com/daattali/shinyjs/blob/master/vignettes/overview.Rmd | |
library(shiny) | |
shinyApp( | |
ui = fluidPage( | |
div(id = "myapp", | |
h2("shinyjs demo"), | |
textInput("name", "Name", ""), | |
numericInput("age", "Age", 30), | |
textInput("company", "Company", ""), | |
p("Timestamp: ", span(date())), | |
actionButton("submit", "Submit") | |
) | |
), | |
server = function(input, output) { | |
} | |
) |
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
--- | |
title: "shiny em - Reset demo" | |
output: html_document | |
runtime: shiny | |
--- | |
Run Document from rstudio. Need to cut and paste token to publish to shinyapps.io | |
Here is a simple demo of reset in action | |
extendShinyjs - allows you to easily call your own JavaScript functions from R | |
```{r} | |
library(shiny) | |
library(shinyjs) | |
jsCode <- "shinyjs.pageCol = function(params){$('body').css('background', params);}" | |
runApp(shinyApp( | |
ui = fluidPage( | |
useShinyjs(), | |
extendShinyjs(text = jsCode), | |
selectInput("col", "Colour:", | |
c("white", "yellow", "red", "blue", "purple")) | |
), | |
server = function(input,output,session) { | |
observeEvent(input$col, { | |
js$pageCol(input$col) | |
}) | |
} | |
)) | |
``` |
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
# http://blog.revolutionanalytics.com/2014/10/introducing-minicran.html | |
library("miniCRAN") | |
sessionInfo() | |
pkgs <- c("chron") | |
pkgDep(pkgs) | |
p <- makeDepGraph(pkgs, enhances = TRUE) | |
# ?plot.pkgDepGraph | |
set.seed(20140917) | |
plot(p, cex=1.5, vertex.size=15) | |
# http://blog.revolutionanalytics.com/2014/07/dependencies-of-popular-r-packages.html | |
# determine all these dependencies? | |
# It is possible to do this using the function available.packages() and then query the resulting object. | |
# miniCRAN to list packages and their dependencies, in particular: | |
# pkgAvail() / pkgDep() / makeDepGraph() | |
library(miniCRAN) | |
pkgdata <- pkgAvail(repos = c(CRAN="http://cran.revolutionanalytics.com"), type="source") | |
head(pkgdata[, c("Depends", "Suggests")]) | |
get dependencies of the 7 popular tags on StackOverflow: | |
tags <- c("ggplot2", "data.table", "plyr", "knitr", | |
"shiny", "xts", "lattice") | |
( pkgList <- pkgDep(tags, availPkgs=pkgdata, suggests=TRUE) ) | |
p <- makeDepGraph(pkgList, availPkgs=pkgdata) | |
library(igraph) | |
plotColours <- c("grey80", "orange") | |
topLevel <- as.numeric(V(p)$name %in% tags) | |
# initial list only has 6 packacges | |
# to install the 7 most popular packages R packages (according to StackOverflow), R will in fact download and install up to 63 different packages! | |
######## | |
# https://modspace.mango-solutions.com/modspace/entryStructure.htm?entryId=49&elementId=693&type=published | |
# input: a numeric value between 1 and 500, a colour, a main title. | |
# output histogram of random data from any distribution where n is the numeric input | |
require(shiny) | |
library(shinythemes) # install.packages("shinythemes") | |
ui <- shinyUI(fluidPage( | |
theme = shinytheme(c("cerulean", "united", "cosmo", "flatly", "journal", "readable", "spacelab")[3]), | |
# Define the header for the page | |
titlePanel("Render Plot in a Shiny App"), | |
# Set up the page to have a sidebar | |
sidebarLayout( | |
# Define the contents of the sidebar | |
sidebarPanel( | |
numericInput("numberInput", "Select size of data:", min = 0, max = 50L, value = 1e1), | |
selectInput("colInput", "Select a colour", choices = c("yellow", "red", "blue", "green")) | |
), | |
# Define the contents of the main panel | |
mainPanel( | |
plotOutput("plotOutput") | |
) | |
) | |
)) | |
ss <- shinyServer(function(input, output){ | |
# Our data is only updated when the number of simulations is changed | |
simData <- reactive({ | |
rnorm(input$numberInput) | |
}) | |
output$plotOutput <- renderPlot( | |
hist(simData(), col = input$colInput) | |
) | |
}) | |
shinyApp(ui = ui, server = ss) | |
# takes a date string input (e.g. “30-03-2015”) and returns the following text: | |
# What day of the week is it (e.g. “Wednesday”) | |
# What month it is (e.g. “December”) | |
# What year it is | |
require(shiny) | |
ui <- shinyUI(fluidPage( | |
# Define the header for the page | |
titlePanel("Exercise 1"), | |
# Set up the page to have a sidebar | |
sidebarLayout( | |
# Define the contents of the sidebar | |
sidebarPanel( | |
dateInput("dateInput", "Select date") | |
), | |
# Define the contents of the main panel | |
mainPanel( | |
textOutput("dateOutput") | |
) | |
) | |
)) | |
ss <- shinyServer(function(input, output){ | |
output$dateOutput <- renderText( | |
format(input$dateInput, format = "A %A in %B. The year is %Y.") | |
) | |
}) | |
shinyApp(ui = ui, server = ss) | |
######### | |
library(shiny) | |
ss <- shinyServer(function(input, output){ | |
output$niceTextOutput <- renderText(paste("You entered the text:\n", input$myText)) | |
}) | |
ui <- shinyUI(fluidPage( | |
titlePanel("My First Shiny App!"), | |
sidebarLayout( | |
sidebarPanel( | |
textInput("myText", "Enter text here:") | |
), | |
mainPanel( | |
textOutput("niceTextOutput") | |
) | |
))) | |
shinyApp(ui = ui, server = ss) |
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) | |
# library(shinyapps) | |
test_app = shinyApp( | |
ui = fluidPage( | |
numericInput("n", "n", 1), | |
plotOutput("plot") | |
), | |
server = function(input, output) { | |
output$plot <- renderPlot( plot(head(cars, input$n)) ) | |
} | |
) | |
test_app | |
shinyapps::deployApp( test_app) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment