如何将多个阴谋并排放在闪亮的r? [英] How can put multiple plots side-by-side in shiny r?

查看:306
本文介绍了如何将多个阴谋并排放在闪亮的r?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在mainpanel中,我尝试通过流水线来处理这个问题。但是,我的情节之一是可选的,以供用户显示或不显示。当用户点击按钮时,第二个图出现在第一个图的下面。

  fluidRow(
column(2,align =right,
plotOutput(outputId =plotgraph1,width =500px,height =400px),
plotOutput(outputId =plotgraph2,width =500px,height = 400px)
))

我玩过align和widths但没有任何改变。

所以这是几年后,而其他人的答案 - 包括我的 - 仍然有效,我不建议今天接触它。今天,我将使用 gridExtra 包中的 grid.arrange 进行布局。


  • 它允许任意数量的地块,并且可以将它们放置在网格棋盘状的地方。 (我错误地认为 splitLayout 只能用于两个)

  • 它有更多的自定义可能性(您可以指定行,列,页眉,页脚,填充等)

  • 即使对于两个图,它最终也易于使用,因为在UI中布局非常挑剔 - 可能难以预测Bootstrap会在屏幕尺寸发生变化时处理您的元素。

  • 由于此问题获得了大量流量,因此我认为应该在这里提供更多选择。



cowplot 包也值得研究,它提供了类似的功能,但我对它并不是很熟悉。



下面是一个小小的闪亮程序,演示:

  library(闪亮)
库(ggplot2)
库(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,
sliderInput(wt3,Weight 3,min = 1,max = 10,value = 1)
)Weight 2,min = 1,max = 10,value = 1) ,
mainPanel(main panel,
column(6,plotOutput(outputId =plotgraph,width =500px,height =400px))
))))
$ bs< - shinyServer(函数(输入,输出)
{
set.seed(123)
pt1 < - reactive({
if( !输入$ donum1)return(NULL)
qplot(rnorm(500),fill = I(red),binwidth = 0.2,main =plotgraph1)
})
pt2 < - reactive({
if(!input $ donum2)return(NULL)
qplot(rnorm(500),fill = I(blue),binwidth = 0.2,main =plotgraph2 )
})
pt3 < - reactive({
if(!input $ donum3)return(NULL)
qplot(rnorm(500),fill = I(green),binwidth = 0.2,main =plotgraph3)
})
output $ plotgraph = renderPlot({
ptlist< - list(pt1(),pt2(),pt3())
wtlist< -c(输入$ wt1,输入$ wt2,输入$ wt3)
# from ptlist and wtlist
to_delete< - !sapply(ptlist,is.null)
ptlist< - ptlist [to_delete]
wtlist< - wtlist [to_delete]
if (长度(ptlist)== 0)返回(NULL)

grid.arrange(grobs = ptlist,widths = wtlist,ncol = length(ptlist))
})
})
shinyApp(u,s)

收益率:


In mainpanel, I try to handle this problem via fluidrow. However, one of my plot is optional to be displayed or not by users. When user clicks the button, the second plot appears below the first plot.

   fluidRow(
      column(2, align="right",
             plotOutput(outputId = "plotgraph1", width  = "500px",height = "400px"),  
             plotOutput(outputId = "plotgraph2", width  = "500px",height = "400px")
      ))

I played with "align" and "widths", but nothing changed.

解决方案

So it is a couple years later, and while the others answers - including mine - are still valid, it is not how I would recommend approaching it today. Today I would lay it out using the grid.arrange from the gridExtra package.

  • It allows any number of plots, and can lay them out in a grid checkerboard-like. (I was erroneously under the impression splitLayout only worked with two).
  • It has more customization possibilities (you can specify rows, columns, headers, footer, padding, etc.)
  • It is ultimately easier to use, even for two plots, since laying out in the UI is finicky - it can be difficult to predict what Bootstrap will do with your elements when the screen size changes.
  • Since this question gets a lot of traffic, I kind of think more alternative should be here.

The cowplot package is also worth looking into, it offers similar functionality, but I am not so familiar with it.

Here is a small shiny program demonstrating that:

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", width="500px",height="400px"))
))))

s <- shinyServer(function(input, output) 
{
  set.seed(123)
  pt1 <- reactive({
    if (!input$donum1) return(NULL)
    qplot(rnorm(500),fill=I("red"),binwidth=0.2,main="plotgraph1")
    })
  pt2 <- reactive({
    if (!input$donum2) return(NULL)
    qplot(rnorm(500),fill=I("blue"),binwidth=0.2,main="plotgraph2")
  })
  pt3 <- reactive({
    if (!input$donum3) return(NULL)
    qplot(rnorm(500),fill=I("green"),binwidth=0.2,main="plotgraph3")
  })
  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)

Yielding:

这篇关于如何将多个阴谋并排放在闪亮的r?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