在主面板中,我试图通过fluidrow处理这个问题.但是,我的一个绘图是可选的,用户可以显示或不显示.当用户单击该按钮时,第二个图显示在第一个图下方.
fluidRow( column(2, align="right", plotOutput(outputId = "plotgraph1", width = "500px",height = "400px"), plotOutput(outputId = "plotgraph2", width = "500px",height = "400px") ))
我玩"对齐"和"宽度",但没有改变.
使用@Mike Wise示例,您还可以使用splitLayout(cellWidths = c("50%","50%")...并排显示两个图.
ui..R
library(shiny) shinyUI(fluidPage( titlePanel("title panel"), sidebarLayout(position = "left", sidebarPanel("sidebar panel", checkboxInput("do2", "Make 2 plots", value = T) ), mainPanel("main panel", fluidRow( splitLayout(cellWidths = c("50%", "50%"), plotOutput("plotgraph1"), plotOutput("plotgraph2")) ) ) ) ) )
server.R
shinyServer(function(input, output) { set.seed(1234) pt1 <- qplot(rnorm(500),fill=I("red"),binplotgraph1") pt2 <- reactive({ input$do2 if (input$do2){ return(qplot(rnorm(500),fill=I("blue"),binplotgraph2")) } else { return(NULL) } }) output$plotgraph1 = renderPlot({pt1}) output$plotgraph2 = renderPlot({pt2()}) } )
你也可以玩数字,下图显示c("60%","40%")
编辑:@Mike Wise的新答案确实提供了一些灵活性.但splitLayout
也可以使用两个以上的情节.使用cellWidths
允许您更改每个单独绘图的大小.并且verticalLayout()
还可以用于垂直添加绘图(请参阅注释部分).
library(shiny) library(ggplot2) u<- shinyUI(fluidPage( titlePanel("title panel"), sidebarLayout(position = "left", sidebarPanel("sidebar panel", checkboxInput("do2", "Make 2 plots", value = T) ), mainPanel("main panel", fluidRow( splitLayout(style = "border: 1px solid silver:", cellWidths = c(300,200,100), plotOutput("plotgraph1"), plotOutput("plotgraph2"), plotOutput("plotgraph3") ) ) ) ) ) ) s <- shinyServer(function(input, output){ set.seed(1234) pt1 <- qplot(rnorm(500),fill=I("red"),binplotgraph1") pt3 <- qplot(rnorm(600),fill=I("blue"),binplotgraph3") pt2 <- reactive({ input$do2 if (input$do2){ return(qplot(rnorm(500),fill=I("blue"),binplotgraph2")) } else { return(NULL) } }) output$plotgraph1 = renderPlot({pt1}) output$plotgraph2 = renderPlot({pt2()}) output$plotgraph3 = renderPlot({pt3} ) }) shinyApp(u,s)
所以几年之后,虽然其他答案 - 包括我的 - 仍然有效,但我今天不建议接近它.今天,我将使用摊开来grid.arrange
从gridExtra
包.
它允许任意数量的绘图,并且可以将它们放置在网格棋盘格中.(我错误地认为splitLayout
只有两个人合作).
它有更多的自定义可能性(您可以指定行,列,页眉,页脚,填充等)
它最终更容易使用,即使是两个图,因为在UI中布局很挑剔 - 当屏幕尺寸发生变化时,很难预测Bootstrap会对您的元素做什么.
由于这个问题获得了大量的流量,我认为更多的选择应该在这里.
该cowplot
软件包也值得研究,它提供类似的功能,但我对它并不熟悉.
这是一个小小的闪亮程序,展示了:
library(shiny) library(ggplot2) library(gridExtra) u <- shinyUI(fluidPage( titlePanel("title panel"), sidebarLayout(position = "left", sidebarPanel("sidebar panel", checkboxInput("donum1", "Make #1 plot", value = T), checkboxInput("donum2", "Make #2 plot", value = F), checkboxInput("donum3", "Make #3 plot", value = F), sliderInput("wt1","Weight 1",min=1,max=10,value=1), sliderInput("wt2","Weight 2",min=1,max=10,value=1), sliderInput("wt3","Weight 3",min=1,max=10,value=1) ), mainPanel("main panel", column(6,plotOutput(outputId="plotgraph", ,)) )))) s <- shinyServer(function(input, output) { set.seed(123) pt1 <- reactive({ if (!input$donum1) return(NULL) qplot(rnorm(500),fill=I("red"),binplotgraph1") }) pt2 <- reactive({ if (!input$donum2) return(NULL) qplot(rnorm(500),fill=I("blue"),binplotgraph2") }) pt3 <- reactive({ if (!input$donum3) return(NULL) qplot(rnorm(500),fill=I("green"),binplotgraph3") }) output$plotgraph = renderPlot({ ptlist <- list(pt1(),pt2(),pt3()) wtlist <- c(input$wt1,input$wt2,input$wt3) # remove the null plots from ptlist and wtlist to_delete <- !sapply(ptlist,is.null) ptlist <- ptlist[to_delete] wtlist <- wtlist[to_delete] if (length(ptlist)==0) return(NULL) grid.arrange(grobs=ptlist,widths=wtlist,ncol=length(ptlist)) }) }) shinyApp(u,s)
产量:
好吧,你没有给我们一个完整的例子,但我认为这就是你想要的:
# ui.R shinyUI(fluidPage( titlePanel("title panel"), sidebarLayout(position = "left", sidebarPanel("sidebar panel", checkboxInput("do2", "Make 2 plots", value = T) ), mainPanel("main panel", fluidRow( column(6,plotOutput(outputId="plotgraph1", ,)), column(6,plotOutput(outputId="plotgraph2", ,)) ) ) ) ) )
# server.r library(ggplot2) shinyServer(function(input, output) { set.seed(1234) pt1 <- qplot(rnorm(500),fill=I("red"),binplotgraph1") pt2 <- reactive({ input$do2 if (input$do2){ return(qplot(rnorm(500),fill=I("blue"),binplotgraph2")) } else { return(NULL) } }) output$plotgraph1 = renderPlot({pt1}) output$plotgraph2 = renderPlot({pt2()}) } )产量