基于情节数量的闪亮情节区域

Posted

技术标签:

【中文标题】基于情节数量的闪亮情节区域【英文标题】:Shiny plot region based on number of plot 【发布时间】:2018-01-31 10:20:57 【问题描述】:

基本上我要做的是我的用户将选择一个基因,并且取决于基因将取决于情节的数量,范围从 1 到 10 个情节。

我希望能够根据要绘制的绘图数量来解决 UI 绘图宽度这可能吗?

希望下面的代码能帮助说明我的问题,循环遍历“基因”A、B、C 和 D

library(shiny)
library(shinydashboard)

dat = data.frame(gene =c(rep("A",3), rep("B",6), rep("C",1), rep("D",10)), sample1 = runif(10, 0,50), sample2 = runif(10, 0,50),
                 sample3 = runif(10, 0,50),sample4 = runif(10, 0,50),sample5 = runif(10, 0,50),sample6 = runif(10, 0,50))

ui <- dashboardPage(
  dashboardHeader(title = "Title", titleWidth = "300px"),
  dashboardSidebar(
    textInput(inputId = "GoI", label = "Select gene of interest(A,B)", value ="A")),

  dashboardBody(
    tabsetPanel(
      tabPanel("Differential Variability",plotOutput("boxplot"))

    )))      

server <- function(input, output)


  output$boxplot <- renderPlot(dataGoI = dat[dat$gene == input$GoI,]
  i = nrow(dat[dat$gene == input$GoI,])
  g = c(0,0,0,1,1,1)
  if (i > 5)
    if (i == 6)
      zones = matrix(c(1:6), nrow=2, byrow =T)
    else if (i == 7)
      zones = matrix(c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7), nrow=2, byrow =T)
     else if (i == 8)
      zones = matrix(c(1:8), nrow=2, byrow =T)
     else if (i == 9)
      zones = matrix(c(rep(1:5, each=4), rep(6:9,each=5)), nrow =2, byrow =T)
     else
      zones = matrix(c(1:10),nrow=2, byrow=T)
    
  

  if( i <= 5)
    par(mfrow = c(1, i))
    for(j in 1:i)
      boxplot(as.numeric(dataGoI[j,2:7])~g)
    
  else 
    layout(zones)
    for(j in 1:i)
      boxplot(as.numeric(dataGoI[j,2:7])~g)
    
  ) 


shinyApp(ui = ui, server = server)

【问题讨论】:

【参考方案1】:

不确定您想要的确切布局,但我建议您使用 renderUI 服务器端和 uiOutput UI 端。基于您的代码的简单示例将是这样的

library(shiny)
library(shinydashboard)

dat = data.frame(gene =c(rep("A",3), rep("B",6), rep("C",1), rep("D",10)), sample1 = runif(10, 0,50), sample2 = runif(10, 0,50),
                 sample3 = runif(10, 0,50),sample4 = runif(10, 0,50),sample5 = runif(10, 0,50),sample6 = runif(10, 0,50))

ui <- dashboardPage(
  dashboardHeader(title = "Title", titleWidth = "300px"),
  dashboardSidebar(
    textInput(inputId = "GoI", label = "Select gene of interest(A,B)", value ="A")),

  dashboardBody(
    tabsetPanel(
      tabPanel("Differential Variability",uiOutput("boxplot_ui"))

    )))      

server <- function(input, output)


  output$boxplot <- renderPlot(dataGoI = dat[dat$gene == input$GoI,]
  i = nrow(dat[dat$gene == input$GoI,])
  g = c(0,0,0,1,1,1)
  if (i > 5)
    if (i == 6)
      zones = matrix(c(1:6), nrow=2, byrow =T)
    else if (i == 7)
      zones = matrix(c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7), nrow=2, byrow =T)
     else if (i == 8)
      zones = matrix(c(1:8), nrow=2, byrow =T)
     else if (i == 9)
      zones = matrix(c(rep(1:5, each=4), rep(6:9,each=5)), nrow =2, byrow =T)
     else
      zones = matrix(c(1:10),nrow=2, byrow=T)
    
  

  if( i <= 5)
    par(mfrow = c(1, i))
    for(j in 1:i)
      boxplot(as.numeric(dataGoI[j,2:7])~g)
    
  else 
    layout(zones)
    for(j in 1:i)
      boxplot(as.numeric(dataGoI[j,2:7])~g)
    
  )

  output$boxplot_ui <- renderUI(plotOutput("boxplot",width = nrow(dat[dat$gene == input$GoI,])*100))


shinyApp(ui = ui, server = server)

这里的 UI 宽度将随着绘图的数量而移动。您当然可以根据您的具体需要调整宽度。如您所见,在处理更改 A、B、C、D 的过程中可能会发生错误,因此您可能需要指定 nrow 为空时要采用的宽度

【讨论】:

这正是我所追求的。

以上是关于基于情节数量的闪亮情节区域的主要内容,如果未能解决你的问题,请参考以下文章

有没有办法使用 plotly 开始一个已经在特定区域放大的情节?

没有在闪亮的情节中显示情节

闪亮的反应情节失败

在闪亮上动态添加情节痕迹

带情节的闪亮仪表板

在闪亮的应用程序中抑制情节警告