簡體   English   中英

如何消除 R Shiny 中反應性 plot 下方的空白空間?

[英]How can I eliminate empty space below a reactive plot in R Shiny?

我有一個復雜的 R shiny 應用程序,它利用了多個模塊。 我有兩個不同的模塊(我將其稱為直方圖模塊和散點圖模塊 plot 模塊),每個 output 一個反應性 plot。 根據用戶輸入,任何時候都應該只顯示這些圖之一。 我可以通過單選按鈕選擇來管理它——用戶只需選擇直方圖或散點圖 plot。

我很難以吸引人的方式安排這兩個反應式 plot 輸出。 我希望直方圖和散點圖 plot 與頁面的左上角對齊。 如果我的直方圖在我的 UI 中首先列出,則當用戶選擇散點 plot 時,此散點 plot 將呈現在大空白區域下方。 當用戶選擇直方圖 plot 時,這個大空白空間被填充,但我希望這個“底部”plot 仍顯示在 Z531704A02607A1646EFCF4C1FAE1EEC6 的頂部。

In my shiny days before using modules, I simply would put my plot output inside of a conditional panel in the UI (eg, conditionalPanel("input.displayPlot", plotOutput("plot1")) However, now that I have a complex module ,我沒有輸入邏輯放在條件面板中(至少我不認為我這樣做)。

我創建了一個簡單的可重現示例,因為我可以設法重新創建這個問題。 當用戶選擇散點圖 plot 時,它會顯示在一個大的空白處。 我真的更願意在我的模塊中保留直方圖與散點圖的單選按鈕選擇(因為它也是基於其他復雜邏輯的反應性顯示),但這可能是不可能的。

histogramPlotOutput <- function(id) {
  tagList(
    plotOutput(NS(id, "histogram"))
  )
}

histogramUIOutput <- function(id) {
  tagList(
    uiOutput(NS(id, "buttons"))
  )
}

histogramServer <- function(id
) {
  moduleServer(id, function(input, output, session) {

    output$histogram <- renderPlot({
      req(input$radiobuttons)

      if(input$radiobuttons){
        p <- hist(mtcars[["mpg"]])
        p
      }

    })


    output$buttons <- renderUI({
      radioButtons(NS(id, "radiobuttons"),
                   label = "Display Histogram?",
                   choices = c(TRUE, FALSE),
                   selected = TRUE)
    })

    # export the reactive button selection out of histogram server
    # this will be used as an input by scatterPlotServer to
    # determine if an alternate plot should be displayed

    reactive(
      input$radiobuttons
    )

  })
}


scatterPlotOutput <- function(id) {
  tagList(
    plotOutput(NS(id, "scatterplot"))
  )
}

scatterPlotServer <- function(id,
                              display_histogram = TRUE
)
  {

  moduleServer(id, function(input, output, session) {

    output$scatterplot <- renderPlot({
      if(display_histogram() == FALSE){
        p <- plot(mtcars$mpg, mtcars$hp)
        p
      }

    })

  })
}


myApp <- function(){
  ui <- fluidPage(
    histogramPlotOutput("hist1"),
    scatterPlotOutput("scat1"),
    histogramUIOutput("hist1")
    )

  server <- function(input, output, session) {
    display_hist <- histogramServer("hist1")

    scatterPlotServer("scat1",
                      display_histogram = reactive(display_hist()))
  }
  shinyApp(ui, server)
}

myApp()

提前感謝您的任何幫助!

您可能希望使用renderUI()/uiOutput()來顯示您的任一圖:

myApp <- function(){
  ui <- fluidPage(
    uiOutput("plot"),
    histogramUIOutput("hist1")
  )
  
  server <- function(input, output, session) {
    display_hist <- histogramServer("hist1")
    
    scatterPlotServer("scat1",
                      display_histogram = reactive(display_hist()))

    output$plot <- renderUI({
      if(req(display_hist()))
        histogramPlotOutput("hist1")
      else
        scatterPlotOutput("scat1")
      })
  }
  shinyApp(ui, server)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM