Created
December 30, 2015 09:44
-
-
Save beader/f79846f25a4febf704d8 to your computer and use it in GitHub Desktop.
simple shiny dashboard
This file contains hidden or 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
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 | |
} |
This file contains hidden or 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
# 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)) | |
}) |
This file contains hidden or 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
# 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) | |
} |
This file contains hidden or 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
# tab2_page.R | |
create_tab2_page <- function(session) { | |
id <- id_maker() | |
tab_page <- fluidPage( | |
"this is tab2 page" | |
) | |
return(tab_page) | |
} |
This file contains hidden or 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
# tab3_page.R | |
create_tab3_page <- function(session) { | |
id <- id_maker() | |
tab_page <- fluidPage( | |
"this is tab3 page" | |
) | |
return(tab_page) | |
} |
This file contains hidden or 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
# 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