Created
September 13, 2013 11:26
-
-
Save stla/6549481 to your computer and use it in GitHub Desktop.
Series of tabs with Shiny - test 7
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
| ## generates two datasets for illustration | |
| I <- 3 # nb tests | |
| J <- 4 # nb timepoints | |
| dat1 <- data.frame( | |
| Test=gl(I,J,labels=LETTERS[1:I]), | |
| timepoint=rep(1:J,I) | |
| ) | |
| dat1 <- transform(dat1, y=round(rnorm(I*J,2*timepoint),1)) | |
| I <- 5 # nb tests | |
| J <- 3 # nb timepoints | |
| dat2 <- data.frame( | |
| Test=gl(I,J,labels=LETTERS[1:I]), | |
| timepoint=rep(1:J,I) | |
| ) | |
| dat2 <- transform(dat2, y=round(rnorm(I*J,2*timepoint),1)) |
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
| # setwd("~/Work/RD/MacroStab") | |
| library(ggplot2) | |
| #### | |
| #### Server | |
| #### | |
| shinyServer(function(input, output, session) { | |
| ## | |
| ## the two available datasets | |
| ## | |
| output$dat1 <- renderTable({ dat1 }) | |
| output$dat2 <- renderTable({ dat2 }) | |
| ## | |
| ## get the selected dataset | |
| ## | |
| datGet <- reactive({ | |
| if (input$datatest == "0") return(NULL) | |
| if (input$datatest == "1") return(dat1) | |
| if (input$datatest == "2") return(dat2) | |
| }) | |
| ## | |
| ## Preliminary objects | |
| ## | |
| pObjects <- reactive({ | |
| dat <- datGet() | |
| if (is.null(dat)) return(NULL) | |
| Levels <- levels(dat$Test) | |
| J <- length(Levels) | |
| Tabnames <- paste0("Test ", Levels) | |
| tableNodes <- paste0("tnode", LETTERS[1:J]) # output nodes names for tables rendering | |
| plotNodes <- paste0("pnode", LETTERS[1:J]) # output nodes names for plots rendering | |
| data.frame(Levels=Levels, Tabnames=Tabnames, tableNodes=tableNodes, plotNodes=plotNodes)#, Selecteds) | |
| }) | |
| outputNodes <- reactive({ # output nodes for plots rendering | |
| pobjects <- pObjects() | |
| if (is.null(pobjects)) return(NULL) | |
| list(tnodes=paste0("tnode", LETTERS[1:nrow(pobjects)]), | |
| pnodes=paste0("pnode", LETTERS[1:nrow(pobjects)])) | |
| }) | |
| Selecteds <- reactive({ # (just for trying) return the values selected in the tabs | |
| dat <- datGet() | |
| if (is.null(dat)) return(NULL) | |
| J <- length(levels(dat$Test)) | |
| selecteds <- rep(NA, J) | |
| for(i in 1:J){ | |
| selecteds[i] <- input[[paste0("sel",i)]] | |
| } | |
| selecteds | |
| }) | |
| ## | |
| ## make the UI in each tab - TRICK: use input$tab0 as the current counter, not i ! | |
| ## | |
| observe({ | |
| pobjects <- pObjects() | |
| if (!is.null(pobjects)) { | |
| outnodes <- outputNodes() | |
| tnodes <- outnodes$tnodes | |
| pnodes <- outnodes$pnodes | |
| tests <- pobjects$Levels | |
| J <- length(tests) | |
| dat <- datGet() | |
| output$dataplot <- renderPlot({ | |
| gg <- ggplot(dat, aes(x=timepoint, y=y)) + | |
| geom_point() + | |
| geom_smooth(method=lm, se=FALSE, size=1, linetype="twodash") + | |
| facet_grid(Test~.) + | |
| ylab("result") | |
| print(gg) | |
| }, width=500, height=900) | |
| for(i in 1:J){ | |
| I <- input$tab0 | |
| if(I==i){ | |
| test <- tests[as.numeric(I)] | |
| dd <- droplevels(subset(dat, subset= Test== test)) | |
| output[[tnodes[i]]] <- renderTable({ # table in each tab | |
| dd | |
| }) | |
| output[[pnodes[i]]] <- renderPlot({ # plot in each tab | |
| plot(dd$timepoint, dd$y) | |
| }, width=600, height=300) | |
| } | |
| } | |
| output$selections <- renderTable({ # to display in the "Summary" tab | |
| data.frame(tab=pobjects$Tabnames, selected=Selecteds()) | |
| }) | |
| } | |
| }) | |
| ## | |
| ## make the tabs | |
| ## | |
| output$twotabs <- renderUI({ | |
| tabs <- list(NULL) | |
| tabs[[1]] <- tabPanel("Data", #conditionalPanel( | |
| #condition = "input.datatest == '0'", | |
| h2("Choose a test dataset"), | |
| h3("one tab will be generated for each level of the Test column"), | |
| withTags(div(class='row-fluid', | |
| div(class='span4', h3("Data test 1:"), tableOutput("dat1")), | |
| div(class='span4', h3("Data test 2:"), tableOutput("dat2")) | |
| )), | |
| value="0") | |
| # | |
| pobjects <- pObjects() | |
| if (!is.null(pobjects)) { | |
| outnodes <- outputNodes() | |
| tnodes <- outnodes$tnodes | |
| pnodes <- outnodes$pnodes | |
| tabnames <- pobjects$Tabnames | |
| J <- length(tabnames) | |
| tabs[[1]] <- tabPanel("Data", | |
| h3("Overview of Data"), | |
| h3("Click on the tabs to run the analysis for each test"), | |
| h3("When done, click on the Summary tab to check and generate a report"), | |
| plotOutput("dataplot"), | |
| value="firsttab") | |
| for(i in 1:J){ | |
| tabs[[i+1]] <- tabPanel(tabnames[i], | |
| h3(tabnames[i]), | |
| selectInput(paste0("sel",i), "Select", choices=as.character(1:3), selected="1"), | |
| tableOutput(tnodes[i]), | |
| plotOutput(pnodes[i]), | |
| value=i) | |
| } | |
| tabs[[J+2]] <- tabPanel("Summary", | |
| h3("Your selections:"), | |
| tableOutput("selections"), | |
| value="summarytab") | |
| } | |
| tabs$id <- "tab0" | |
| do.call(tabsetPanel, tabs) | |
| }) | |
| # | |
| }) | |
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
| shinyUI(pageWithSidebar( | |
| headerPanel("Generating an arbitrary number of tabs - assay 7", | |
| tags$head( | |
| tags$img(src="http://upload.wikimedia.org/wikipedia/commons/thumb/c/c1/Rlogo.png/200px-Rlogo.png", height="200px") | |
| )), | |
| ## | |
| ## sidebar panel | |
| ## | |
| sidebarPanel( | |
| selectInput("datatest", "Select a dataset", choices=c(none=0, test1=1, test2=2), selected=0) | |
| ), | |
| ## | |
| ## main panel | |
| ## | |
| mainPanel( | |
| uiOutput("twotabs") | |
| #) | |
| ) | |
| )) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment