I have a " renderplot " function in the server section of Shiny that produces 1 to 4 figures.
how can I change the height of the plot area in the shiny app based on the number of the figures (automatically)?
I mean the height for 4 figures should be bigger than the height for 2 figures.
I used wight = "auto" , height = "auto" in the UI section but I got error. So I assign a fix size to it (height = "800"), but if I have 1 figure it would be very big. if I have 3 or 4 they get smaller.
please see a part of my code:
#UI section
plotOutput("fancyPlot", inline = F, height = "800")
#Server section
output$fancyPlot <- renderPlot({
#I get the plot_list_final which has some plots (1 or 2 or 3 or 4).
n <- lenght(plot_list_final )
nCol <- floor(sqrt(n))
p_last = do.call("grid.arrange", c(plot_list_final, ncol=nCol))
return(p_last)
})
You can pass a function to the heigth argument of the renderPlot() function so you can define any logic inside based on the length of your plot_list, for example:
output$fancyPlot <- renderPlot({
#I get the plot_list_final which has some plots (1 or 2 or 3 or 4).
n <- length(plot_list_final)
nCol <- floor(sqrt(n))
p_last = do.call("grid.arrange", c(plot_list_final, ncol=nCol))
return(p_last)
}, height = function() {
if_else(length(plot_list_final) == 1,
400,
800)
})
this is the code that I replaced, the problem is not related to "length"
I removed the " , " because I got an error.
output$fancyPlot <- renderPlot({
#I get the plot_list_final which has some plots (1 or 2 or 3 or 4).
n <- lenght(plot_list_final )
nCol <- floor(sqrt(n))
p_last = do.call("grid.arrange", c(plot_list_final, ncol=nCol))
return(p_last)
}
height = function() {
if_else(length(plot_list_final) == 1,
400,
800)}
})
looks like you missed the comma but not just that, the arrangement of the open and closing round and curly brackets. best try
output$fancyPlot <- renderPlot({
#I get the plot_list_final which has some plots (1 or 2 or 3 or 4).
n <- lenght(plot_list_final )
nCol <- floor(sqrt(n))
p_last = do.call("grid.arrange", c(plot_list_final, ncol=nCol))
return(p_last)
},
height = function() {
if_else(length(plot_list_final) == 1,
400,
800)}
)
I advise you not to hold back potentially useful information i.e. the content of your error