Skip to content

Commit

Permalink
Function to add logo finally works as intended
Browse files Browse the repository at this point in the history
  • Loading branch information
gavieira committed Dec 1, 2023
1 parent 7e9cb9d commit 4ffa1c4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Used `dplyr::bind_rows()` instead of `rbind()` in the ShinyApp's `get_merged_db_list()` to allow merging of datasets with different rows (and probably from different databases)
* Added `tryCatch()` to ShinyApp's `merge_input_files()` to raise an error in case the rbind fails (possibly because of divergences in column names/number) and warn the user that the files provided are not compatible for merging
* Added 'quote' tabpanels alongside all `fileInput()` calls to determine quotation of input datasets
* Added download buttons for plots in ShinyApp
* Added package logo to plots


# biblioverlap 1.0.3
Expand Down
20 changes: 14 additions & 6 deletions R/06-plots_and_app.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,33 @@ add_logo_to_plot <- function(plot,
interpolate = TRUE,
... ) {

# Getting graphics device
previous_device <- grDevices::dev.cur()

# Set up null graphics device to avoid unintentional plotting
grDevices::pdf(NULL)

# Set up the layout for the main plot and logo
layout <- grid::grid.layout(nrow = 1, ncol = 1)
#

## Create a new page with the specified layout
grid::grid.newpage()
grid::pushViewport(grid::viewport(layout = layout))
#

## Plot the Venn diagram on the left
grid::pushViewport(grid::viewport(layout.pos.col = 1))
print(plot, newpage = FALSE)
#

## Clear the viewport before adding the logo
grid::popViewport(1)

# Getting logo
logo_raw <- png::readPNG(logo_path) #Importing logo into R
logo <- matrix(grDevices::rgb(logo_raw[,,1],logo_raw[,,2],logo_raw[,,3], logo_raw[,,4] * alpha), nrow=dim(logo_raw)[1]) #Adding transparency to logo - source: https://stackoverflow.com/questions/11357926/r-add-alpha-value-to-png-image

# Ploting logo on the right
# Pushing viewport
grid::pushViewport(grid::viewport(layout.pos.col = 1))

# Ploting logo
grid::grid.raster(logo,
width = width,
height = height,
Expand All @@ -65,10 +71,12 @@ add_logo_to_plot <- function(plot,
#Generate combined plot
combined_plot <- grid::grid.grab(wrap.grobs = TRUE )

#Generate final plot
#Generate final plot (ggplot-like object)
final_plot <- cowplot::ggdraw() +
cowplot::draw_plot(combined_plot)

# Go back to previous graphics device
grDevices::dev.set(previous_device)

#Return the final graphical object
return( final_plot )
Expand Down
10 changes: 7 additions & 3 deletions inst/biblioverApp/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,20 @@ server <- function(input, output, session) {
#### Generating plots (reactive functions)

summary_plot <- reactive( {
biblioverlap::plot_matching_summary(calculate_results()$summary,
summary <- biblioverlap::plot_matching_summary(calculate_results()$summary,
text_size = input$summary_text_size,
size = input$summary_value_size)
return(summary)
})

venn_plot <- reactive( {
biblioverlap::plot_venn(calculate_results()$db_list,
venn <- biblioverlap::plot_venn(calculate_results()$db_list,
label = input$venn_label,
label_color = input$venn_label_color,
label_size = input$venn_label_size,
label_alpha = input$venn_label_alpha,
set_size = input$venn_set_size )
return(venn)
} )

# Upset plots from UpSetR can hide its empty intersections if a NULL value is passed to its `empty.intersections` parameter
Expand All @@ -220,7 +222,7 @@ server <- function(input, output, session) {
})

upset_plot <- reactive ({
biblioverlap::plot_upset(calculate_results()$db_list,
upset <- biblioverlap::plot_upset(calculate_results()$db_list,
nsets = length(db_list),
nintersects = input$nintersects,
order.by = input$order.by,
Expand All @@ -231,6 +233,7 @@ server <- function(input, output, session) {
show.numbers = input$show.numbers,
mb.ratio = c(input$mb.ratio, 1 - input$mb.ratio)
)
return(upset)
} )

#Displaying plots in shinyApp (renderPlot functions)
Expand Down Expand Up @@ -264,6 +267,7 @@ server <- function(input, output, session) {
content = function(file) {
ggplot2::ggsave(file, plot = plot_obj,
limitsize = FALSE,
bg = 'white',
height = input$plot_height,
width = input$plot_width,
units = 'px', dpi = 'screen'
Expand Down

0 comments on commit 4ffa1c4

Please sign in to comment.