Skip to content

Commit

Permalink
Merge pull request #17 from NorwegianVeterinaryInstitute/trish-modify
Browse files Browse the repository at this point in the history
Add modify and delete button
  • Loading branch information
trishangu authored Sep 26, 2024
2 parents abdbcb2 + 5b603b8 commit 74a6008
Showing 1 changed file with 55 additions and 6 deletions.
61 changes: 55 additions & 6 deletions mod_sidebar.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ sidebarUI <- function(id) {
ns <- NS(id)

tagList(
useShinyjs(),
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 @@ -54,6 +58,7 @@ sidebarUI <- function(id) {
"Virologi, immunologi og parasittologi"
)),
textInput(ns('person'), 'Person Name:'),
textInput(ns('sec_in'), 'Secret Ingredient:'),
textAreaInput(ns('cake_desc'), 'Cake Description:', rows = 3),
actionButton(ns('submit'), 'Submit')
)
Expand All @@ -64,14 +69,30 @@ sidebarServer <- function(id, board) {
moduleServer(id, function(input, output, session) {
ns <- session$ns

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,
`Cake Description` = input$cake_desc,
`Person.Name` = input$person,
`Secret.Ingredient` = input$sec_in,
`Cake.Description` = input$cake_desc,
stringsAsFactors = FALSE
)
})
Expand All @@ -83,11 +104,37 @@ sidebarServer <- function(id, board) {
input$room,
input$section,
input$person,
input$cake_desc
input$sec_in
)
pinned_cakes <- pin_read(board, name = paste0('cake_user_inputs'))
pinned_cakes <- pin_read(board,
name = paste0(Sys.getenv("USER_NAME"), '/cake_user_inputs'))

pinned_cakes_exists <- pinned_cakes |>
dplyr::filter(Secret.Ingredient == input$sec_in & Person.Name == input$person & Date == input$date)

updated_cakes <- rbind(pinned_cakes, input_data())
if(input$select == 2) {
if (nrow(pinned_cakes_exists) == 0) {
updated_cakes <- pinned_cakes
} else {
pinned_cakes <- pinned_cakes |>
dplyr::filter(Secret.Ingredient != input$sec_in | Person.Name != input$person | Date != input$date)
updated_cakes <- rbind(pinned_cakes, input_data())
}
} else if (input$select == 3) {
if (nrow(pinned_cakes_exists) == 0) {
updated_cakes <- pinned_cakes
} else {
pinned_cakes <- pinned_cakes |>
dplyr::filter(Secret.Ingredient != input$sec_in | Person.Name != input$person | Date != input$date)
updated_cakes <- pinned_cakes
}
} else {
if (nrow(pinned_cakes_exists) != 0) {
updated_cakes <- pinned_cakes
} else {
updated_cakes <- rbind(pinned_cakes, input_data())
}
}

# Save the input data frame to the pin board
pin_write(
Expand All @@ -98,11 +145,13 @@ 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)
updateSelectInput(session, "section", selected = NULL)
updateTextInput(session, "person", value = "")
updateTextInput(session, "sec_in", value = "")
updateTextAreaInput(session, "cake_desc", value = "")
})

Expand Down

0 comments on commit 74a6008

Please sign in to comment.