如何在闪亮的r中并排放置多个情节?

时间:2022-05-07 06:07:09

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处理这个问题。然而,我的其中一个情节是可选的,用户可以显示或不显示。当用户单击按钮时,第二个图出现在第一个图的下面。

   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.

我玩的是“align”和“widths”,但什么都没变。

3 个解决方案

#1


6  

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.

因此,两年后的今天,尽管其他人的答案——包括我的答案——仍然有效,但我不建议今天就这么做。今天我要用网格把它放出来。从gridExtra包进行排列。

  • 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).
  • 它允许任意数量的图,并且可以把它们放在网格棋盘上。(我错误地认为splitLayout只使用了两个)。
  • 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.
  • 它最终更容易使用,即使是对于两个图,因为在UI中布局非常繁琐——当屏幕大小发生变化时,很难预测引导程序将如何处理元素。
  • 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.

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

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中并排放置多个情节?

#2


26  

Using @Mike Wise example, you can also use splitLayout(cellWidths = c("50%", "50%")...to show two plots side by side.

使用@Mike Wise示例,您还可以使用splitLayout(cellWidths = c(“50%”,“50%”)……并列显示两个情节。

ui..R

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

server.R

shinyServer(function(input, output) 
{
  set.seed(1234)
  pt1 <- qplot(rnorm(500),fill=I("red"),binwidth=0.2,title="plotgraph1")
  pt2 <- reactive({
    input$do2
    if (input$do2){
      return(qplot(rnorm(500),fill=I("blue"),binwidth=0.2,title="plotgraph2"))
    } else {
      return(NULL)
    }
  })
  output$plotgraph1 = renderPlot({pt1})
  output$plotgraph2 = renderPlot({pt2()})
}
)

you can also play with the numbers the figure below shows c("60%", "40%")

您还可以使用下图所示的数字c(“60%”、“40%”)

如何在闪亮的r中并排放置多个情节?

EDIT: It is true that @Mike Wise new answer's gives some flexibility. But splitLayout can also be used with more than two plots. Using cellWidths allows you to change the size of each individual plot. And verticalLayout() can also be used to add plots vertically (see comment section).

编辑:的确,@Mike Wise的新答案提供了一些灵活性。但是splitLayout也可以用于两个以上的情节。使用cellWidths允许您更改每个单独的图的大小。垂直布局()也可以用于垂直添加情节(请参阅注释部分)。

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"),binwidth=0.2,title="plotgraph1")
  pt3 <- qplot(rnorm(600),fill=I("blue"),binwidth=0.2,title="plotgraph3")
  pt2 <- reactive({
    input$do2
    if (input$do2){
      return(qplot(rnorm(500),fill=I("blue"),binwidth=0.2,title="plotgraph2"))
    } else {
      return(NULL)
    }
  })
  output$plotgraph1 = renderPlot({pt1})
  output$plotgraph2 = renderPlot({pt2()})
  output$plotgraph3 = renderPlot({pt3}
  )
})

shinyApp(u,s)

如何在闪亮的r中并排放置多个情节?

#3


5  

Well, you did not exactly give us a complete example, but I think this is what you want:

你没有给我们一个完整的例子,但我认为这就是你想要的:

ui.r

# 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", width="300px",height="300px")),  
          column(6,plotOutput(outputId="plotgraph2", width="300px",height="300px"))
        )
      )
    )
  )
)

server.r

# server.r

library(ggplot2)

shinyServer(function(input, output) 
  {
  set.seed(1234)
  pt1 <- qplot(rnorm(500),fill=I("red"),binwidth=0.2,title="plotgraph1")
    pt2 <- reactive({
      input$do2
      if (input$do2){
        return(qplot(rnorm(500),fill=I("blue"),binwidth=0.2,title="plotgraph2"))
      } else {
        return(NULL)
      }
    })
    output$plotgraph1 = renderPlot({pt1})
    output$plotgraph2 = renderPlot({pt2()})
  }
)

Output

"Make 2 plots" checked:

如何在闪亮的r中并排放置多个情节?

"Make 2 plots" unchecked:

如何在闪亮的r中并排放置多个情节?

#1


6  

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.

因此,两年后的今天,尽管其他人的答案——包括我的答案——仍然有效,但我不建议今天就这么做。今天我要用网格把它放出来。从gridExtra包进行排列。

  • 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).
  • 它允许任意数量的图,并且可以把它们放在网格棋盘上。(我错误地认为splitLayout只使用了两个)。
  • 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.
  • 它最终更容易使用,即使是对于两个图,因为在UI中布局非常繁琐——当屏幕大小发生变化时,很难预测引导程序将如何处理元素。
  • 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.

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

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中并排放置多个情节?

#2


26  

Using @Mike Wise example, you can also use splitLayout(cellWidths = c("50%", "50%")...to show two plots side by side.

使用@Mike Wise示例,您还可以使用splitLayout(cellWidths = c(“50%”,“50%”)……并列显示两个情节。

ui..R

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

server.R

shinyServer(function(input, output) 
{
  set.seed(1234)
  pt1 <- qplot(rnorm(500),fill=I("red"),binwidth=0.2,title="plotgraph1")
  pt2 <- reactive({
    input$do2
    if (input$do2){
      return(qplot(rnorm(500),fill=I("blue"),binwidth=0.2,title="plotgraph2"))
    } else {
      return(NULL)
    }
  })
  output$plotgraph1 = renderPlot({pt1})
  output$plotgraph2 = renderPlot({pt2()})
}
)

you can also play with the numbers the figure below shows c("60%", "40%")

您还可以使用下图所示的数字c(“60%”、“40%”)

如何在闪亮的r中并排放置多个情节?

EDIT: It is true that @Mike Wise new answer's gives some flexibility. But splitLayout can also be used with more than two plots. Using cellWidths allows you to change the size of each individual plot. And verticalLayout() can also be used to add plots vertically (see comment section).

编辑:的确,@Mike Wise的新答案提供了一些灵活性。但是splitLayout也可以用于两个以上的情节。使用cellWidths允许您更改每个单独的图的大小。垂直布局()也可以用于垂直添加情节(请参阅注释部分)。

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"),binwidth=0.2,title="plotgraph1")
  pt3 <- qplot(rnorm(600),fill=I("blue"),binwidth=0.2,title="plotgraph3")
  pt2 <- reactive({
    input$do2
    if (input$do2){
      return(qplot(rnorm(500),fill=I("blue"),binwidth=0.2,title="plotgraph2"))
    } else {
      return(NULL)
    }
  })
  output$plotgraph1 = renderPlot({pt1})
  output$plotgraph2 = renderPlot({pt2()})
  output$plotgraph3 = renderPlot({pt3}
  )
})

shinyApp(u,s)

如何在闪亮的r中并排放置多个情节?

#3


5  

Well, you did not exactly give us a complete example, but I think this is what you want:

你没有给我们一个完整的例子,但我认为这就是你想要的:

ui.r

# 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", width="300px",height="300px")),  
          column(6,plotOutput(outputId="plotgraph2", width="300px",height="300px"))
        )
      )
    )
  )
)

server.r

# server.r

library(ggplot2)

shinyServer(function(input, output) 
  {
  set.seed(1234)
  pt1 <- qplot(rnorm(500),fill=I("red"),binwidth=0.2,title="plotgraph1")
    pt2 <- reactive({
      input$do2
      if (input$do2){
        return(qplot(rnorm(500),fill=I("blue"),binwidth=0.2,title="plotgraph2"))
      } else {
        return(NULL)
      }
    })
    output$plotgraph1 = renderPlot({pt1})
    output$plotgraph2 = renderPlot({pt2()})
  }
)

Output

"Make 2 plots" checked:

如何在闪亮的r中并排放置多个情节?

"Make 2 plots" unchecked:

如何在闪亮的r中并排放置多个情节?