Create a simple R Shiny app that outputs the plot you just created, use the following scaffold:
library(shiny)
# other libraries?
# load data
# Define UI
ui <- fluidPage(
titlePanel(""),
sidebarLayout(
sidebarPanel(
# input here
),
mainPanel(
# output here (?plotOutput)
)
)
)
# Define server logic
server <- function(input, output) {
output$plot <- renderPlot({
# ggplot2
})
}
# Run the application
shinyApp(ui = ui, server = server)
Possible solution:
library(shiny)
library(shiny)
library(tidyverse)
library(ggbeeswarm)
data(iris)
# Define UI
ui <- fluidPage(
titlePanel(""),
sidebarLayout(
sidebarPanel(
# input here
),
mainPanel(
plotOutput("plot")
)
)
)
# Define server logic
server <- function(input, output) {
output$plot <- renderPlot({
ggplot(iris, aes(x=Species, y=Petal.Length)) +
geom_violin(aes(fill=Species)) +
geom_quasirandom() +
geom_boxplot(width=0.1)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Possible solution:
sliderInput(
"dotsize",
label = "Dot Size",
min = 0.1,
max = 5,
value = 1,
step = 0.1
)
output$plot <- renderPlot({
ggplot(iris, aes(x=Species, y=Petal.Length)) +
geom_violin(aes(fill=Species)) +
geom_quasirandom(size=input$dotsize) +
geom_boxplot(width=0.1)
})
downloadButton(
outputId = "save_violinplot",
label = "Save plot",
icon = shiny::icon("download")
)
Hint: make the plot a reactive({}) to use it twice!
output$violinplot <- renderPlot({
violinplot()
})
output$save_violinplot <- downloadHandler(
filename = 'plot.pdf',
content = function(file) {
ggsave(file, plot=violinplot(), width=297, height=210, unit='mm')
}
Solution server part
violinplot <- reactive({
ggplot(iris, aes(x=Species, y=Petal.Length)) +
geom_violin(aes(fill=Species)) +
geom_quasirandom(size=input$dotsize) +
geom_boxplot(width=0.1)
})
output$violinplot <- renderPlot({
violinplot()
})
output$save_violinplot <- downloadHandler(
filename = 'plot.pdf',
content = function(file) {
ggsave(file, plot=violinplot(), width=297, height=210, unit='mm')
}
Good job! 👍