PiTao
PiTao

Reputation: 19

Two different settings and have results side by side in Shiny

I am trying to display multiple columns with multiple input options in Shiny. In the following codes, I can select one option for ctree depth and plot the tree as well as the data table. Now, I want to compare that with another depth input. How can I duplicate the results for another depth (such as depth = 2) and plot it next to the existing plot? I want something like the picture I show here image. Any help would be appreciated.

library(shiny)
library(party)
airq <- subset(airquality, !is.na(Ozone))

ui <- fluidPage(
  
  sidebarPanel(selectInput("max", label="depth", choices=list("2" = "2", "3" = "3", "4" = "4"), 
                           selected=list("3"))),
  
  mainPanel(
    plotOutput("myPlot"),
    tableOutput("myTable")
  )
)

server <- function(input, output, session) {

 fit <- reactive({
   ctree(Ozone ~ ., data = airq, 
         controls = ctree_control(maxdepth = input$max))
 })
output$myPlot = renderPlot({
    plot(fit())
  })
  
  output$myTable = renderTable({
   data 
  })
}

shinyApp(ui, server)

Upvotes: 1

Views: 185

Answers (1)

ViviG
ViviG

Reputation: 1726

You can use the width and column to achieve that. I used the dataset iris for my example as yours was not reproducible. Inside the function, width will control the row width, and column will control column width. Both widths go from 1 to 12. Divide 12 by the number of columns you wish in that row :

library(shiny)
library(party)
airq <- subset(airquality,!is.na(Ozone))

ui <- fluidPage(sidebarPanel(width = 12, column(
  6,
  selectInput(
    "max",
    label = "depth",
    choices = list("2" = "2", "3" = "3", "4" = "4"),
    selected = list("3")
  )
),
column(
  6,
  selectInput(
    "max2",
    label = "depth2",
    choices = list("2" = "2", "3" = "3", "4" = "4"),
    selected = list("2")
  )
)),



mainPanel(
  width = 12,
  column(6,
         plotOutput("myPlot"),
         
         tableOutput("myTable")),
  column(6,
         plotOutput("myPlot2"),
         
         tableOutput("myTable2"))
))




server <- function(input, output, session) {
  fit <- reactive({
    ctree(Ozone ~ .,
          data = airq,
          controls = ctree_control(maxdepth = input$max))
  })
  output$myPlot = renderPlot({
    plot(iris)
  })
  
  output$myTable = renderTable({
    iris
  })
  output$myPlot2 = renderPlot({
    plot(iris)
  })
  
  output$myTable2 = renderTable({
    iris
  })
}

shinyApp(ui, server)

enter image description here

Upvotes: 1

Related Questions