基于绘图数量的闪亮绘图区域

时间:2017-08-23 01:39:16

标签: r shiny

基本上我要做的是我的用户将选择一个基因并依赖于基因将取决于绘图的数量,范围从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 个答案:

答案 0 :(得分: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为空时要采用的宽度