The provided code implements a basic online banking system using R and Shiny.
Here's a brief description of its components
User Interface (UI): The UI is created using Shiny's UI functions. It includes input fields for the user to enter their account details and select banking operations like deposit, withdrawal, and balance inquiry.
Server Logic: The server logic is implemented using Shiny's server functions. It handles user inputs; performs banking operations based on the user requests and updates the UI with results.
Features:
Deposit: Users can deposit funds into their account by entering the deposit amount and clicking the "Deposit" button.
Withdrawal: Users can withdraw funds from their account by entering the withdrawal amount and clicking the "Withdraw" button. The system checks for a sufficient balance before processing the withdrawal.
Balance Inquiry: Users can check their account balance by clicking the "Check Balance" button.
Example
library(shiny)
# Simulated user data
users <- data.frame(
username = c("user1", "user2", "user3"),
password = c("password1", "password2", "password3"),
balance = c(1000, 2000, 500)
)
ui <- fluidPage(
titlePanel("The Online Banking System"),
sidebarLayout(
sidebarPanel(
textInput("username", "Username"),
passwordInput("password", "Password"),
actionButton("login", "Login")
),
mainPanel(
textOutput("login_message"),
conditionalPanel(
condition = "input.login",
textInput("recipient", "Recipient"),
numericInput("amount", "Amount", value = 0),
actionButton("transfer", "Transfer"),
verbatimTextOutput("transaction_message")
)
)
)
)
# Server logic
server <- function(input, output, session) {
# Track logged in user
loggedInUser <- reactiveVal(NULL)
# Login action
observeEvent(input$login, {
user <- users[users$username == input$username & users$password == input$password, ]
if (nrow(user) == 1) {
loggedInUser(user$username)
output$login_message <- renderText({
paste("Welcome, ", user$username, "!")
})
} else {
output$login_message <- renderText("Invalid username or password")
}
})
# The Transfer the action
observeEvent(input$transfer, {
if (!is.null(loggedInUser())) {
recipient <- input$recipient
amount <- input$amount
user <- users[users$username == loggedInUser(), ]
recipient_user <- users[users$username == recipient, ]
if (nrow(recipient_user) == 1 && amount > 0 && user$balance >= amount) {
users[users$username == loggedInUser(), "balance"] <- user$balance - amount
users[users$username == recipient, "balance"] <- recipient_user$balance + amount
output$transaction_message <- renderPrint({
paste("The Transfer successful! New balance:", users[users$username == loggedInUser(), "balance"])
})
} else {
output$transaction_message <- renderPrint("Transfer failed")
}
}
})
}
# Run
shinyApp(ui, server)
Output