Last active
August 29, 2015 14:11
-
-
Save geotheory/db479811c6237a0741fe to your computer and use it in GitHub Desktop.
R Shiny stability testing
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
require(shiny) | |
# inbuilt dataset | |
diamonds = ggplot2::diamonds[,c(1,5,7)] | |
# csv datasets to input via front-end | |
for(i in 1:3){ | |
dat <- diamonds[sample(1:nrow(diamonds), 200),] | |
write.table(dat, paste0('dat',i,'.csv'), sep=',',row.names=F, col.names=T) | |
} | |
diamonds = diamonds[sample(1:nrow(diamonds),200),] | |
# global variables | |
inbuilt = FALSE # whether currently using inbuilt data or not | |
datapath = '' # to chech current against previous to see if new dataset input | |
# secondary plot functions | |
emptyplot <- function() plot(0, xlim=c(0,100), ylim=c(0,100), col=NA, axes=FALSE, xlab=NA, ylab=NA) | |
welcome <- function(){ | |
emptyplot() | |
text(10,80,"Please input CSV file data with 3 numerical columns", cex=2, pos=4) | |
text(10,65,"Use the inbuilt dataset and the csv files in the app folder..", cex=1.5, pos=4) | |
text(10,50,"check app's reliability and how often commands fail", cex=1.5, pos=4) | |
text(10,35,"output to PDF", cex=1.5, pos=4) | |
text(10,20,"how stable is the app for you?", cex=1.5, pos=4) | |
} | |
shinyServer(function(input, output, session) { | |
# REACTIVE FUNCTION | |
plotInput = reactive({ | |
# import data from inbuilt (internal) or a user-input CSV | |
# first must check if reactive is triggered by new data or not: | |
newdata = FALSE # initialise variables | |
pagereset = FALSE | |
if(input$inbuilt != inbuilt){ # inbuilt data option toggled | |
if(input$inbuilt) { # inbuilt selected | |
inbuilt <<- TRUE # update global | |
d <<- diamonds | |
newdata = TRUE | |
} else{ # inbuilt de-selected. | |
inbuilt <<- FALSE # update global | |
d <<- NULL # return splashscreen | |
pagereset = TRUE # would now crash so refresh app instead | |
} | |
} else { # input doesn't relate to inbuilt dataset | |
if(!input$inbuilt){ # inbuilt unselected | |
if(is.null(input$file1)) { # if null no input received yet | |
d = NULL # so reactive will return splash-screen | |
} else { # data has been input before | |
if(input$file1$datapath != datapath){ # new dataset just received | |
datapath <<- input$file1$datapath # update global | |
d <<- read.csv(datapath, header=TRUE, sep = ',') # update global | |
newdata = TRUE | |
#Sys.sleep(2) # allow file-upload aanimation to finish | |
# reset file handler in page | |
session$sendCustomMessage(type = "resetFileInputHandler", "file1") | |
} else NULL # new input not dataset-related | |
} | |
} | |
} | |
# reset/null javascript command - to reset app after inbuilt | |
# dataset is de-selected, as the script crashes otherwise.. | |
reset_js = ifelse(pagereset, "window.location.reload()", '') | |
reset_js = paste("<script>", reset_js,";</script>") | |
if(pagereset) { | |
plot(0, xlim=c(0,100), ylim=c(0,100), col=NA, axes=FALSE, xlab=NA, ylab=NA) | |
return(reset_js) # reset instruction | |
} | |
# no data input so return splash-screen | |
if(is.null(d)) { | |
welcome() | |
return(reset_js) | |
} | |
# NORMAL PLOT | |
# # stroke around polygons | |
if(input$border != 'none') border = input$border else border = NA | |
# PDF handling (save file locally to be passed forward) | |
if(input$returnpdf){ | |
pdf("plot.pdf", width=as.numeric(input$w), height=as.numeric(input$h)) | |
symbols(d[[1]], d[[2]], circles=sqrt(d[[3]]), inches=as.numeric(input$inches), | |
bg='#ff000020', fg=border) | |
dev.off() | |
} | |
# run plot | |
symbols(d$carat, d$depth, circles=sqrt(d$price), inches=as.numeric(input$inches), | |
bg='#ff000020', fg=border) | |
return(reset_js) | |
}) # end reactive | |
# OUTPUT ELEMENTS | |
# PDF file | |
output$pdflink = downloadHandler( | |
filename = "shiny_plot.pdf", # default browser save filename | |
content = function(file) file.copy("plot.pdf", file) # call pre-saved pdf | |
) | |
# plot | |
output$plot = renderPlot({ plotInput() }) | |
# reset instruction | |
output$reset = renderText({ plotInput() }) | |
}) |
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
require(shiny) | |
fluidPage( | |
titlePanel("Stability testing"), | |
sidebarLayout( | |
sidebarPanel( | |
# this css just resets the CSV upload function | |
tags$head( | |
tags$style(type="text/css", ".shiny-bound-input { width: 40px; }"), | |
tags$script(' | |
Shiny.addCustomMessageHandler("resetFileInputHandler", function(x) { | |
var id = "#" + x + "_progress"; | |
var idBar = id + " .bar"; | |
$(id).css("visibility", "hidden"); | |
$(idBar).css("width", "0%"); | |
}); | |
') | |
), | |
# inputs | |
h4('Input options'), | |
p("Chose inbuilt dataset or upload a CSV:"), | |
checkboxInput('inbuilt', 'Inbuilt dataset (app resets when de-selected)', FALSE), | |
fileInput('file1', '', accept = 'text/comma-separated-values'), | |
# plot layout | |
h4('Plot options'), | |
selectInput(inputId="border", label="Outline colour:", choices=list(black='black', white='white', none='none'), width=150, selected='black'), | |
br(), | |
sliderInput(inputId="inches", label = "Circle size (higher values can crash the app)", min=0.05, max=.5, value=.2, width=150), | |
# PDF output | |
br(), | |
h4('PDF output'), | |
p("Buggy: plot disappears, but link still downloads last plot. Sometimes after download app crashes"), | |
checkboxInput('returnpdf', 'Save plot to PDF?', FALSE), | |
strong("PDF size (inches):"), | |
textInput(inputId = "w", label = "width: ", value = "12"), | |
textInput(inputId = "h", label = "height: ", value = "9"), | |
br(), | |
downloadButton('pdflink', label = "Download") | |
), | |
mainPanel( | |
htmlOutput('reset'), # reset command (when inbuild dataset de-selected) | |
imageOutput('plot') | |
) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment