【问题标题】:Shiny : variable height of renderplotShiny:渲染图的可变高度
【发布时间】:2016-04-19 23:31:38
【问题描述】:

我想采用反应式绘图的高度,因为有时我只需要绘制一张图,有时我需要绘制两到三张图。这是我的代码:

    output$myplot<-renderPlot({

    plot_to_draw <- data[data$code==input$code,"River_name"]

    plot(plot_to_draw)   

    number_of_plot <- length(plot_to_draw)

    },height = 500*number_of_plot)

但闪亮只读取一次情节的高度,因此它不是反应性的。 谢谢你的回答!

【问题讨论】:

  • 也许你应该在代码中包含决定你将绘制的绘图数量的部分
  • 谢谢你的回答,我终于找到了解决办法:
  • output$myplot
  • 作为未来读者的答案发布。

标签: r shiny


【解决方案1】:

我终于想出了一个解决办法;

server.R
output$myplot<-renderPlot({ 
plot_to_draw <- data[data$code==input$code,"River_name"] 
plot(plot_to_draw) 
number_of_plot <- length(plot_to_draw) 
},height = function(){500*number_of_plot})

ui.R
plotOutput(outputId="myplot",height = "auto")

【讨论】:

    【解决方案2】:

    这是我在苦苦使用我的应用程序后终于得到的解决方案,并感谢此线程中的所有人的好意建议。请不要介意变量的名称。

    在服务器部分:

      #I had to transform my imput into a data.frame, otherwise sqldf didn't work.
      country12<- reactive({as.data.frame(matrix(c(input$sel_country121),1,1))})
      question12<-reactive({
        country121 <- country12()
        sqldf("SELECT dp.Year, dp.Type_of_Product COUNT (*) as num_products12 FROM dataPanelV5 dp, country121 p WHERE dp.Country_name = p.V1 GROUP BY dp.Year, dp.Type_of_Product")})
    
    #I use this function to calculate the number of different types of products resulting from the query, using unique() and calculating its length, as that number is the number of facets.
      n_facets12<-function(){
                          question121<- question12()
                          return (500*length(unique(question121$Type_of_Product)))}
    
      output$barplot12 <- renderPlot({ 
    
        question121<-question12()
    
        ggplot(question121,aes(x=factor(Year),y=num_products12,fill=Type_of_Product)) + geom_bar(stat="identity") + facet_grid(Type_of_Product ~ .,scales = "free_y") +
          geom_text(aes(label=num_products12), vjust=-0.2, colour="black")  + scale_x_discrete(breaks=question121$Year,labels=as.character(question121$Year),position = "top") + theme(legend.position="top",axis.title.y=element_blank(),axis.text.y = element_blank(),panel.grid.major.y = element_blank(),panel.grid.minor.y = element_blank()) + labs(fill="Type_of_Product", x="Year")
    
    
      },height = n_facets12)
    
    #And it works!!
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-20
      • 2020-04-04
      • 1970-01-01
      相关资源
      最近更新 更多