Created
October 22, 2013 22:23
-
-
Save stla/7109236 to your computer and use it in GitHub Desktop.
test datatables shiny_v8 and rCharts
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
| # test whether rCharts is installed | |
| rcharts <- require(rCharts) | |
| # test shiny version | |
| shinyv8 <- packageVersion("shiny") == '0.7.0.99' | |
| # auxiliary function to extract numbers from a character string | |
| numextract <- function(string){ # http://stackoverflow.com/questions/19252663/extracting-decimal-numbers-from-a-string | |
| str_extract(string, "\\d+\\.*\\d*") | |
| } |
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
| ## required packages ## | |
| library(stringr) | |
| library(XLConnect) | |
| require(rCharts) | |
| library(data.table) | |
| library(ascii) | |
| shinyServer(function(input, output) { | |
| ################################# | |
| ##### LOAD EXCEL DATA DILE ###### | |
| ################################# | |
| fileInput <- reactive({ | |
| if (is.null(input$file)) { | |
| # User has not uploaded a file yet | |
| return(NULL) | |
| } | |
| input$file | |
| }) | |
| XLfile <- reactive({ | |
| inFile <- fileInput() | |
| if (is.null(inFile)) { | |
| # User has not uploaded a file yet | |
| return(NULL) | |
| } | |
| loadWorkbook(inFile$datapath) | |
| }) | |
| output$Sheet <- renderUI({ | |
| if (is.null(input$file)) { | |
| # User has not uploaded a file yet | |
| return(NULL) | |
| } | |
| sheets <- getSheets(XLfile()) | |
| list( | |
| h4("Select sheet:"), | |
| selectInput(inputId = 'Sheet', | |
| label = "Select sheet:", | |
| choices = sheets), | |
| actionButton("goButton", "Go !") | |
| ) | |
| }) | |
| fileLoaded <- reactive({ | |
| if(length(input$goButton)!=0){ | |
| return(ifelse(input$goButton != 0, "ok", "no")) | |
| }else return("no") | |
| }) | |
| Dataset <- reactive({ | |
| if(fileLoaded()=="ok") return(readWorksheet(XLfile(), sheet = input$Sheet, colTypes="character")) | |
| return(NULL) | |
| }) | |
| output$str <- renderPrint({ | |
| dataset <- Dataset() | |
| str(dataset) | |
| }) | |
| output$filetable <- renderTable({ | |
| fileInput() | |
| }) | |
| output$jstableUIcheck <- renderUI({ | |
| if(fileLoaded() != "ok" | !rcharts) return(NULL) | |
| checkboxInput("jstablecheck", "try renderChart2 (rCharts package)") | |
| }) | |
| #################################### | |
| ###### SELECT COLUMNS ####### | |
| #################################### | |
| output$columns <- renderUI({ | |
| if(fileLoaded() != "ok") return(NULL) | |
| dat <- Dataset() | |
| cols <- names(dat) | |
| ok <- tolower(cols) %in% c("day") | |
| day.selected <- if(any(ok)) cols[which(ok)[1]] else NULL | |
| ok <- tolower(cols) %in% c("operator", "tech", "technician") | |
| operator.selected <- if(any(ok)) cols[which(ok)[1]] else NULL | |
| ok <- tolower(cols) %in% c("sample") | |
| sample.selected <- if(any(ok)) cols[which(ok)[1]] else NULL | |
| ok <- tolower(cols) %in% c("titre", "titer", "conc", "concentration") | |
| titre.selected <- if(any(ok)) cols[which(ok)[1]] else NULL | |
| list( | |
| h4("Select columns:"), | |
| selectInput(inputId = 'col.titre', | |
| label = "Titre:", | |
| choices = cols, | |
| selected = titre.selected), | |
| selectInput(inputId = 'col.day', | |
| label = "Day:", | |
| choices = cols, | |
| selected = day.selected), | |
| selectInput(inputId = 'col.operator', | |
| label = "Operator:", | |
| choices = cols, | |
| selected = operator.selected), | |
| selectInput(inputId = 'col.sample', | |
| label = "Sample:", | |
| choices = cols, | |
| selected = sample.selected) | |
| ) | |
| }) | |
| #################################### | |
| ##### MAKE WORKING DATASET ####### | |
| #################################### | |
| wdatGet <- reactive({ | |
| if(fileLoaded() != "ok") return(NULL) | |
| dat0 <- Dataset() | |
| col.day <- input$col.day | |
| col.operator <- input$col.operator | |
| col.sample <- input$col.sample | |
| col.titre <- input$col.titre | |
| wdat <- subset(dat0, select=c(col.day,col.operator,col.sample,col.titre)) | |
| names(wdat) <- c("Day","Operator","Sample","Titre") | |
| x <- wdat$Sample | |
| isnum <- all(numextract(x) == x) | |
| if(!is.na(isnum)) if(isnum) wdat$Sample <- as.numeric(x) | |
| wdat <- transform(wdat, | |
| Day=factor(Day), Operator=factor(Operator), Sample=factor(Sample), | |
| Titre=as.numeric(numextract(Titre)) | |
| ) | |
| wdat <- droplevels(subset(wdat, subset=!is.na(Titre))) | |
| wdat$Run <- wdat$Day:wdat$Operator | |
| wdat | |
| }) | |
| #################################### | |
| ### WORKING DATASET TABLE ### | |
| #################################### | |
| output$ordinaryTable <- renderTable({ # embedded in wdatTableUI | |
| if(fileLoaded() != "ok") return(NULL) | |
| wdatGet() | |
| }) | |
| output$wdatSummary <- renderPrint({ | |
| if(length(input$col.titre)==0) return(NULL) | |
| wdat <- wdatGet() | |
| print(ascii(summary(wdat), include.rownames=FALSE), type="rest") | |
| }) | |
| observe({ # make the rCharts table | |
| if(fileLoaded() != "ok" | !rcharts | length(input$jstablecheck)==0) return(NULL) | |
| if(!input$jstablecheck) return(NULL) | |
| output$JStable <- renderChart2({ | |
| if(!rcharts) return(NULL) | |
| dTable(data.table(wdatGet()), sPaginationType = "full_numbers") | |
| }) | |
| }) | |
| observe({ # make the DataTables table | |
| if(fileLoaded() != "ok" | !shinyv8 | length(input$jstablecheck)==0) return(NULL) | |
| if(input$jstablecheck) return(NULL) | |
| output$Dtable <- renderDataTable({ | |
| wdatGet() | |
| }) | |
| }) | |
| output$wdatTableUI <- renderUI({ | |
| if(fileLoaded() != "ok") return(NULL) | |
| no.rCharts <- function(){ | |
| if(shinyv8) return(dataTableOutput("Dtable")) | |
| return(tableOutput("ordinaryTable")) | |
| } | |
| if(!rcharts) return(no.rCharts()) | |
| if(rcharts & length(input$jstablecheck)!=0){ | |
| if(!input$jstablecheck) no.rCharts() else chartOutput("JStable", "datatables") | |
| }else{return(NULL)} | |
| }) | |
| }) |
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("test renderChart2 and renderDataTable"), | |
| sidebarPanel( | |
| fileInput("file", "File data", multiple=FALSE), | |
| br(), | |
| conditionalPanel(condition= "input.tabset=='Data import'", | |
| uiOutput("Sheet")), | |
| br(), | |
| uiOutput("columns") | |
| ), | |
| mainPanel( | |
| tabsetPanel( | |
| tabPanel("Data import", | |
| h4("Uploaded file:"), | |
| tableOutput("filetable"), | |
| h4("Raw data summary:"), | |
| verbatimTextOutput("str"), | |
| br(), | |
| h4("Working dataset :"), | |
| tags$div(class='row-fluid', | |
| div(class='span6', | |
| uiOutput("jstableUIcheck"), | |
| uiOutput("wdatTableUI") | |
| ), | |
| div(class='span6', | |
| verbatimTextOutput("wdatSummary") | |
| ) | |
| ) | |
| ), id="tabset" | |
| ) | |
| ))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment