有没有办法增加 shiny
中的绘图窗口大小?取决于 ggplot
中使用的方面的数量图 - 也许使用垂直滚动。
例如使用下面的例子,当输入为"A"
有三个方面,情节看起来不错。 When the option "B"
选择绘图数量增加但绘图窗口保持相同大小导致绘图太小。
是否有一种策略可以保持所有面板高度相同,独立于输入?谢谢。
library(ggplot2)
library(shiny)
mtcars$cyl = sample(letters[1:5], 32, TRUE)
ui <- fluidPage(
navbarPage(title="title",
tabPanel("One",
column(3,
wellPanel( selectInput('name', 'NAME', c("A", "B")))),
column(9, plotOutput('plot1')))
))
server <- function(input, output) {
X = reactive({input$name == "A"})
output$plot1 <- renderPlot({
if(X()){
p1 = ggplot(mtcars, aes(mpg, wt)) + facet_grid( . ~ gear )
}else{
p1 = ggplot(mtcars, aes(mpg, wt)) + facet_grid( cyl ~ gear )
}
return(p1)})
}
shinyApp(ui, server)
最佳答案
您可以在下面找到一个工作示例:
library(ggplot2)
library(shiny)
library(tidyverse)
mtcars$cyl = sample(letters[1:5], 32, TRUE)
gg_facet_nrow<- function(p){
p %>% ggplot2::ggplot_build() %>%
magrittr::extract2('layout') %>%
magrittr::extract2('panel_layout') %>%
magrittr::extract2('ROW') %>%
unique() %>%
length()
}
ui <- fluidPage(
navbarPage(title="title",
tabPanel("One",
column(3,
wellPanel( selectInput('name', 'NAME', c("A", "B")))),
column(9, plotOutput('plot1')))
))
server <- function(input, output) {
X <- reactive({input$name == "A"})
p1 <- reactive({
if(X()){
p1 <- ggplot(mtcars, aes(mpg, wt)) + facet_grid( . ~ gear )
}else{
p1 <- ggplot(mtcars, aes(mpg, wt)) + facet_grid( cyl ~ gear )
}
return(p1)
})
he <- reactive(gg_facet_nrow(p1()))
output$plot1 <- renderPlot({p1() }, height = function(){he()*300})
}
shinyApp(ui,server)
由于以下帖子,这个答案是可能的:
height = function(){he()*300})
) gg_facet_nrow()
)。 关于r - 使用 ggplot facets 时增加 Shiny 的绘图大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50914398/