Skip to content

Instantly share code, notes, and snippets.

@beader
Created December 30, 2015 09:44
Show Gist options
  • Save beader/f79846f25a4febf704d8 to your computer and use it in GitHub Desktop.
Save beader/f79846f25a4febf704d8 to your computer and use it in GitHub Desktop.
simple shiny dashboard
id_maker <- function(prefix = make_random_string()) {
function(id) {
paste(prefix, id, sep = "_")
}
}
make_random_string <- function(n=1, len=12, chars=c(0:9, letters, LETTERS)) {
randomString <- sapply(seq(n), function(i) {
paste(sample(chars, len, replace = TRUE), collapse = "")
})
randomString
}
# server.R
library(shiny)
source("tab1_page.R")
source("tab2_page.R")
source("tab3_page.R")
source("id_maker.R")
shinyServer(function(input, output, session) {
output$tab1_page <- renderUI(create_tab1_page(session = session))
output$tab2_page <- renderUI(create_tab2_page(session = session))
output$tab3_page <- renderUI(create_tab3_page(session = session))
})
# tab1_page.R
create_tab1_page <- function(session) {
id <- id_maker()
tab_page <- fluidPage(
"this is tab1 page",
selectInput(id('select_input'), "select a letter", choices = letters),
verbatimTextOutput(id('text_output'))
)
on.exit({
input <- session$input
output <- session$output
output[[id('text_output')]] <- renderText({
input[[id('select_input')]]
})
})
return(tab_page)
}
# tab2_page.R
create_tab2_page <- function(session) {
id <- id_maker()
tab_page <- fluidPage(
"this is tab2 page"
)
return(tab_page)
}
# tab3_page.R
create_tab3_page <- function(session) {
id <- id_maker()
tab_page <- fluidPage(
"this is tab3 page"
)
return(tab_page)
}
# ui.R
library(shiny)
library(shinydashboard)
header <- dashboardHeader(
title = "shiny app"
)
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("tab1", tabName = "tab1", icon = icon("pie-chart")),
menuItem("tab2", tabName = "tab2", icon = icon("retweet")),
menuItem("tab3", tabName = "tab3", icon = icon("users"))
)
)
body <- dashboardBody(
tabItems(
tabItem(tabName = "tab1", uiOutput("tab1_page")),
tabItem(tabName = "tab2", uiOutput("tab2_page")),
tabItem(tabName = "tab3", uiOutput("tab3_page"))
)
)
shinyUI(dashboardPage(header, sidebar, body))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment