1

Am learning to use R shiny and I am trying to create a heatmap that allows the user to specify the height in plotlyOutput to prevent labels from being clumped together. My minimal working code:

library(shiny)
library(plotly)

ui <- fluidPage(

titlePanel("Heatmap"),
sidebarLayout(
sidebarPanel(
  sliderInput("mapHeight", 
              "Heatmap Height", 
               min = 500, 
               max = 1000,
               value =500),
  sliderInput("L", "left adjust", min = 0, max =900, value = 80)
  ),
  mainPanel(plotlyOutput("heatmap"))
  )
  )

server <- function(input, output) {

 output$heatmap <- renderPlotly({ 
 p<- heatmaply(mtcars, plot_method = "plotly")%>% 
  layout(margin = list(l = input$L, r =50 , t=0, b=90))
 #not sure why this is being ignored
 ggplotly(p, height = input$mapHeight)
 })
 }

shinyApp(ui = ui, server = server)
Eddy Zavala
  • 549
  • 1
  • 4
  • 12
  • What happens if you set an initial value to your `sliderInput`? If it starts with an invalid value, you will receive the error. It would be helpful if the code in your question represented a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Kevin Arseneau Feb 12 '18 at 00:23
  • I actually have the sliderInput set up as: sliderInput("mapHeight", "Heatmap Height", min= 500, max =1000, value = 500). So the starting value is 500. And my apologies for not posting reproducible code, I just thought it was to long. – Eddy Zavala Feb 12 '18 at 00:37

1 Answers1

2

The constraint is related to the heatmaply package, the solution below is temporary while plotly continues to accept the height argument in layout.

Warning: Specifying width/height in layout() is now deprecated. Please specify in ggplotly() or plot_ly()

You could approach the developer on their GitHub and raise and issue or better yet a pull request with the changes you propose. For the time being, the solution below works with Plotly 4.7.1.

app.R

library(shiny)
library(heatmaply)

ui <- fluidPage(

  titlePanel("Heatmap"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("mapHeight", "Heatmap Height",
                  min = 500, max = 1000, value = 500),
      sliderInput("L", "left adjust", min = 0, max = 900, value = 80)
    ),
    mainPanel(plotlyOutput("heatmap"))
  )
)

server <- function(input, output) {

  output$heatmap <- renderPlotly({

    heatmaply(mtcars) %>%
      layout(margin = list(l = input$L, r = 50, t = 0, b = 90),
             height = input$mapHeight)

  })
}

shinyApp(ui = ui, server = server)
Kevin Arseneau
  • 6,186
  • 1
  • 21
  • 40
  • your code does exactly what I need. However, the heatmap am using is not a ggplot object and so it when I try to use ggploty it ignores the input (I dosent react to user changing the height). Am using heatmaply to build the heatmap. Would you happen to know alternative methods to adjusting the height? I have tried so many things to no avail. – Eddy Zavala Feb 16 '18 at 20:29
  • I have edited my original post and provided minimal working code. Thank you for your help in advance. – Eddy Zavala Feb 17 '18 at 23:51
  • @EddyZavala, revised my solution – Kevin Arseneau Feb 18 '18 at 22:14