首页 > 解决方案 > 滚动条如何包含在图例中?

问题描述

很高兴看到超过 18 行的图例。

将输出包装在 wellPanel( style = 'overflow-y:scroll;' ) 中似乎是正确的功能,但没有奏效。

library(shiny)

shinyApp( 
  ui     <- fluidPage( 
    splitLayout(cellWidths = c('20%', '30%', '50%'),  
      sliderInput('mySldr'  , value = 4   , min =  1 , 
               label = 'how many groups ?', max = 44 ),     # max 52 > 44
      plotOutput( 'myLgnd' ),
      plotOutput( 'myPlt'  )
     )
   ),        
  server          <- function(input, output, session) {  
    n             <- reactive({   input$mySldr                       })
    theD          <- reactive({ matrix(runif( 5*n() ), nrow = 5,  
                  dimnames = (list( 1:5, rep(letters,2)[ 1:n() ] ))) }) 
    output$myLgnd <- renderPlot({ 
                  legend(  x = 'center',   legend = colnames( theD() ), 
                                             fill = rainbow( n() ) ) })  
    output$myPlt  <- renderPlot({ 
          matplot( x = c(2001:2005), type = 'o', xlab = '', ylab = '',       
                           y = theD()      , col  = rainbow( n() ) ) })
   }
 )

任何对可滚动图例的帮助将不胜感激。谢谢你。

标签: shinylegend

解决方案


不是很健壮,但这适用于您的示例:

            div(
              style = "overflow-y: auto; overflow-x: hidden",
              plotOutput( 'myLgnd' )
            ),

output$myLgnd <- renderPlot({ 
  par(mar = c(0,0,0,0))
  legend(  x = 'center',   legend = colnames( theD() ), 
           fill = rainbow( n() ) ) }, height = function(){170+10*ncol(theD())})  

我已经找到了这些值170,并且10经过多次试验错误。也许有更好的价值。

最好用plotly.


推荐阅读