SlothXavier
SlothXavier

Reputation: 1

Shiny App Only Filling 1/3 of the screen and has scrollbar

I am trying to make a shiny app dashboard and after I run my code it only fills up half of the screen. This leaves my page having a scroll bar when it doesn't fill up the page at all. Here is the result of my code:

enter image description here

I tried making the boxes have a bigger height but that didn't work. I also tried added a second fluidrow and that didn't work. This leaves my dashboard with a scrollbar on a page that doesn't fill the screen. Below is my code I have let me know where to change it. This is all being run in the ui.R page.

library(shiny)
library(shinydashboard)
library(ggplot2)
library(dplyr)

# Sample Data
set.seed(123)
data <- data.frame(
  Date = seq.Date(Sys.Date() - 30, Sys.Date(), by = "day"),
  Facility = rep(c("Facility A", "Facility B", "Facility C"), each = 31),
  Product = rep(c("Product 1", "Product 2", "Product 3"), each = 31),
  Parameter = rep(c("Parameter 1", "Parameter 2", "Parameter 3"), each = 31),
  Value = rnorm(93, mean = 50, sd = 10)
)

# Define UI for the entire application
ui <- uiOutput("main_ui")  # Root UI output

# Define Server Logic
server <- function(input, output, session) {
  
  # Dummy credentials for users
  credentials <- data.frame(
    username = c("Bill", "Joey", "William"),
    password = c("HBe_0R$n!<(oX_O", "0ZhlF43Vl^Hzk#H", "7BR909)**&=dmks"),
    stringsAsFactors = FALSE
  )
  
  # Reactive value to track login status
  user_logged_in <- reactiveVal(FALSE)
  
  # Login logic
  observeEvent(input$login, {
    valid_user <- input$username %in% credentials$username
    valid_pass <- input$password == credentials$password[match(input$username, credentials$username)]
    if (valid_user && valid_pass) {
      user_logged_in(TRUE)
    } else {
      user_logged_in(FALSE)
      output$login_msg <- renderText("Invalid username or password.")
    }
  })
  
  # Render the appropriate UI
  output$main_ui <- renderUI({
    if (user_logged_in()) {
      mainPage
    } else {
      loginPage
    }
  })
  
  # UI Outputs for the main app
  output$facilitySelect <- renderUI({
    selectInput("facility", "Select Facility:", choices = unique(data$Facility))
  })
  
  output$productSelect <- renderUI({
    selectInput("product", "Select Product:", choices = unique(data$Product))
  })
  
  output$parameterSelect <- renderUI({
    selectInput("parameter", "Select Parameter:", choices = unique(data$Parameter))
  })
  
  output$selectedValues <- renderText({
    req(input$dateRange, input$facility, input$product, input$parameter)
    paste("Date Range:", input$dateRange[1], "to", input$dateRange[2], "\n",
          "Selected Facility:", input$facility, "\n",
          "Selected Product:", input$product, "\n",
          "Selected Parameter:", input$parameter)
  })
  
  # Reactive filtered data
  filtered_data <- reactive({
    req(input$dateRange, input$facility, input$product, input$parameter)
    data %>%
      filter(Date >= input$dateRange[1] & Date <= input$dateRange[2],
             Facility == input$facility,
             Product == input$product,
             Parameter == input$parameter)
  })
  
  # Render the plot
  output$dynamicPlot <- renderPlot({
    req(filtered_data())
    ggplot(filtered_data(), aes(x = Date, y = Value)) +
      geom_line() +
      labs(title = "Dynamic Plot", x = "Date", y = "Value") +
      theme_minimal()
  })
}

# Define Login Page UI
loginPage <- dashboardPage(
  dashboardHeader(title = "Login"),
  dashboardSidebar(disable = TRUE),
  dashboardBody(
    fluidRow(
      box(
        title = "Login", status = "primary", solidHeader = TRUE, width = 4, 
        style = "height: 300px; margin: auto;", 
        textInput("username", "Username"),
        passwordInput("password", "Password"),
        actionButton("login", "Login"),
        textOutput("login_msg")
      )
    )
  )
)

# Define Main Application UI
mainPage <- dashboardPage(
  dashboardHeader(title = "Reactive App"),
  dashboardSidebar(disable = TRUE),
  dashboardBody(
    fluidRow(
      box(title = "Date Range Input", width = 6,
          dateRangeInput("dateRange", "Select Date Range:",
                         start = Sys.Date() - 30, 
                         end = Sys.Date())
      ),
      box(title = "Select Options", width = 6,
          uiOutput("facilitySelect"),
          uiOutput("productSelect"),
          uiOutput("parameterSelect")
      )),fluidRow(
      box(title = "Plot", width = 12,
          plotOutput("dynamicPlot")
      ),
      box(title = "Selected Values", width = 12,
          textOutput("selectedValues")
      ))
    )
  )


# Run the Application
shinyApp(ui = ui, server = server)

Upvotes: 0

Views: 67

Answers (0)

Related Questions