How to add tabset to table position in this application using Shiny R?

27 Views Asked by At

I'm trying to add a tabset so that only tables have multiple tabs. I'm not entirely sure how to do this without the barplot changing positions. I want the bar plot to be in the middle but the tables to be on the left the bar chart. Anybody know how to do this?

I'm now having trouble with elements of the barplot overlapping. Is there anyway to fix this?

Here is my code:

library(shiny)
library(ggplot2)
library(DT)

# Define UI
ui <- library(shiny)
library(ggplot2)
library(DT)

# Define UI
ui <- fluidPage(
  tags$head(
    tags$style(HTML("
      .container-fluid {
  padding-left: 0px !important;
}
.dataTables_paginate {
  white-space: nowrap;
}
.dataTables_wrapper .dataTables_paginate {
  float: none !important;
}
.dataTables_wrapper .dataTables_paginate .paginate_button {
  margin-left: 0px !important;
}
    "))
  ),
  sidebarLayout(
    sidebarPanel(
      selectInput("variable", "Select Variable:", choices = c("setosa", "versicolor", "virginica"), selected = "A"),
      sliderInput("range", "Select Range:", min = 4.3, max = 7.9, value = c(4.3, 7.9))
    ),
    mainPanel(
      fluidRow(
        column(width = 8,
               plotOutput("barplot", height = "300px")
        ),
        column(width = 4,
               tabsetPanel(
                 tabPanel("Table 1", DTOutput("table1")),
                 tabPanel("Table 2", DTOutput("table2"))
               )
        )
      )
    )
  )
)

# Define server
server <- function(input, output) {
  
  # Generate input data based on user selections
  input_data <- reactive({
    iris[iris$Species == input$variable & iris$Sepal.Length >= input$range[1] & iris$Sepal.Length <= input$range[2], ]
  })
  
  # Render bar plot
  output$barplot <- renderPlot({
    ggplot(input_data(), aes(x = Species, fill = Species)) +
      geom_bar() +
      labs(title = "Bar Plot")
  })
  
  # Render first table
  output$table1 <- renderDT({
    datatable(input_data(), options = list(pageLength = 5))
  })
  
  # Render second table
  output$table2 <- renderDT({
    datatable(input_data(), options = list(pageLength = 5))
  })
}

# Run the app
shinyApp(ui, server)
0

There are 0 best solutions below