I have a shiny app displaying a ggplot dotplot of columns to choice from the iris dataset. How can I add a zoom functionality to the plot, being able to zoom in and out of the plot? Possible with a doubleclick or by moving the mouse wheel?
I have already tried using plotly, but have not been successful in this because the plot never showed up correctly.
library(shiny)
library(readxl)
library(ggplot2)
library(ggrepel)
library(dplyr)
ui = fluidPage(
br(),
fluidRow(
column(width = 2, # 2/12
varSelectInput("variablex", "Trait x:", iris[1:4]),
varSelectInput("variabley", "Trait y:", iris[1:4]),
varSelectInput("variabledot", "Trait dot size:", iris[1:4]),
#> plot dimensions
textInput(
inputId ="plotdim"
, label = "Dimensions (W*H*dpi)"
, placeholder = "W*H*dpi"
, value = "35*25*100"
),
br(), # Add a line break for spacing
column(width = 2, # 2/12
downloadButton("download_pdf", "Download as PDF")
)
),
column(width = 10, # 10/12
#> render as PDF
uiOutput("plotui")
)
),
)
server = function(input, output) {
culturename<-"Iris"
Speciesname<-iris$Species[1]
# plot reactive
plot <- reactive({
ggplot(iris, aes(x=!!input$variablex, y=!!input$variabley)) +
geom_point(aes(size=!!input$variabledot), pch=21) +
theme_grey(base_size = 22) +
theme(legend.position = "right") +
guides(fill = guide_legend(override.aes = list(size=3)))
})
# plot render
output$plot <- renderImage({
dim <- input$plotdim %>%
strsplit(., "[*]") %>%
unlist() %>% as.numeric()
if(length(dim) < 3) stop("Need W*H*dpi")
outfile <- tempfile(fileext = ".png")
png(outfile, width = dim[1], height = dim[2], units = "cm", res = dim[3])
print(plot())
dev.off()
list(src = outfile)
}, deleteFile = TRUE)
# Save PDF
output$download_pdf <- downloadHandler(
filename = function(){
paste(culturename,"_",Speciesname,".pdf",sep="")
},
content = function(file){
pdf(file, width = 20, height = 13)
print(plot())
dev.off()
}
)
#> render ui in the server
output$plotui <- renderUI({
tagList(
div(imageOutput("plot"), align = "center"),
)
})
}
shinyApp(ui = ui, server = server)