Skip to content

Commit

Permalink
Merge pull request #23 from NorwegianVeterinaryInstitute/trish-modify
Browse files Browse the repository at this point in the history
Add modify and delete options
  • Loading branch information
trishangu authored Sep 27, 2024
2 parents 88a290c + 4dca307 commit ff0b6fa
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions mod_sidebar.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ sidebarUI <- function(id) {
ns <- NS(id)

tagList(
# useshinyjs() to expand or collapse input list
useShinyjs(),
# Add radio buttons
radioButtons(ns('select'), 'Select Option:',
choices = list("Add a new cake entry" = 1, "Modify an entry" = 2, "Delete an entry" = 3),
selected = 1),
dateInput(ns('date'), 'Select Date:', value = Sys.Date()),
timeInput(ns("hour"), "Time:", value = strptime("12:00:00", "%T"), minute.steps = 10),
selectInput(
Expand Down Expand Up @@ -65,10 +71,25 @@ sidebarServer <- function(id, board) {
moduleServer(id, function(input, output, session) {
ns <- session$ns

# Observe the radio button (select) to expand or collapse input list
observeEvent(input$select, {
if(input$select == 3) {
shinyjs::hide("hour")
shinyjs::hide("room")
shinyjs::hide("section")
shinyjs::hide("cake_desc")
} else {
shinyjs::show("hour")
shinyjs::show("room")
shinyjs::show("section")
shinyjs::show("cake_desc")
}
})

input_data <- reactive({
data.frame(
`Date` = input$date,
`Hour` = input$hour,
`Hour` = (strftime(input$hour, "%R")),
`Room` = input$room,
`Section` = input$section,
`Person.Name` = input$person,
Expand All @@ -90,7 +111,37 @@ sidebarServer <- function(id, board) {
pinned_cakes <- pin_read(board,
name = paste0(Sys.getenv("USER_NAME"), '/cake_user_inputs'))

updated_cakes <- rbind(pinned_cakes, input_data())
# See if the entry already exists
pinned_cakes_exists <- pinned_cakes |>
dplyr::filter(Secret.Ingredient == input$sec_in & Person.Name == input$person & Date == input$date)

# If the user chooses to modify
if(input$select == 2) {
# If the entry is not found, do not change anything
if (nrow(pinned_cakes_exists) == 0) {
updated_cakes <- pinned_cakes
} else { # If entry is found, update the table
# Remove previous entry
pinned_cakes <- pinned_cakes |>
dplyr::filter(Secret.Ingredient != input$sec_in | Person.Name != input$person | Date != input$date)
# Replace with new entry
updated_cakes <- rbind(pinned_cakes, input_data())
}
} else if (input$select == 3) { # If the user chooses to delete
if (nrow(pinned_cakes_exists) == 0) { # If entry does not exists, do nothing
updated_cakes <- pinned_cakes
} else { # If entry exists, remove the entry
pinned_cakes <- pinned_cakes |>
dplyr::filter(Secret.Ingredient != input$sec_in | Person.Name != input$person | Date != input$date)
updated_cakes <- pinned_cakes
}
} else { # If the user chooses to add an entry
if (nrow(pinned_cakes_exists) != 0) { # If entry already exists, do not do anything
updated_cakes <- pinned_cakes
} else { # If entry does not exist, add the entry
updated_cakes <- rbind(pinned_cakes, input_data())
}
}

# Save the input data frame to the pin board
pin_write(
Expand All @@ -101,6 +152,7 @@ sidebarServer <- function(id, board) {
)

# Clear the inputs after submission
updateRadioButtons(session, "select", selected = 1)
updateDateInput(session, "date", value = Sys.Date())
updateTimeInput(session, "hour", value = strptime("12:00:00", "%T"))
updateSelectInput(session, "room", selected = NULL)
Expand Down

0 comments on commit ff0b6fa

Please sign in to comment.