Motivation
My group was working on a Shiny app for the first time and we found ourselves having to add one more input to our Shiny app, which happened to be a slider input. Until that point, our app’s function was to generate a boxplot which show differences between regions based on a chosen plant trait such as protein content. We wanted to enable the user to further refine the results by defining blast resistance level using the slider input. While adding the slider input itself was straightforward, we had trouble making the boxplot react to any changes to the slider input. At the end of our lab, I guessed that we needed to somehow filter our dataset based on the slider input before generating the boxplot.
The solution
My groupmate Colton managed to come up with a code which did just that! I further improved it so it is able to read the input as a range, in case the user wants to filter the results based on a range of blast resistance levels.
- The code for our
server.R
. Look at the line containing thefilter
function for the solution!1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#
# This is the server logic of a Shiny web application. You can run the
# application by clicking 'Run App' above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(tidyverse)
library(readr)
data.pheno <- read_csv("RiceDiversity.44K.MSU6.Phenotypes.csv")
data.pheno.clean <- na.omit(data.pheno)
data.pheno.clean2 <- data.pheno[complete.cases(data.pheno[ ,c(14,13,23,25,36,39)]), ]
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
output$boxPlot <- renderPlot({
plotPhenotypes <- as.name(input$Phenotypes) # convert string to name
# set up the plot
pl <- data.pheno.clean %>%
# the important part!
filter(`Blast resistance` %in% input$Blastresistance[1]:input$Blastresistance[2]) %>%
ggplot(aes(x=Region,
y= !! plotPhenotypes, # !! to use the column names contained in plotPhenotypes
fill=Region
)
)
# draw the boxplot for the specified trait
pl + geom_boxplot()
})