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)