如何在闪亮的上下文中更改绘图的高度

Nad*_*hri 4 r shiny

如下图所示,当在侧栏中选择多个县时,我的图的高度保持不变。这使得情节过于拥挤,以至于标签变得不可读。我想根据侧栏中选择的县数来改变我的情节的高度。我怎样才能做到这一点?

下面,请查看我认为需要修改的部分代码。

在此处输入图片说明

界面元素

mainPanel(plotOutput("workforce_plot"), 
Run Code Online (Sandbox Code Playgroud)

服务器元素

workforce_plot <- reactive({
     ggplot(workforce_data(), aes(x =Age_Group, y = Percentage, fill=County.y)) + 
     geom_bar(stat = "identity",position = position_dodge()) +                                                         
     geom_text(
     aes(label = Percentage2),
     vjust = 0.5,
     colour = "black", 
     position = position_dodge(width=0.9),
     fontface = "bold",
     size=3.2,
     hjust = 0)+
     ylim(0,110)+        
     coord_flip()+

     labs(
          x = "Age Groups",
          y = "Percentage",
          face = "bold")
          })

   output$workforce_plot <- renderPlot ({ 
            workforce_plot()
          })
Run Code Online (Sandbox Code Playgroud)

use*_*748 6

您可以height向您的renderPlot函数添加一个参数,并将一个函数传递给它,该函数根据一些相关的反应调整高度。

一个例子:

output$workforce_plot <- renderPlot ({ 
            workforce_plot()
          }, height = function() {200 + (length(workforce_data()[["County.y]]) *.2)})
Run Code Online (Sandbox Code Playgroud)

(它也可能类似于length(input$country)而不是反应式 expr。)

然后在为该面板ui.R定义height = 'auto'