R语言 动态改变plotly shiny图形的高度会导致与其他ui对象重叠

lndjwyie  于 2023-04-09  发布在  其他
关注(0)|答案(1)|浏览(132)

我有一个闪亮的应用程序与2图。第一个(Plot 1)根据下拉菜单有不同数量的子图。为了保持图形清晰,我需要根据子图的数量调整图形的高度。当我增加图形的高度时,它开始与下面的图重叠(图2)并位于“图2”标题的顶部,并消失在下面的图后面。有没有一种方法可以调整Plot 1的大小,并将其余元素向下移动以适应它?Reprex如下:

library(shiny)
library(dplyr)
library(plotly)

ui_page <- function(){
  fluidPage(
    fluidRow(
      selectizeInput(inputId = "num_panels",
                     label = "Select Number of Subplots:",
                     choices = c(1,2,3),
                     selected = 1)
    ),
    
    h1("Plot 1"),
    plotlyOutput("plot_1"), 
    
    h1("Plot 2"),
    plotlyOutput("plot_2"),
  )
}

server_page <- function(input, output, session) {
  
  #Create Plot 1
  output$plot_1 <- renderPlotly({
    
    # Modify the dataset dependign on teh number of subplots needed
    num_panel <- as.numeric(input$num_panels)
    locs <- c("Place 1", "Place 2", "Place 3")
    df1 <- c()
    for (ii in 1:num_panel) {
      df1[[ii]] <- data.frame(location_name=locs[ii],time=c(1,2,3,4), yy=c(10,20,30,40))
    }
    df <- bind_rows(df1)
    
    TS_plot <-  ggplot(df, aes_string(x = "time", y = "yy")) +
      geom_point() +
      facet_wrap(~location_name, ncol = 1)

    fig <- ggplotly(TS_plot, height = 300*num_panel) 
  })
  
  # Create Plot 2
  output$plot_2 <- renderPlotly({
    fig <- plot_ly(
      x = c("giraffes", "orangutans", "monkeys"),
      y = c(20, 14, 23),
      name = "SF Zoo",
      type = "bar",
      height = 300)
  })
}

shinyApp(ui=ui_page, server=server_page)

看起来不错,有一个子情节:

更多的子情节,它开始重叠:

kkbh8khc

kkbh8khc1#

试试这个

library(shiny)
library(dplyr)
library(plotly)

ui_page <- function(){
  fluidPage(
    fluidRow(
      selectizeInput(inputId = "num_panels",
                     label = "Select Number of Subplots:",
                     choices = c(1,2,3),
                     selected = 1)
    ),
    
    h1("Plot 1"), 
    uiOutput("p1"),
    
    h1("Plot 2"),
    plotlyOutput("plot_2")
  )
}

server_page <- function(input, output, session) {
  
  ht <- reactive(300*as.numeric(input$num_panels))
  
  fig1 <- eventReactive(input$num_panels, {
    # Modify the dataset dependign on teh number of subplots needed
    num_panel <- as.numeric(input$num_panels)
    locs <- c("Place 1", "Place 2", "Place 3")
    df1 <- c()
    for (ii in 1:num_panel) {
      df1[[ii]] <- data.frame(location_name=locs[ii],time=c(1,2,3,4), yy=c(10,20,30,40))
    }
    df <- bind_rows(df1)
    
    TS_plot <-  ggplot(df, aes_string(x = "time", y = "yy")) +
      geom_point() +
      facet_wrap(~location_name, ncol = 1)
    
    ggplotly(TS_plot, height = 300*num_panel) 
  })
  
  #Create Plot 1
  output$plot_1 <- renderPlotly({
    fig1()
  })
  
  output$p1 <- renderUI({
    plotlyOutput("plot_1", height=ht())
  })
  
  fig2 <-  plot_ly(
      x = c("giraffes", "orangutans", "monkeys"),
      y = c(20, 14, 23),
      name = "SF Zoo",
      type = "bar",
      height = 300)
  
  # Create Plot 2
  output$plot_2 <- renderPlotly({
    fig2
  })
}

shinyApp(ui=ui_page, server=server_page)

相关问题