7

I would like to take the height of my plot reactive because sometimes I have to draw just one graph and sometimes two or three graphs. Here my code :

    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)

But shiny reads the height of the plot just one time so that it's not reactive. Thank you for your answers !

4
  • maybe you should include the part of your code that dictate the number of plots you will draw Commented Jan 14, 2016 at 16:55
  • Thank you for your answer, I finally figuerd out a solution : Commented Jan 15, 2016 at 13:17
  • 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}) with option height ="auto" in ui.r Commented Jan 15, 2016 at 13:19
  • posted as an answer for future readers. Commented Jan 15, 2016 at 13:22

2 Answers 2

9

I finally figuerd out a solution ;

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")
Sign up to request clarification or add additional context in comments.

Comments

3

This is the solution that I finally got, after drudging with my app and thanks to all persons in this thread for your kind suggestions. Please don't mind the name of the variables.

In the server part:

  #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!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.