Reputation: 903
Basically what I am trying to do is my user will select a gene and depend on the gene will depend on the number of plot, ranges from 1 to 10 plots.
I want to be able to address the UI plot width based on the number of plots to be plotted is this possible?
Hopefully the code below will help illustrate my problem, cycle through 'genes' A,B,C and 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)
Upvotes: 0
Views: 55
Reputation: 450
Not sure the exact layout you want to be conditionnal but I suggest you work with renderUI server side and uiOutput UI side. A simple exemple base on your code will be like this
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)
Here the UI width will move with the number of plot. You can of course adjust the width base on your specific needs. As you can see an error can occur during the processing of the change A,B,C,D, so you may want to specify the width to take if the nrow
is null
Upvotes: 1