Changing Dimensions of downloaded plot of plotly in r shiny - r

I have a code which downloads the png image of the graph formed in plotly with same dimensions everytime. I want to generate a image with higher quality in it. Like if i click on the buttons provided at the top right corner in plotly graphs, it should download with different dimensions
Reference code is given below
library(shiny)
library(plotly)
ui <- fluidPage(
selectInput("choice", "Choose", choices = names(iris), selected = NULL),
plotlyOutput("graph")
)
server <- function(input, output, session){
output$graph <- renderPlotly({
plot_ly(iris, x = ~get(input$choice), y = ~Sepal.Length, type = 'scatter', mode = 'markers')
})
}
shinyApp(ui, server)

You can use toImageButtonOptions in the config function to set the dimensions:
plot_ly(
iris, x = ~get(input$choice), y = ~Sepal.Length, type = 'scatter', mode = 'markers'
) %>% config(
toImageButtonOptions = list(format = "png", width = 1500, height = 750)
)

Related

how to add reactive x and y axis labels to shiny plotly graph?

I am struggling to find a way to add axis labels to this plotly graph. Since it's a bit different than when I've used plotly or even ggplot outside of apps, I can't seem to make it work. Any tips?
I would need the x and y axis labels to change with the widget on the right side of the code. I'm also not sure if the labels already show and its a matter of the graph being too large to show them.
library(shiny)
library(plotly)
library(tibble)
library(tidyverse)
library(tidyr)
library(readr)
library(dplyr)
library(ggplot2)
library(gapminder)
#read data
gm <- gapminder
# Define UI ----
ui <- fluidPage(
column(3,offset = 4, titlePanel("Explore Gapminder Data with Shiny")),
headerPanel('Graphs'),
mainPanel(
plotlyOutput('plot')
),
sidebarPanel(
#variable selection for x-axis
selectInput(inputId ='xvrbl', #The input slot that will be used to access the value.
label = 'X-Axis Variable', #Display label for the control, or NULL for no label.
choices = colnames(gm), #List of values to select from
selected = 'CO2 emissions (metric tons per capita)'
),
checkboxInput(inputId = "LogX",
label = "Log Transform",
value = FALSE),
#variable selection for y-axis
selectInput(inputId ='yvrbl', #The input slot that will be used to access the value.
label = 'Y-Axis Variable', #Display label for the control, or NULL for no label.
choices = colnames(gm), #List of values to select from
selected = 'gdpPercap'
),
checkboxInput(inputId = "LogY",
label = "Log Transform",
value = FALSE),
# date range - slider
sliderInput(
inputId = "time",
label = "Years",
min = min(gm$year),
max = max(gm$year),
step = 5,
value = range(gm$year)
)
)
)
server <- function(input, output) {
x <- reactive({
x <- dat()[[input$xvrbl]]
if (input$LogX) x <- log(x)
return(x)
})
y <- reactive({
y <- dat()[[input$yvrbl]]
if (input$LogY) y <- log(y)
return(y)
})
dat <- reactive({
subset(gm, year >= input$time[[1]], year <= input$time[[2]])
})
output$plot <- renderPlotly({
plot_ly(
x = x(),
y = y(),
type = "scatter",
mode = "markers",
color = dat()$continent
)
})
}
# Run the app
shinyApp(ui = ui, server = server)
You should specify the layout parameter to renderPlotly:
output$plot <- renderPlotly({
plot_ly(
x = ~x(),
y = ~y(),
type = "scatter",
mode = "markers",
color = dat()$continent) %>%
layout(
yaxis = list(title = input$yvrbl),
xaxis = list(title = input$xvrbl)
)
})

Get "selected data" in a parallel coordinates chart using plotly to update table

