0
votes

I've just started at an internship where my first task is to improve a Shiny application by updating all existing ggplot graphs into Plotly graphs. I'm pretty new to R and Shiny, so I'm having a couple of issues getting started.

Currently, one of the graphs in this application are generated as follows:

    output$barBoxPlot <- renderUI({
      plotOutput("Barplot", width = 700, height = 300
                 , click = "plot_click"
                 , hover = "plot_hover"
                 , brush = "plot_brush")
    })

Where "Barplot" is an outputId that corresponds to a ggplot graph generated in another part of the code.

I'm trying to plot this graph ("Barplot") as a Plotly graph, using the ggplotly() function, using something like this:

output$barBoxPlot <- renderPlotly({
    ggplotly(<insert graph here>)
})

However, I cannot figure out how to pass in the "Barplot" parameter (which is an outputId) into ggplotly(). ggplotly takes in an instance of a ggplot graph, and not an outputId.

Is there any way to pass in an outputId into ggplotly()?

Thank you!

1

1 Answers

0
votes

Would've put this in a comment but can't to that yet because of reputation:

There are multiple ways to solve this wihtout trying to pass an outputID to ggplotly().

  1. In the reactive that is in the output$Barplot <- renderPlot(this_reactive_here()) , use ggplotly() directly (if you don't need the original output$barBoxPlot
  2. Create a function or a reactive that takes a ggplot object and only does return(ggplotly(x)) , then put that into your renderPlotly()

EDIT for clarification on 2:

The Barplot that is in plotOutput should have a server entry that looks something like this:

output$Barplot <- renderPlot({
  ggplot(data, aes(...)) # etc
})

Take the code inside and put it in a reactive, like this:

barplot_ggplot <- reactive({
  ggplot(data, aes(...)) # etc
})

Then you can use it in the output$Barplot as barplot_ggplot() and in the renderPlotly() as ggplotly(barplot_ggplot()).

I hope that's a little bit clearer.