library(shiny)
library(DT)
Define UI for login page
loginUI <- fluidPage(
titlePanel("Login"),
sidebarLayout(
sidebarPanel(
textInput("username", "Username:"),
passwordInput("password", "Password:"),
actionButton("login", "Login")
),
mainPanel()
)
)
Define server logic for login page
loginServer <- function(input, output, session) {
observeEvent(input$login, {
# Simplified authentication (replace with actual authentication logic)
if (input$username == "user" && input$password == "password") {
session$user <- input$username # Store username in session
output$page <- renderUI({
productionDataEntryUI()
}) # Render the main application page
} else {
showModal(modalDialog(
title = "Error",
"Invalid username or password. Please try again."
))
}
})
}
Define UI for production data entry
productionDataEntryUI <- fluidPage(
titlePanel("Production Data Entry"),
sidebarLayout(
sidebarPanel(
dateInput("date", "Date:"),
textInput("unit", "Unit:"),
numericInput("production", "Production:", value = 0),
numericInput("expense", "Expense:", value = 0),
numericInput("shipment", "Shipment", value = 0),
numericInput("proceeds", "Proceeds", value = 0),
actionButton("submit", "Submit")
),
mainPanel(
textOutput("status"),
DTOutput("table")
)
)
)
Define server logic for production data entry
productionDataEntryServer <- function(input, output, session) {
Check if user is logged in, if not, render login UI
output$page <- renderUI({
if (is.null(session$user)) {
loginUI
} else {
productionDataEntryUI()
}
})
Initialize reactive values to store data frame
data <- reactiveValues(csv_data = data.frame(Date = character(),
Unit = character(),
Production = numeric(),
Expense = numeric(),
Shipment = numeric(),
Proceeds = numeric()))
observeEvent(input$submit, {
# Create a data frame with the input values
new_entry <- data.frame(Date = format(input$date, "%Y-%m-%d"),
Unit = input$unit,
Production = input$production,
Expense = input$expense,
Shipment = input$shipment,
Proceeds = input$proceeds)
# Update the reactive values with new entry
data$csv_data <- rbind(data$csv_data, new_entry)
# Define the filename
filename <- "data.csv"
# Write the updated data frame to CSV file
write.csv(data$csv_data, file = filename, row.names = FALSE)
# Display a confirmation message
output$status <- renderText({
paste("Data saved to file:", filename)
})
})
Render the CSV data as an editable DataTable
output$table <- renderDT({
datatable(data$csv_data, editable = TRUE)
})
Update the reactive values with the edited data
observeEvent(input$table_cell_edit, {
info <- input$table_cell_edit
str(info)
data$csv_data[info$row, info$col] <- info$value
write.csv(data$csv_data, file = "data.csv", row.names = FALSE)
})
}
Run the application
shinyApp(
ui = fluidPage(
uiOutput("page")
),
server = function(input, output, session) {
output$page <- renderUI({
loginUI
})
callModule(loginServer, "login")
callModule(productionDataEntryServer, "productionDataEntryServer")
}
)
I am trying to create a Login Page for this basic data entry form that I made. However, I am not able to log in after clicking the button. Any assistance would be greatly appreciated. Thanks