As couple of users before, I want to update a table conditional on the selected lines in a parallel coordinates chart produced by plotly in R. I found a similar question for Python but don't get it working in R.
Though I get the latest dynamic interaction/selection by event_data("plotly_restyle"), I have the following issues:
It provides only information for the axis the user has changed the latest and no information about all other axes which are returned as NULL. Though this is annoying, it can be resolved by updating e.g. a reactive value in the background observing the "event data" (not shown).
Unselecting the selection area by a double click, gives back a NULL which has to be used for resetting the selection.
In case the user modifies the order of the axis per manually dragging the axis, I get a different output and actually which lists a perfect summary of the order of all axes/variables and their user defined limits. This is actually the output I want to get in any case, so also when just a single axis is updated.
I give a minimal example to play with and to better understand my given points above (based on a official plotly example). Any input welcome!
library("shiny")
library("plotly")
library("DT")
ui <- fluidPage(
headerPanel("Example"),
mainPanel(
plotlyOutput("plot"),
verbatimTextOutput("text"),
DTOutput("table")
)
)
server <- function(input, output, session) {
df <- reactive({read.csv("https://raw.githubusercontent.com/bcdunbar/datasets/master/iris.csv")})
output$plot <- renderPlotly({
fig <- plot_ly(
data = df(),
source = "myplot",
type = "parcoords",
line = list(color = ~species_id,
colorscale = list(c(0, "red"), c(0.5, "green"), c(1, "blue"))),
dimensions = list(
list(
range = c(2,4.5),
label = "Sepal Width",
values = ~sepal_width
),
list(
range = c(4,8),
constraintrange = c(5,6),
label = "Sepal Length",
values = ~sepal_length
),
list(
range = c(0,2.5),
label = "Petal Width",
values = ~petal_width
),
list(range = c(1,7),
label = "Petal Length",
values = ~petal_length
)
)
)
fig
fig <- event_register(fig, "plotly_restyle")
})
output$text <- renderPrint({
event_data("plotly_restyle", source = "myplot", session = session)
})
output$table <- renderDT({
datatable(
df()
)
})
}
shinyApp(ui,server)

Shiny plotly : how to fix hidden hovermode tooltip at the top of the graph

I have a shiny app with a Plotly in the middle of the screen with multiple lines. I am using "x unified" hovermode, so when I put the cursor on the plot, I can see the value for each line. However, when I have a lot of lines, the tooltip of "hovermode" is cut at the top of the graph, and I am trying to make it entirely visible.
What I already tried and doesn't work:
put a higher z-index on the graph ph
add margin or padding to the graph, but the tooltip is still cut at the top of the graph
Any idea how I can fix this ?
library(shiny)
library(plotly)
df <- data.frame(
xvar = rep(1:5, each = 26),
yvar = sample(1:100, 5*26, replace = TRUE),
groupvar = rep(letters, 5)
)
ui <- fluidPage(
tags$div(id = "my_div", style = "height:100px; background-color: blue; z-index: 0"),
plotlyOutput("my_plot") # I need the plot hovermode to be over the previous div
)
server <- function(input, output) {
output$my_plot <- renderPlotly({
fig <- plot_ly(df, x = ~xvar, y = ~yvar, type = 'scatter', mode = 'lines',
name = ~groupvar, color = ~groupvar)
fig <- fig %>% layout(hovermode = "x unified")
fig
})
}
shinyApp(ui = ui, server = server)
If it doesn't affect the layout of your shiny app, increasing the height of the plot allows the hover label to stay within the borders and prevents overflow.
ui <- fluidPage(
tags$div(id = "my_div", style = "height:100px; background-color: blue; z-index: 0"),
plotlyOutput("my_plot", height = "600px")
)

Download multiple files to one PDF shiny

I have an app with multiple pages that each have two plots and two tables. Is there a way to download all 4 as one file? A PDF would be nice but if there's a different file type that would work better, I'm open to that.
I tried following the suggestion here but I could not get it to work. I need the plots to be interactive within the app, but they can be static when downloaded. Any suggestions or is this even possible? Is there a way to even just print the main panel?
library(plotly)
library(shiny)
library(DT)
ui <- fluidPage(
mainPanel(
plotlyOutput("SepalPlot"),
DT::dataTableOutput("Sepal"),
plotlyOutput("PetalPlot"),
DT::dataTableOutput("Petal")
)
)
server <- function(input, output) {
output$SepalPlot<- renderPlotly({
plot_ly(iris, x = ~Sepal.Length, y = ~Sepal.Width, type = 'scatter', mode = 'markers')
})
sep<-data.frame(c(iris$Sepal.Length, iris$Sepal.Width))
output$Sepal<-renderDataTable({datatable(sep)})
output$PetalPlot<- renderPlotly({
plot_ly(iris, x = ~Petal.Length, y = ~Petal.Width, type = 'scatter', mode = 'markers')
})
pet<-data.frame(c(iris$Petal.Length, iris$Petal.Width))
output$Petal<-renderDataTable({pet})
}
shinyApp(ui = ui, server = server)

Plotly 'Download as png' resizing image when used in RShiny application

I'm using plotly package for R to display some large graphs in my shiny application, the graphs display nicely as intended. However, when I click the "Download as png" button, the downloaded .png has been resized. here is a demo of this behavior.
How can I specify the resolution for the exported plot?
Here is a minimal example that demonstates the issue by having a really long title that gets clipped on download
app.R
library(shiny)
library(ggplot2)
library(plotly)
ui <- fluidPage(titlePanel("Plotly Download demo"),
plotlyOutput("demoPlotly"))
server <- function(input, output) {
output$demoPlotly <- renderPlotly({
#make an arbritrary graph with a long title
p <- iris %>%
ggplot(aes(x = Petal.Length, y = Petal.Width, color = Species)) +
geom_point() +
labs(title = "This is a really long title to demo my problem of plotly resizing my downloaded output")
ggplotly(p)
})
}
# Run the application
shinyApp(ui = ui, server = server)
The downloaded graph looks like this:
Michael,
Take a look at plotly_IMAGE(). [Version 4.7.1]. The function does allow you to specify width and height for the saved image. For example 800 by 600.
Regarding your example, I have provided an example of how you might modify the layout to accommodate the title wrapping. Note the changes the margin and padding. I also inserted a call to plotly_IMAGE to provide a simplified example of its usage - it slows things down as it is in line with the plot generation. I'd recommend you add a button and separate out from image display.
Hope this helps
Take care
T.
Pre-requisite - Plotly Credentials:
To use this plotly example you need to register and obtain a plotly api key. Once obtained you need add these entries to your ~/.Renviron file.
Ref: https://plot.ly/r/getting-started/
Example .Renviron file entries
plotly_username="xxxxx"
plotly_api_key="xxxxxx"
Example Code
library(shiny)
library(ggplot2)
library(plotly)
wrapper <- function(x, ...) {
paste(strwrap(x, ...), collapse = "\n")
}
# Example usage wrapper(my_title, width = 20)
my_title <- "This is a really long title to demo my problem of plotly resizing my downloaded output"
ui <- fluidPage(titlePanel("Plotly Download demo"), plotlyOutput("demoPlotly"))
pal <- c("blue", "red", "green")
pal <- setNames(pal, c("virginica", "setosa", "versicolor"))
layout <- list(title = wrapper(my_title, width = 60),
xaxis = list(title = "Petal Length"),
yaxis = list(title = "Petal Width"),
margin = list(l = 50, r = 50, b = 100, t = 100, pad = 4))
server <- function(input, output) {
output$demoPlotly <- renderPlotly({
# make an arbitrary graph with a long title
p <- iris %>%
plot_ly(x = ~Sepal.Length,
y = ~Petal.Width,
color = ~Species,
colors = pal,
mode = "markers",
type = "scatter") %>%
layout(autosize = F,
title = layout$title,
xaxis = layout$xaxis,
yaxis = layout$yaxis,
margin = layout$margin)
p$elementId <- NULL
# Example usage of plotly image
plotly_IMAGE(p,
width = 800,
height = 600,
format = "png",
scale = 1,
out_file = "output.png")
p
})
}
# Run the application
shinyApp(ui = ui, server = server)
Screen Shot Image - screen capture
Plotly save - screen click
plotly_IMAGE() save image output 800 by 600

Resources