From 5dc677a3375f71293b779c50feb82bc6918aeafb Mon Sep 17 00:00:00 2001 From: ayanamartins Date: Sun, 12 Jul 2015 11:06:24 -0300 Subject: [PATCH] Adds "Tutorial" tabs solving conflicts that resulted from the awesome modifications made by @andrechalom and @adalardo. --- help.html | 3 +- server.R | 224 +++++++++++++++++++++++++++++++++++++++++++++++++++++- ui.R | 126 +++++++++++++++++++++++++++++- 3 files changed, 350 insertions(+), 3 deletions(-) diff --git a/help.html b/help.html index eb62341..0c885ad 100644 --- a/help.html +++ b/help.html @@ -19,7 +19,8 @@

The idea behind this package is to help students to understand the logic behind these tests, with lots of flexibility on what is the statistic that will be calculated.

Rsampling and and this web interface were written by Andre Chalom, - Alexandre Adalardo and Paulo Prado.

+ Alexandre Adalardo, Ayana Martins + and Paulo Prado.

Please visit our project webpage and the Rsampling library page on github, diff --git a/server.R b/server.R index 67c975a..962c355 100644 --- a/server.R +++ b/server.R @@ -1,4 +1,5 @@ library(shiny) +library(gridExtra) library(Rsampling) shinyServer(function(input, output, session) { ########################################### @@ -242,5 +243,226 @@ shinyServer(function(input, output, session) { }) session$onSessionEnded(function() { run_iter$suspend() - }) + }) + ########################################### + ####### FUNCTIONS USED IN TUTORIAL ####### + ########################################### + ########################################################################### + values <- reactiveValues() + values$saveDist <- list() + observeEvent(input$clear1, { + isolate(values$saveDist <- list()) + }) + observeEvent(input$clear2, { + isolate(values$saveDist <- list()) + }) + observeEvent(input$clear3, { + isolate(values$saveDist <- list()) + }) + observeEvent(input$tabEvent, { + isolate(values$saveDist <- list()) + }) + ################################################################# + ###Mangrove trees + mangStat <- mean(rhyzophora[ which(rhyzophora$soil.instability=='high'),]$root)- + mean(rhyzophora[ which(rhyzophora$soil.instability=='medium'),]$root) + ### Specific plots for this dataset + output$mangPlot <- renderPlot({ + par(mar = c(4,4,2,2)) + boxplot(root ~ soil.instability, data=rhyzophora, type="n", + main="Original data", + xlab="Soil instability", ylab="area covered by aerial root (m2)") + text(2,33,paste("mean difference = \n",round(mangStat,4)),col="red") + }) + output$mangPlotRandom <- renderPlot({ + thisSet <- randomizedMang() + thisDf <- data.frame(matrix(unlist(thisSet), nrow=24)) + names(thisDf) <- c("si","canopy.trunk","root","n.roots") + thisStat <- mean(thisDf[ which(thisDf$si==1),]$root)- + mean(thisDf[ which(thisDf$si==2),]$root) + isolate(values$saveDist <- rbind(values$saveDist, thisStat)) + par(mar = c(4,4,2,2)) + boxplot(root ~ si, data=thisDf, type="n", + main="Randomized data", col="#428bca", + xlab="Soil instability", ylab="area covered by aerial root (m2)") + text(2,33,paste("mean difference = \n",round(thisStat,4))) + }) + ### Display data set as a plot + output$mangTable <- renderPlot({ + data <- rhyzophora[,c(1,3)] + data$root <- round(data$root,3) + tbl <- tableGrob(data, + cols = c("soil.instability", + "roots"), + show.rownames =FALSE, + gpar.corefill = gpar(fill = "grey80", col = "white")) + grid.draw(tbl) + },height=500) + output$mangTableRandom <- renderPlot({ + thisSet <- randomizedMang() + thisDf <- data.frame(matrix(unlist(thisSet), nrow=24)) + names(thisDf) <- c("si","canopy.trunk","root","n.roots") + thisDf$si[thisDf$si == 1] <- "high" + thisDf$si[thisDf$si == 2] <- "medium" + data <- thisDf[,c(1,3)] + data$root <- round(data$root,3) + tbl <- tableGrob(data, + cols = c("soil.instability", + "roots"), + show.rownames =FALSE, + gpar.corefill = gpar(fill = "#428bca", col = "white")) + grid.draw(tbl) + },height=500) + #Randomization output + randomizedMang <- reactive({ + input$mangRand # triggers the calculations when the "..." is pressed + Rsampling(type = "normal_rand", dataframe = rhyzophora, + statistics = function(dataframe) return(dataframe), cols = 3, + ntrials = 1) + }) + ### main plot of the program: generates a histogram of distribution() + output$distPlotMang <- renderPlot({ + par(mar = c(4,3,2,0)) + Rsampling::dplot(dist = as.numeric(values$saveDist), svalue = mangStat, + extreme = FALSE, vline = TRUE, rejection = FALSE, + breaks = seq(-15,15,2), xlim=c(-15,15)) + }) + ###################################################################### + ###More mangrove trees + rhyzSlope <- coef(lm(n.roots ~ canopy.trunk, data=rhyzophora))[2] + ### Specific plots for this dataset + output$rhyzPlot <- renderPlot({ + par(mar = c(4,4,2,2)) + plot(n.roots ~ canopy.trunk, data=rhyzophora, pch=19, + main="Original data", + xlab="canopy area / trunk area", ylab="number of roots") + abline(lm(n.roots ~ canopy.trunk, data=rhyzophora)) + text(3000,150,paste("slope =",round(rhyzSlope,4)),col="red") + }) + output$rhyzPlotRandom <- renderPlot({ + thisSet <- randomizedRhyz() + thisDf <- data.frame(matrix(unlist(thisSet), nrow=24)) + names(thisDf) <- c("si","canopy.trunk","root","n.roots") + thisSlope <- coef(lm(n.roots ~ canopy.trunk, data=thisDf))[2] + isolate(values$saveDist <- rbind(values$saveDist, thisSlope)) + par(mar = c(4,4,2,2)) + plot(n.roots ~ canopy.trunk, data=thisDf, pch=19, + main="Randomized data", col="#428bca", + xlab="canopy area / trunk area", ylab="number of roots") + abline(lm(n.roots ~ canopy.trunk, data=thisDf)) + text(3000,150,paste("slope =",round(thisSlope,4))) + }) + output$rhyzTable <- renderPlot({ + data <- rhyzophora[,c(2,4)] + data$canopy.trunk <- round(data$canopy.trunk,2) + tbl <- tableGrob(data, + cols = c("canopy.trunk", + "n.roots"), + show.rownames =FALSE, + gpar.corefill = gpar(fill = "grey80", col = "white")) + grid.draw(tbl) + },height=500) + output$rhyzTableRandom <- renderPlot({ + thisSet <- randomizedRhyz() + thisDf <- data.frame(matrix(unlist(thisSet), nrow=24)) + names(thisDf) <- c("si","canopy.trunk","root","n.roots") + data <- thisDf[,c(2,4)] + data$canopy.trunk <- round(data$canopy.trunk,2) + tbl <- tableGrob(data, + cols = c("canopy.trunk", + "n.roots"), + show.rownames =FALSE, + gpar.corefill = gpar(fill = "#428bca", col = "white")) + grid.draw(tbl) + },height=500) + #Randomization output + randomizedRhyz <- reactive({ + input$rhyzRand # triggers the calculations when the "..." is pressed + Rsampling(type = "normal_rand", dataframe = rhyzophora, + statistics = function(dataframe) return(dataframe), cols = 4, + ntrials = 1) + }) + ### main plot of the program: generates a histogram of distribution() + output$distPlotRhyz <- renderPlot({ + par(mar = c(4,3,2,0)) + Rsampling::dplot(dist = as.numeric(values$saveDist), svalue = rhyzSlope, + extreme = FALSE, vline = TRUE, rejection = FALSE, + breaks = seq(-0.04,0.04,0.005), xlim=c(-0.04,0.04)) + }) + ########################################################################### + ###Azteca + ### Specific plots for this dataset + aztStat <- mean(azteca$extract.new)-mean(azteca$extract.old) + output$aztPlot <- renderPlot({ + par(mar = c(4,4,2,2)) + plot(c(azteca$extract.new[1], azteca$extract.old[1]), type="o", + main="Original data", ylim = c(0,60), xlim=c(0.9,2.1), pch=19, + xlab="Treatment", ylab="Number of recruited ants", xaxt='n') + for(i in 2:dim(azteca)[1]) + { + points(c(azteca$extract.new[i], azteca$extract.old[i]), pch=19,type="o") + } + mtext("Extract of \n new leaves",1, at=1, line=1.5) + mtext("Extract of \n old leaves",1, at=2, line=1.5) + text(1.8,52,paste("mean difference = \n", + round(aztStat,4)),col="red") + }) + output$aztPlotRandom <- renderPlot({ + thisSet <- randomizedAzt() + thisDf <- data.frame(matrix(unlist(thisSet), nrow=21)) + names(thisDf) <- c("plant","extract.new","extract.old") + thisStat <- mean(thisDf$extract.new)-mean(thisDf$extract.old) + isolate(values$saveDist <- rbind(values$saveDist, thisStat)) + par(mar = c(4,4,2,2)) + plot(c(thisDf$extract.new[1], thisDf$extract.old[1]), type="o", + main="Randomized data", ylim = c(0,60), xlim=c(0.9,2.1), pch=19, + col="#428bca", + xlab="Treatment", ylab="Number of recruited ants", xaxt='n') + for(i in 2:dim(azteca)[1]) + { + points(c(thisDf$extract.new[i], thisDf$extract.old[i]), pch=19,type="o", + col="#428bca") + } + mtext("Extract of \n new leaves",1, at=1, line=1.5) + mtext("Extract of \n old leaves",1, at=2, line=1.5) + text(1.8,52,paste("mean difference = \n", + round(thisStat,4))) + }) + ### Display data set as a plot + output$aztTable <- renderPlot({ + data <- azteca + tbl <- tableGrob(data, + cols = c("plant", + "extract.new", + "extract.old"), + show.rownames =FALSE, + gpar.corefill = gpar(fill = "grey80", col = "white")) + grid.draw(tbl) + },height=500) + output$aztTableRandom <- renderPlot({ + thisSet <- randomizedAzt() + thisDf <- data.frame(matrix(unlist(thisSet), nrow=21)) + data <- thisDf + tbl <- tableGrob(data, + cols = c("plant", + "extract.new", + "extract.old"), + show.rownames =FALSE, + gpar.corefill = gpar(fill = "#428bca", col = "white")) + grid.draw(tbl) + },height=500) + #Randomization output + randomizedAzt <- reactive({ + input$aztRand # triggers the calculations when the "Do it again!" button is pressed + Rsampling(type = "within_rows", dataframe = azteca, + statistics = function(dataframe) return(dataframe), cols = c(2,3), + ntrials = 1) + }) + ### main plot of the program: generates a histogram of distribution() + output$distPlotAzt <- renderPlot({ + par(mar = c(4,3,2,0)) + Rsampling::dplot(dist = as.numeric(values$saveDist), svalue = aztStat, + extreme = FALSE, vline = TRUE, rejection = FALSE, + breaks = seq(-8,8,1), xlim=c(-8,8)) + }) }) diff --git a/ui.R b/ui.R index 70be594..e3c5bbe 100644 --- a/ui.R +++ b/ui.R @@ -1,7 +1,7 @@ library(shiny) library(shinyBS) shinyUI(fluidPage(theme= "bootstrap.css", - tabsetPanel(type="tabs", + tabsetPanel(type="tabs", id="tabEvent", tabPanel("Rsampling", h2("Rsampling - resampling statistics in R"), h4("powered by ", a("Shiny", href="http://www.rstudio.com/shiny")), @@ -17,6 +17,130 @@ shinyUI(fluidPage(theme= "bootstrap.css", style="color:#f30") ) ), + navbarMenu("Tutorials", + tabPanel("Mangrove trees", + column(4, + h4("Mangrove trees and soil stability"), + h6("Question"), + p("Do mangrove trees in more unstable soil allocate + more biomass in supporting roots?"), + h6("Hypothesis"), + p("Trees located in less stable soils + will have more biomass on roots."), + h5("In the left there is a plot and table (in blue) for which + the second column has been randomized"), + actionButton("mangRand","Do it again!"), + actionButton("clear1","Clear histogram"), + plotOutput("distPlotMang",height=300), + helpText("Each time a new randomized data set is generated, the statistic of interest is + recalculated and added to this histogram. The statistic calculated on the original data + is shown as a dotted red line. Should the null hypothesis be rejected? Run this exercise in + the following tabs to find out!") + ), + column(8, + fluidRow( + h5("About this data set"), + p("Area covered by aerial roots in mangrove trees sampled in two soil types.") + ), + fluidRow( + column(3, plotOutput("mangTable")), + column(6, plotOutput("mangPlot",height=300), + plotOutput("mangPlotRandom",height=300)), + column(3, plotOutput("mangTableRandom")) + ), + fluidRow( + helpText("Source: Prado, A. et al. 2013. Variações na morfologia de sustentação em", em("Rhizophora mangle"), + "(Rizophoraceae) em diferentes condições de inundação do solo. Curso de campo + Ecologia da Mata Atlântica (G. Machado, P.I. Prado & A.M.Z. Martini eds.). + Universidade de São Paulo, São Paulo.", + tags$a(href="http://ecologia.ib.usp.br/curso/2013/pdf/PO4-2.pdf", "Download pdf!")) + ) + ) + ), + tabPanel("Balanced mangrove trees", + column(4, + h4("Mangrove trees and torque"), + h6("Question"), + p("Does the torque caused by canopy weight result in mangrove + trees with more roots?"), + helpText(tags$small("What is torque again? Review this concept in", + tags$a(href="https://en.wikipedia.org/wiki/Torque", "Wikipedia"))), + h6("Hypothesis"), + p("The higher the torque caused by the canopy, the more roots + a tree wil have."), + h5("In the left there is a plot and table (in blue) for which + the second column has been randomized"), + actionButton("rhyzRand","Do it again!"), + actionButton("clear2","Clear histogram"), + plotOutput("distPlotRhyz", height=300), + helpText("Each time a new randomized data set is generated, the statistic of interest is + recalculated and added to this histogram. The statistic calculated on the original data + is shown as a dotted red line. Should the null hypothesis be rejected? Run this exercise in + the following tabs to find out!") + ), + column(8, + fluidRow( + h5("About this data set"), + p("Ratio between canopy and trunk area, both in square meters, and number of aerial roots") + ), + fluidRow( + column(3, plotOutput("rhyzTable")), + column(6, plotOutput("rhyzPlot",height=300), + plotOutput("rhyzPlotRandom",height=300)), + column(3, plotOutput("rhyzTableRandom")) + ), + fluidRow( + helpText("Source: Prado, A. et al. 2013. Variações na morfologia de sustentação em", em("Rhizophora mangle"), + "(Rizophoraceae) em diferentes condições de inundação do solo. Curso de campo + Ecologia da Mata Atlântica (G. Machado, P.I. Prado & A.M.Z. Martini eds.). + Universidade de São Paulo, São Paulo.", + tags$a(href="http://ecologia.ib.usp.br/curso/2013/pdf/PO4-2.pdf", "Download pdf!")) + ) + ) + ), + tabPanel("Protective ants", + column(4, + h4("Protective ants"), + h6("Question"), + p("Do ants respond more intensely to damage in younger leaves?"), + h6("Hypothesis"), + p("Damage on young leaves will lead to more recruited ants when compared + to damage on old leaves."), + h5("In the left there is a plot and table (in blue) for which + the data has been randomized. How should the randomization be performed in + a paired experimental design?"), + actionButton("aztRand","Do it again!"), + actionButton("clear3","Clear histogram"), + plotOutput("distPlotAzt", height=300), + helpText("Each time a new randomized data set is generated, the statistic of interest is + recalculated and added to this histogram. The statistic calculated on the original data + is shown as a dotted red line. Should the null hypothesis be rejected? Run this exercise in + the following tabs to find out!") + ), + column(8, + fluidRow( + h5("About this data set"), + p("The ant colonies live in the hollow trunk of", em("Cecropia"), "and can detect and expel leaf-chewing + insects. To test if this response is more intense in young leaves, drops of extract of smashed + young and old leaves were poured in two neighbor leaves of the same plant. After 7 minutes + the number of recruited", em("Azteca"),"ants in each leaf was recorded.") + ), + fluidRow( + column(3, plotOutput("aztTable")), + column(6, plotOutput("aztPlot",height=300), + plotOutput("aztPlotRandom",height=300)), + column(3, plotOutput("aztTableRandom")) + ), + fluidRow( + helpText("Source: Kondrat, H. 2012. Estímulos químicos de folhas novas promovem recrutamento eficiente + de formigas associadas à embaúba Cecropia glaziovi (Urticaceae). Curso de campo Ecologia + da Mata Atlântica (G. Machado; P.I. Prado & A.M.Z. Martini, eds.). Universidade de + São Paulo, São Paulo", + tags$a(href="http://ecologia.ib.usp.br/curso/2012/PDF/PI-Hebert.pdf", "Download pdf!")) + ) + ) + ) + ), tabPanel("Data input", helpText("Use this tab to select the input data for your analysis. The first options are datasets included in the library, select the \"upload\" option to upload your own