ropensci / iheatmapr

Complex, interactive heatmaps in R
https://docs.ropensci.org/iheatmapr
Other
267 stars 35 forks source link

A few questions regarding ModeBar functions #38

Open Tomer-Tsaban opened 5 years ago

Tomer-Tsaban commented 5 years ago

Hi Alicia, I recently came across iheatmapr package and it is great! very useful and straight forward.

however, I have some questions regarding the functionality of the ModeBar (the bar of plotly designed options, of snapshot, zoom etc).

  1. is there a way to modify which options are being presented to the user? for example if I wouldn't like to allow the png download. I know there is a similar option in plotly, by config, e.g. as shown here https://stackoverflow.com/questions/37437808/how-to-custom-or-display-modebar-in-plotly

this doesn't seem to work when I try it in iheatmap r, e.g. like this -

q<-main_heatmap(als_genes_cor)%>%
        add_row_dendro(als_ord_hclust_genes) %>%
        add_col_dendro(als_ord_hclust_genes) %>%
        add_row_labels(side=c("right")) %>%
        add_col_labels() %>%
        config(displaylogo = FALSE,
               modeBarButtonsToRemove = list(
                       'sendDataToCloud',
                       'toImage',
                       'autoScale2d',
                       'resetScale2d',
                       'hoverClosestCartesian',
                       'hoverCompareCartesian'
               ))

q

I get this error Error in config(., displaylogo = FALSE, modeBarButtonsToRemove = list("sendDataToCloud", : could not find function "config"

  1. is there a way to control the output of those options? specifically, I would like to change the size of the plot being downloaded to be larger than the default one.

Thanks alot!

AliciaSchep commented 5 years ago

Hi @Tomer-Tsaban this is not currently readily configurable with iheatmapr, unfortunately. However, there is a hacky back-door approach you can use to get this effect:

# q is the heatmap object created above
# first convert it to an htmlwidgets object proper (this is normally done during the print process)
q_widget <- to_widget(q)
# Edit the htmlwidget object itself
q_widget$x$config$modeBarButtonsToRemove <- list(
'sendDataToCloud',
'toImage',
'autoScale2d',
'resetScale2d',
'hoverClosestCartesian',
'hoverCompareCartesian'
)
# you should be able to display the heatmap using q_widget similarly to how you would q.  

Having a modify_config function to enable this customization directly seems like a good feature to add though going forward.

AliciaSchep commented 5 years ago

Re the 2nd question -- I don't know. I am not aware of a way to do that.

Tomer-Tsaban commented 5 years ago

Hi Alicia, thanks for your answer!

I tried to implement what you suggested (exactly your code) and for some reason I get this error: ERROR: unable to find an inherited method for function ‘to_widget’ for signature ‘"iheatmapr"

are you familiar with this?

AliciaSchep commented 5 years ago

Try iheatmapr::to_widget -- maybe another package is loaded that also has to_widget method that is shadowing iheatmapr's.

Tomer-Tsaban commented 5 years ago

Hi, thanks for your quick responses!

I tried again and figured out that the problem is with shiny package. I use iheatmapr in a shiny app, and this is how the error was received. sorry for foolishly not stating that out previously.

anyhow, when trying the to_widget function in a non-shiny context, it works smoothly. however when trying to embed in shiny, this error comes up again. here's a simple reproducible example with the measles data: ` data(measles, package = "iheatmapr")

ui<-fluidPage( iheatmaprOutput("output_q_widget")
)

server<-function(session,input,output){

    q<-main_heatmap(measles, name = "Measles<br>Cases", x_categorical = FALSE,
                    layout = list(font = list(size = 8))) %>%
            add_col_groups(ifelse(1930:2001 < 1961,"No","Yes"),
                           side = "bottom", name = "Vaccine<br>Introduced?",
                           title = "Vaccine?",
                           colors = c("lightgray","blue")) %>%
            add_col_labels(ticktext = seq(1930,2000,10),font = list(size = 8)) %>%
            add_row_labels(size = 0.3,font = list(size = 6)) %>%
            add_col_summary(layout = list(title = "Average<br>across<br>states"),
                            yname = "summary")  %>%
            add_col_title("Measles Cases from 1930 to 2001", side= "top") %>%
            add_row_summary(groups = TRUE,
                            type = "bar",
                            layout = list(title = "Average<br>per<br>year",
                                          font = list(size = 8)))

    q_widget <- to_widget(q)
    # Edit the htmlwidget object itself
    q_widget$x$config$modeBarButtonsToRemove <- list(
            'sendDataToCloud',
            'toImage',
            'autoScale2d',
            'resetScale2d',
            'hoverClosestCartesian',
            'hoverCompareCartesian'
    )
    #q_widget

    output$output_q_widget<-renderIheatmap({
            q_widget

    })

}

shinyApp(ui,server) ` Thanks, and again sorry for lacking this information in my previous question. EDIT: also, for some reason github won't accept the whole code snippet as code-text... sorry.

AliciaSchep commented 5 years ago

Thanks for additional details... the issue is that renderIheatmap will call 'to_widget' but with the above approach that has already been done. As another temporary workaround, you could define

renderIheatmap2 <- function(expr, env = parent.frame(), quoted = FALSE) {
  if (!quoted) { expr <- substitute(expr) } # force quoted
  htmlwidgets::shinyRenderWidget(expr, iheatmaprOutput, env, quoted = TRUE)
}

and use that instead of renderIheatmap (basically an edit of that function that removes the call to 'to_widget')

Will be much better to add ability to edit config directly to the package, but in meantime hope that unblocks whatever app you are working on.

Tomer-Tsaban commented 5 years ago

This works beautifully! thanks alot

rfenouil commented 4 years ago

@AliciaSchep May I had a question to your suggestion ?

I succesfully removed logo using the strategy you suggested:

heatmapPlot_widget <- iheatmapr::to_widget(heatmapPlot);

heatmapPlot_widget[["x"]][["config"]][["displaylogo"]] = FALSE;
heatmapPlot_widget[["x"]][["config"]][["toImageButtonOptions"]] = list( format='svg');

But the 'svg' output does not seem to work. Is the plotly version embedded in iHeatmapr too old to include this option ?

Thank you.

daattali commented 3 years ago

Another vote for allowing a custom config.

This issue is currently unresolvable (even with the hacky way) in shiny apps because of #71 which was mentioned by @Tomer-Tsaban

liangyu02 commented 1 year ago

NiuBi!