Shiny plotlyOutput() not responding to height and width sizes

前端 未结 2 1145
迷失自我
迷失自我 2021-02-10 03:46

I am creating a Shiny app with a plotly scatterplot. I am trying to keep the scatterplot square-shaped, but want a much larger size than its default. In the simple MWE below, fo

2条回答
  •  面向向阳花
    2021-02-10 03:50

    It seems that the plotlyOutput function does not hand over the height/width parameters. As mentioned before, you can force to plot to be of a certain size:

    p <- plot_ly(x = x, y = y, height=800) 
    

    However, if you have following elements on the site (e.g. another plot like in my case) the plot is partially hidden. I found a workaround by manipulating the plotlyOutput object on the server-side. Here is a simple example:

    Server:

    output$plotly <- renderUI({
      plot_output_list <- lapply(1:3, function(i) {
        plotname <- paste0("plotly", i)
        plot_output_object <- plotlyOutput(plotname)
        plot_output_object <- renderPlotly({
          p <- plot_ly(x = a, y = b)
          return(p) # only necessary when adding other plotly commands like add_trace 
        })
      })
      # for each element set the height (here went something wrong with plotlyOutput)
      for(i in 1:length(plot_output_list)){
        attr(plot_output_list[[i]],'outputArgs') <- list(height="850px")
      }
      # return
      return(plot_output_list)
    })
    

    UI:

    uiOutput("plotly")
    

    I anyway had to go the way via renderUI since I have a dynamic number of plots. Hope this helps you too

提交回复
热议问题