insertUI更改图的大小

时间:2018-09-11 05:51:29

标签: html r shiny

当使用insertUI插入地块时,地块似乎“占据”了一定的边距,不一定是地块的大小。在下面的示例中,一个图小于另一个图,我希望这些图垂直对齐。您是否知道div(“ placeholder”)如何自动将绘图空间调整为实际绘图大小? See the problem in this image.

library(shiny)
library(shinydashboard)

### UI

ui <- dashboardPage(
  dashboardHeader(),

  dashboardSidebar(
    actionButton("add", "Add"),
    radioButtons("add_elements", "Elements", c("Element1",  "Element2")),
    actionButton("remove", "Remove")
  ),
  dashboardBody(
    fluidRow(
      tags$div(id="placeholder")
    )
  )
)

#### SERVER


server <- function(input, output, session) {

  # Test data set

  a<-(letters)
  b<-rnorm(length(letters), 4,2)
  c<-rnorm(length(letters), 10,15)
  d<-c(1:10,20:30,45:49)

  data<-data.frame(a,b,c,d)
  names(data)<-c("name","v1","v2","v3")

  # Initialize empty vector

  inserted<- c()

  ### Elements ###

  observeEvent(input$add, {
    id_add <- paste0(input$add, input$add_elements)
    insertUI(selector = '#placeholder', where = "afterEnd",
             ui= switch(input$add_elements,
                        'Element1'= plotOutput(id_add),
                        'Element2' = plotOutput(id_add))
    )

    output[[id_add]] <- 
      if (input$add_elements == "Element1") renderPlot({ 
        plot(data[,1],data[,2])
      }, height=200) # this plot is smaller
    else if (input$add_elements == "Element2") renderPlot({
      plot(data[,1],data[,4])
    })
    inserted <<- c(id_add,inserted)
  })

  ### Remove Elements ###
  observeEvent(input$remove, {
    removeUI(
      ## pass in appropriate div id
      selector = paste0('#', inserted[length(inserted)])
    )
    inserted <<- inserted[-length(inserted)]
  })

}

shinyApp(ui = ui, server = server)

0 个答案:

没有答案