How do I add a download feature in my R shiny app to download plotly graphs?

Hi,
I am making this app to create plotly graphs but I am unable to use ORCA to create a download feature. Please help.
here is a snippet of the code:

output$barPlot <- renderPlotly({
req(sortedData(), input$selected_numeric)
df <- sortedData()
# Check for the selection of numeric column and set character/date column
if (!is.null(input$selected_numeric)) {
num_col <- input$selected_numeric
char_col <- ifelse(sum(sapply(df, class) == "Date") == 1, names(df)[sapply(df, class) == "Date"], names(df)[!names(df) %in% num_col])
} else {
char_col <- names(which(sapply(df, is.character)))[1]
num_col <- names(which(sapply(df, is.numeric)))[1]
}

# Reorder factor levels if data is sorted
if (!is.null(input$sortCol) && input$sortCol == char_col) {
  df[[char_col]] <- factor(df[[char_col]], levels = unique(df[[char_col]]))
}
df[[char_col]] <- factor(df[[char_col]], levels = unique(df[[char_col]]))

# Assign labels based on graph type
if (input$graphType == 'Horizontal Bar Graph') {
  x_axis <- input$ylab
  y_axis <- input$xlab
} else {
  x_axis <- input$xlab
  y_axis <- input$ylab
}
# Determine the unique number of categories/labels on x-axis
num_categories <- length(unique(df[[char_col]]))

# Set label angle dynamically based on the number of categories
# For fewer categories, a smaller angle (or 0) can be used, and for more, a larger angle
label_angle <- if (input$graphType == 'Vertical Bar Graph') {
  ifelse(num_categories <= 10, 45, ifelse(num_categories <= 20, 90, 90))
} else {
  # For Horizontal Bar Graph or other graph types, you might want to set it to a default value, like 0
  0
}
p <- ggplot(df, aes_string(x = char_col, y = num_col)) + 
  geom_bar(stat = "identity", fill = rgb(74/255, 121/255, 134/255), width = input$barSpace) +
  labs(title = input$chartTitle, x = x_axis, y = y_axis) +
  theme_minimal() +
  theme(
    plot.title = element_text(
      hjust = 0.5,
      size = input$fontSize, 
      family = "Arial",
      colour = rgb(27/255, 87/255, 104/255)
    ),
    axis.title.x = element_text(family = "Arial"), # Set Arial font for x axis title
    axis.title.y = element_text(family = "Arial"), # Set Arial font for y axis title
    axis.text.x = element_text(angle = label_angle, vjust = 1, hjust = 1), # Rotate x-axis labels
    panel.grid = element_blank()
  )

# Add coord_flip if Horizontal Bar Graph is selected
if (input$graphType == 'Horizontal Bar Graph') {
  p <- p + coord_flip()
}

# Convert ggplot object to plotly object
p <- ggplotly(p)

3 points

  1. there is no appearance of orca() in the code you shared, therefore theres no sign that you are using or attempting to use orca
  2. if you were to want a plotly image to pass in as a parameter to orca, you would need it be something attached to a name, so that it can be identified. some reactive() would be suitable for that; you should assign your plotting code to the reactive, and your renderplotly should be rewritten so as not to contain plotting code but rather to require the reactive; when you therefore go on to create a download button you will thereby be able to incorporate an orca call requireing your plot reactive as part of that
  3. orca is no longer the only game in town; I havent tried it myself but the latest documentation is about using kaleido help(save_image, package = "plotly") shows this. Also in previous work I've done I have used webshot package to take a screenshot of a plotly widget saved out to HTML; though I'm looking to transition from that approach likely towards using kaleido.

Calling it again worked for me. Now battling with learning kaleido, cannot find good documentation.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.