Skip to content

Commit 74a6008

Browse files
authored
Merge pull request #17 from NorwegianVeterinaryInstitute/trish-modify
Add modify and delete button
2 parents abdbcb2 + 5b603b8 commit 74a6008

File tree

1 file changed

+55
-6
lines changed

1 file changed

+55
-6
lines changed

mod_sidebar.R

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ sidebarUI <- function(id) {
33
ns <- NS(id)
44

55
tagList(
6+
useShinyjs(),
7+
radioButtons(ns('select'), 'Select Option:',
8+
choices = list("Add a new cake entry" = 1, "Modify an entry" = 2, "Delete an entry" = 3),
9+
selected = 1),
610
dateInput(ns('date'), 'Select Date:', value = Sys.Date()),
711
timeInput(ns("hour"), "Time:", value = strptime("12:00:00", "%T"), minute.steps = 10),
812
selectInput(
@@ -54,6 +58,7 @@ sidebarUI <- function(id) {
5458
"Virologi, immunologi og parasittologi"
5559
)),
5660
textInput(ns('person'), 'Person Name:'),
61+
textInput(ns('sec_in'), 'Secret Ingredient:'),
5762
textAreaInput(ns('cake_desc'), 'Cake Description:', rows = 3),
5863
actionButton(ns('submit'), 'Submit')
5964
)
@@ -64,14 +69,30 @@ sidebarServer <- function(id, board) {
6469
moduleServer(id, function(input, output, session) {
6570
ns <- session$ns
6671

72+
observeEvent(input$select, {
73+
if(input$select == 3) {
74+
shinyjs::hide("hour")
75+
shinyjs::hide("room")
76+
shinyjs::hide("section")
77+
shinyjs::hide("cake_desc")
78+
} else {
79+
shinyjs::show("hour")
80+
shinyjs::show("room")
81+
shinyjs::show("section")
82+
shinyjs::show("cake_desc")
83+
}
84+
})
85+
6786
input_data <- reactive({
6887
data.frame(
88+
6989
`Date` = input$date,
70-
`Hour` = input$hour,
90+
`Hour` = (strftime(input$hour, "%R")),
7191
`Room` = input$room,
7292
`Section` = input$section,
73-
`Person Name` = input$person,
74-
`Cake Description` = input$cake_desc,
93+
`Person.Name` = input$person,
94+
`Secret.Ingredient` = input$sec_in,
95+
`Cake.Description` = input$cake_desc,
7596
stringsAsFactors = FALSE
7697
)
7798
})
@@ -83,11 +104,37 @@ sidebarServer <- function(id, board) {
83104
input$room,
84105
input$section,
85106
input$person,
86-
input$cake_desc
107+
input$sec_in
87108
)
88-
pinned_cakes <- pin_read(board, name = paste0('cake_user_inputs'))
109+
pinned_cakes <- pin_read(board,
110+
name = paste0(Sys.getenv("USER_NAME"), '/cake_user_inputs'))
111+
112+
pinned_cakes_exists <- pinned_cakes |>
113+
dplyr::filter(Secret.Ingredient == input$sec_in & Person.Name == input$person & Date == input$date)
89114

90-
updated_cakes <- rbind(pinned_cakes, input_data())
115+
if(input$select == 2) {
116+
if (nrow(pinned_cakes_exists) == 0) {
117+
updated_cakes <- pinned_cakes
118+
} else {
119+
pinned_cakes <- pinned_cakes |>
120+
dplyr::filter(Secret.Ingredient != input$sec_in | Person.Name != input$person | Date != input$date)
121+
updated_cakes <- rbind(pinned_cakes, input_data())
122+
}
123+
} else if (input$select == 3) {
124+
if (nrow(pinned_cakes_exists) == 0) {
125+
updated_cakes <- pinned_cakes
126+
} else {
127+
pinned_cakes <- pinned_cakes |>
128+
dplyr::filter(Secret.Ingredient != input$sec_in | Person.Name != input$person | Date != input$date)
129+
updated_cakes <- pinned_cakes
130+
}
131+
} else {
132+
if (nrow(pinned_cakes_exists) != 0) {
133+
updated_cakes <- pinned_cakes
134+
} else {
135+
updated_cakes <- rbind(pinned_cakes, input_data())
136+
}
137+
}
91138

92139
# Save the input data frame to the pin board
93140
pin_write(
@@ -98,11 +145,13 @@ sidebarServer <- function(id, board) {
98145
)
99146

100147
# Clear the inputs after submission
148+
updateRadioButtons(session, "select", selected = 1)
101149
updateDateInput(session, "date", value = Sys.Date())
102150
updateTimeInput(session, "hour", value = strptime("12:00:00", "%T"))
103151
updateSelectInput(session, "room", selected = NULL)
104152
updateSelectInput(session, "section", selected = NULL)
105153
updateTextInput(session, "person", value = "")
154+
updateTextInput(session, "sec_in", value = "")
106155
updateTextAreaInput(session, "cake_desc", value = "")
107156
})
108157

0 commit comments

Comments
 (0)