Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- plot_1 <- function(var = input$select) {
- gg1 <- aggregate(cbind(get(var)) ~ Mi + hours , a(), FUN=mean)
- ggl <- aggregate(cbind(get(var)) ~ Mi + hours , b(), FUN=mean)
- names(gg1)[2] <- "Day"
- names(ggl)[2] <- "Day"
- names(gg1)[3] <- var
- names(ggl)[3] <- var
- gg1$Day <- gg1$Day /24
- ggl$Day <- ggl$Day /24
- print(ggplot(subset(gg1,!is.na(var)), aes_string(x = "Day", y = var, group = "Mi")) +
- geom_point(aes(color = factor(Mi)),size = 3, alpha = 0.7) +
- #scale_x_continuous(breaks=pretty_breaks(n=10)) + #geom_smooth(stat= "smooth" , alpha = I(0.4), method="loess",color="grey", formula = y ~ x)
- geom_line(data = (ggl), size = 1) +
- scale_color_manual("Mesocosm", values = c('#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#FF00FF', '#808080', '#800000' , '#008000', '#008080')) +
- scale_y_continuous(breaks=pretty_breaks(n=10)) +
- guides(colour = guide_legend(override.aes = list(size=8))) +
- theme_bw() +
- theme (legend.position = "right", legend.title=element_text(size=14),legend.key.width = unit(5, "line"),
- legend.key.size = unit(0.2, "in"), legend.text = element_text(size=14),
- panel.border = element_rect(colour = "black"),strip.background = element_rect(fill="#CCCCFF"),
- strip.text.x = element_text(size=14, face="bold"),axis.text.y = element_text(colour="grey20",size=13,face="bold"),
- axis.text.x = element_text(colour="grey20",size=13,face="bold"),
- axis.title.x = element_text(colour="grey20",size=20,face="bold"),
- axis.title.y = element_text(colour="grey20",size=20,face="bold")) +
- scale_x_continuous(breaks=pretty_breaks(n=10), limits=c(input$slider[1],input$slider[2])) )
- }
- library(shiny)
- pkgs <- c("reshape2","raster","maps","maptools", "ggplot2", "scales", "grid")
- pkgs <- pkgs[!(pkgs %in% installed.packages()[,"Package"])]
- if(length(pkgs)) install.packages(pkgs,repos="http://cran.cs.wwu.edu/")
- library(reshape2); library(raster); library(maptools) ; library(ggplot2) ; library(scales) ; library(grid)
- # By default, the file size limit is 5MB. It can be changed by
- # setting this option. Here we'll raise limit to 9MB.
- options(shiny.maxRequestSize = 10*1024^2)
- #Main Shinyserver function
- shinyServer(function(input, output) {
- #Plot
- output$showMapPlot <- renderUI({
- { list(plotOutput("plot",height="100%"), br()) }
- })
- #Change the selectInput based on the radioButton option
- output$select <- renderUI({
- if (input$radio == 1 | input$radio == 2) {selectInput("select", h4("Variables:"), choices=var.name, selected=var.name[1], multiple=F, width="100%")}
- else { selectInput("select", h4("Variables:"), choices=var.name, selected=var.name[1], multiple=T, width="100%")}
- })
- output$experiment <- renderUI({
- if(input$radio == 4) {selectInput("experiment", h4("Experiment:"), choices=unique(a()$Ei), selected=NULL)}
- })
- #Upload Model Data
- a <- reactive({
- fileinput1 <- input$file1
- if (is.null(fileinput1))
- return(NULL)
- read.table(fileinput1$datapath, header = TRUE, col.names = c("Ei","Mi","hours","Nphy","Cphy","CHLphy","Nhet","Chet","Ndet","Cdet","DON","DOC","DIN","DIC","AT","dCCHO","TEPC","Ncocco","Ccocco","CHLcocco","PICcocco","par","Temp","Sal","co2atm","u10","dicfl","co2ppm","co2mol","pH"))
- })
- #Upload Observation Data
- b <- reactive({
- fileinput2 <- input$file2
- if (is.null(fileinput2))
- return(NULL)
- read.table(fileinput2$datapath, header = TRUE, col.names = c("Ei","Mi","hours","Nphy","Cphy","CHLphy","Nhet","Chet","Ndet","Cdet","DON","DOC","DIN","DIC","AT","dCCHO","TEPC","Ncocco","Ccocco","CHLcocco","PICcocco","par","Temp","Sal","co2atm","u10","dicfl","co2ppm","co2mol","pH"))
- })
- # Based on the selected radio button (Option) and the input variables (SelectInput) Do the plotting.
- # Option --> radioButton --> Function to apply
- # Variable --> SelectInput --> Variables to pass into functions
- #########################################Plot Code############################################
- #Plot conditions and Downlaod graph implementation
- output$plot <- renderPlot({
- if(input$radio == "1")
- {
- plot_1()
- output$Plotoutput <- downloadHandler(
- filename = 'curPlot.pdf',
- content = function(file){
- pdf(file = file, width=11, height=8.5, pointsize=8)
- plot_1()
- dev.off()
- }
- )
- }
- if(input$radio == "2")
- {
- plot_2()
- output$Plotoutput <- downloadHandler(
- filename = 'curPlot.pdf',
- content = function(file){
- pdf(file = file, width=11, height=8.5, pointsize=8)
- plot_2()
- dev.off()
- }
- )
- }
- if(input$radio == "3")
- {
- if(length(input$select) >= 2)
- {
- plot_3()
- output$Plotoutput <- downloadHandler(
- filename = 'curPlot.pdf',
- content = function(file){
- pdf(file = file, width=11, height=8.5, pointsize=8)
- plot_3()
- dev.off()
- }
- )
- }
- }
- if(input$radio == "4")
- {
- if(length(input$select) >= 6)
- {
- plot_4()
- output$Plotoutput <- downloadHandler(
- filename = 'curPlot.pdf',
- content = function(file){
- pdf(file = file, width=11, height=8.5, pointsize=8)
- plot_4()
- dev.off()
- }
- )
- }
- }
- },
- height=700, width=1100
- )
- ################# Option-1 ###################
- plot_1 <- function(var = input$select) {
- gg1 <- aggregate(cbind(get(var)) ~ Mi + hours , a(), FUN=mean)
- ggl <- aggregate(cbind(get(var)) ~ Mi + hours , b(), FUN=mean)
- names(gg1)[2] <- "Day"
- names(ggl)[2] <- "Day"
- names(gg1)[3] <- var
- names(ggl)[3] <- var
- gg1$Day <- gg1$Day /24
- ggl$Day <- ggl$Day /24
- print(ggplot(subset(gg1,!is.na(var)), aes_string(x = "Day", y = var, group = "Mi")) +
- geom_point(aes(color = factor(Mi)),size = 3, alpha = 0.7) +
- #scale_x_continuous(breaks=pretty_breaks(n=10)) + #geom_smooth(stat= "smooth" , alpha = I(0.4), method="loess",color="grey", formula = y ~ x)
- geom_line(data = (ggl), size = 1) +
- scale_color_manual("Mesocosm", values = c('#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#FF00FF', '#808080', '#800000' , '#008000', '#008080')) +
- scale_y_continuous(breaks=pretty_breaks(n=10)) +
- guides(colour = guide_legend(override.aes = list(size=8))) +
- theme_bw() +
- theme (legend.position = "right", legend.title=element_text(size=14),legend.key.width = unit(5, "line"),
- legend.key.size = unit(0.2, "in"), legend.text = element_text(size=14),
- panel.border = element_rect(colour = "black"),strip.background = element_rect(fill="#CCCCFF"),
- strip.text.x = element_text(size=14, face="bold"),axis.text.y = element_text(colour="grey20",size=13,face="bold"),
- axis.text.x = element_text(colour="grey20",size=13,face="bold"),
- axis.title.x = element_text(colour="grey20",size=20,face="bold"),
- axis.title.y = element_text(colour="grey20",size=20,face="bold")) +
- scale_x_continuous(breaks=pretty_breaks(n=10), limits=c(input$slider[1],input$slider[2])) )
- }
- ################ END of Option-1 ###############
- ################ Option-2 ######################
- plot_2 <- function(var = input$select) {
- #EXP <- unique(a()$Ei)
- gg2 <- aggregate(cbind(get(var)) ~ Ei + Mi + hours, a(), FUN=mean)
- ggl <- aggregate(cbind(get(var)) ~ Ei + Mi + hours, b(), FUN=mean)
- #gg2 <- subset(aggr,aggr$Ei == c(EXP))
- names(gg2)[3] <- "Day"
- names(ggl)[3] <- "Day"
- names(gg2)[4] <- var
- names(ggl)[4] <- var
- gg2$Day <- gg2$Day /24
- ggl$Day <- ggl$Day /24
- print( ggplot(subset(gg2,!is.na(var)),aes_string(x="Day", y= var)) +
- geom_point(aes(color = factor(Mi)),size = 3) +
- geom_line(data = (ggl), size = 1) +
- #geom_smooth(color="blue",stat= "smooth" , alpha = I(0.01), method="loess") +
- #coord_fixed(ratio=1) +
- facet_grid(Ei~., labeller=function(x,y) (paste0("EXP",y))) +
- scale_color_manual("Mesocosm", values = c('#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#FF00FF', '#808080', '#800000' , '#008000', '#008080'), breaks=pretty_breaks(n=8) ) +
- scale_y_continuous(breaks=pretty_breaks(n=10)) +
- guides(colour = guide_legend(override.aes = list(size=8))) +
- theme_bw() +
- theme (legend.position = "right", legend.title=element_text(size=14),legend.key.width = unit(5, "line"),
- legend.key.size = unit(0.2, "in"), legend.text = element_text(size=14),
- panel.border = element_rect(colour = "black"),strip.background = element_rect(fill="#CCCCFF"),
- strip.text.x = element_text(size=14, face="bold"),
- strip.text.y = element_text(size=14, face="bold"),
- axis.text.y = element_text(colour="grey20",size=13,face="bold"),
- axis.text.x = element_text(colour="grey20",size=13,face="bold"),
- axis.title.x = element_text(colour="grey20",size=20,face="bold"),
- axis.title.y = element_text(colour="grey20",size=20,face="bold")) +
- scale_x_continuous(breaks=pretty_breaks(n=10), limits=c(input$slider[1],input$slider[2])) )
- }
- ################ END of Option-2 ###############
- ############## Option-3 ########################
- plot_3 <- function(var1 = input$select[1],var2 = input$select[2]) {
- gg3 <- aggregate(cbind(get(var1),get(var2))~Mi+hours,a(), FUN=mean)
- names(gg3)[2] <- "Day"
- names(gg3)[3] <- var1
- names(gg3)[4] <- var2
- gg3$Day <- gg3$Day/24
- plot(x = gg3[,"Day"], y = gg3[,var1], type="p", col="red", xlab="", ylab="", xlim=c(input$slider[1],input$slider[2]))
- par(new=TRUE)
- plot(x = gg3[,"Day"], y = gg3[,var2], type="p", col="blue", xaxt="n", yaxt="n", xlab="", ylab="", xlim=c(input$slider[1],input$slider[2]))
- axis(4)
- mtext(var1,side=2,line=2)
- mtext(var2,side=4,line=-2)
- mtext("Day",side=1,line=2)
- legend("topleft",col=c("red","blue"),lty=1,legend=c(var1,var2), text.width = 3)
- }
- ################ END of Option-3 ################
- #################### Option-4 #####################
- plot_4 <- function(var1 = input$select[1], var2 = input$select[2], var3 = input$select[3], var4 = input$select[4], var5 = input$select[5], var6 = input$select[6]) {
- myvars <- c(var1,var2,var3,var4,var5,var6)
- gg4 <- subset((aggregate(cbind(get(var1),get(var2),get(var3),get(var4),get(var5),get(var6))~Ei+Mi+hours,a(), FUN=mean)),Ei == input$experiment)
- ggl <- subset((aggregate(cbind(get(var1),get(var2),get(var3),get(var4),get(var5),get(var6))~Ei+Mi+hours,b(), FUN=mean)),Ei == input$experiment)
- names(gg4)[4] <- var1
- names(ggl)[4] <- var1
- names(gg4)[5] <- var2
- names(ggl)[5] <- var2
- names(gg4)[6] <- var3
- names(ggl)[6] <- var3
- names(gg4)[7] <- var4
- names(ggl)[7] <- var4
- names(gg4)[8] <- var5
- names(ggl)[8] <- var5
- names(gg4)[9] <- var6
- names(ggl)[9] <- var6
- names(gg4)[3] <- "Day"
- names(ggl)[3] <- "Day"
- gg4$Day <- gg4$Day /24
- ggl$Day <- ggl$Day /24
- ddp <- melt(gg4,id.vars=c("Ei","Mi","Day"), measure.vars=myvars, na.rm = TRUE)
- ddl <- melt(ggl,id.vars=c("Ei","Mi","Day"), measure.vars=myvars, na.rm = TRUE)
- print(ggplot(subset(ddp,!is.na(myvars)),aes(x=Day, y=value)) +
- geom_point(aes(color=factor(Mi)), size = 3, position = position_jitter(width = 0.1)) +
- geom_line(data = (ddl), size = 1) +
- #geom_smooth(stat= "smooth" , alpha = I(0.01), method="loess", color = "blue") +
- facet_wrap(~variable, nrow=3, ncol=2,scales = "free_y") +
- scale_color_manual("Mesocosm", values = c('#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#FF00FF', '#808080', '#800000' , '#008000', '#008080')) +
- scale_y_continuous(breaks=pretty_breaks(n=6)) +
- guides(colour = guide_legend(override.aes = list(size=8))) +
- theme_bw() +
- theme (legend.position = "right", legend.title=element_text(size=14),legend.key.width = unit(5, "line"),
- legend.key.size = unit(0.2, "in"), legend.text = element_text(size=14),
- panel.border = element_rect(colour = "black"),strip.background = element_rect(fill="#CCCCFF"),
- strip.text.x = element_text(size=14, face="bold"),
- axis.text.y = element_text(colour="grey20",size=13,face="bold"),
- axis.text.x = element_text(colour="grey20",size=13,face="bold"),
- axis.title.x = element_text(colour="grey20",size=20,face="bold"),
- axis.title.y = element_text(colour="grey20",size=20,face="bold")) +
- scale_x_continuous(breaks=pretty_breaks(n=6), limits=c(input$slider[1],input$slider[2])) )
- }
- ################ END of Option-4 ################
- })
- ###################################### END of Program##########################################
- library(shiny)
- tabPanelAbout <- source("about.r")$value
- headerPanel_2 <- function(title, h, windowTitle=title) {
- tagList(
- tags$head(tags$title(windowTitle)),
- h(title)
- )
- }
- shinyUI(fluidPage(
- headerPanel_2(
- HTML(
- '<div id="stats_header" align ="center">
- <font color="grey">
- <body style="background-color:black;">
- <h2 style="font-family:georgia;">
- BIOCHEMICAL MODELLING
- </body>
- </font>
- </div>'
- ), h3, "BIOCHEMICAL MODELLING"
- ),
- fluidRow(column(3,
- uiOutput("showMapPlot"),
- wellPanel(
- h4("Data Upload"),
- fileInput('file1', h5('Choose Your Model Data'), accept=c('text/csv','text/comma-separated-values,text/plain','.OUT')),
- fileInput('file2', h5('Choose Your Observation Data'), accept=c('text/csv','text/comma-separated-values,text/plain','.OUT'))
- ),
- wellPanel(
- sliderInput("slider", label = h4("Time (Days)"), min = 0, max = 552/24, value = c(0,20),animate = FALSE, width="100%", format = "#")
- ),
- wellPanel(
- radioButtons("radio", label = h4("Plot Options:"),
- choices = list("Choice 1 [Single var, all Exp and all Mesc]" = 1,
- "Choice 2 [Single var, compare Exp for all Mesc]" = 2,
- "Choise 3 [Two var, all Exp and all Mesc, line plot comparison]" = 3,
- "Choice 4 [Six variable comparison for all exp and all Mesc]"= 4),selected = 1)),
- wellPanel(
- div(class="row-fluid",
- div(class="span6", uiOutput("select"))
- )),
- wellPanel(div(class="row-fluid", div(class="span6", downloadButton("Plotoutput", "Download Graphic"))))
- ),
- #Conditional Plots tab creation
- column(8,
- tabsetPanel(
- tabPanel("Conditional Plots",plotOutput("plot",height="auto"),value="barplots"),
- tabPanelAbout(),
- id="tsp")),
- #Helps to suppress the unwanted red Error messages on the Browser
- tags$style(type="text/css",
- ".shiny-output-error { visibility: hidden; }",
- ".shiny-output-error:before { visibility: hidden; }"
- )
- )))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement