#' editMeal UI Function
#'
#' @description Module shared for adding new and editing meal information.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @param data The reactive values list common to all modules, named 'LOCAL'
#'
#' @noRd
#'
#' @importFrom shiny NS tagList
#' @importFrom DT DTOutput renderDT datatable dataTableProxy editData
#' @importFrom shinycssloaders withSpinner
#' @importFrom shinyWidgets pickerInput updatePickerInput
#' @importFrom plyr round_any
#' @importFrom shinydashboard updateTabItems
#' @importFrom openxlsx readWorkbook saveWorkbook addWorksheet createWorkbook writeData read.xlsx
#' @importFrom magrittr %>%
#' @importFrom dplyr mutate select group_by case_when pull filter left_join inner_join arrange bind_rows bind_cols
#'
#'
mod_editMeal_ui <- function(id){
ns <- NS(id)
fluidRow(
column(width = 12,
fluidRow(
column(width = 12,
uiOutput(ns('addInputs'))
),#end column
),#end fluidRow
fluidRow(
uiOutput(ns('viewTables'))
)#end fluidRow
)#end column
)#end fluidRow
}
#' editMeal Server Functions
#'
#' @noRd
mod_editMeal_server <- function(id, data){
moduleServer( id, function(input, output, session){
ns <- session$ns
LOCAL<-data
#TOP ROW INPUTS----------------------------------
output$addInputs <- renderUI({
ns <- session$ns
if(LOCAL$editMode == FALSE){
inputPanel(
textInput(ns('newmealinput'),label = 'Enter New Meal Name'),
pickMealType(session,input,output,data = LOCAL, control = 'choosemealtype',label = 'Pick New Meal Type'),
textInput(ns('newmealdesc'),label = 'Enter New Meal Description'),
tags$div(class = 'btn-group', style = "width: 480px;",
btn_panelItem(ns(id='addIngredient'),lbl='Add Ingredients',icn = "plus", class = "btn-success"),
btn_panelItem(ns(id='deleteIngredient'),lbl='Delete Selected',icn = "remove", class = "btn-danger"),
btn_panelItem(ns(id='newIngredient'),lbl='Create Ingredient',icn = "pencil", class = "btn-success"),
)#end div
)#end inputPanel
} else
if(LOCAL$editMode == TRUE){
inputPanel(
textInput(ns('editmealname'), label = 'Editing Meal:',value = paste(LOCAL$editMeal$MEAL_NAME %>% unique(.),'(revised)')),
pickMealType(session,input,output,data = LOCAL, control = 'choosemealtype',label = 'Meal Type',selected = LOCAL$editMeal$MEAL_TYPE %>% unique(.)),
textInput(ns('editmealdesc'), label = 'Meal Description', value = LOCAL$editMeal$MEAL_DESCRIPTION %>% unique(.)),
tags$div(class = 'btn-group', style = "width: 480px;",
btn_panelItem(ns(id='addIngredient'),lbl='Add Ingredients',icn = "plus", class = "btn-success"),
btn_panelItem(ns(id='deleteIngredient'),lbl='Delete Ingredient',icn = "remove", class = "btn-danger"),
btn_panelItem(ns(id='newIngredient'),lbl='Create Ingredient',icn = "pencil", class = "btn-success"),
)#end div
)#end inputPanel
}#end if
})#end renderUI
#EDITABLE INGREDIENTS TABLE UI-----------
output$viewTables <- renderUI({
ns <- session$ns
if(LOCAL$editMode == FALSE){
column(width = 12,
tags$div(h2(paste('Preview of',input$newmealinput), style = "display: inline;"),
style = 'display: inline-block; float: left; vertical-align: middle;'),#end div
DTOutput(ns('newMealView')),
btn_panelItem(ns(id='commit'),lbl='Save New Meal', class = "btn-success"),
btn_panelItem(ns(id='cancel'),lbl='Cancel', class = "btn-default")
)#end column
} else
if(LOCAL$editMode == TRUE){
column(width = 12,
tags$div(h2(paste('Editing',LOCAL$editMeal$MEAL_NAME %>% unique(.)), style = "display: inline;"),h3('(double-click table to edit cells)',style = "display: inline;"),
style = 'display: inline-block; float: left; vertical-align: middle;'),#end div
DTOutput(ns('editView')),
btn_panelItem(ns(id='commit'),lbl='Done Editing', class = "btn-success"),
btn_panelItem(ns(id='cancel'),lbl='Cancel', class = "btn-default")
)#end column
}#end if
})#end renderUI
#OUTPUT ADD and EDITING TABLES-----------------
output$newMealView <- renderDT(dtStyle1(session, df = LOCAL$newMeal, edit = FALSE))#end renderDT
output$editView <- renderDT(dtStyle1(session, df = LOCAL$editMeal))
#Observe cell edits on editing table------
observe({
proxy<-dataTableProxy('editView')
DT::replaceData(proxy, LOCAL$editMeal, rownames = FALSE, resetPaging = FALSE)
})
#Updating cell being edited
observeEvent(input$editView_cell_edit, {
#Change SSF and REVISED
col <- input$editView_cell_edit$col + 1 #account for HTML table column numbers zero-based
row <- input$editView_cell_edit$row
oldValue <- LOCAL$editMeal[row,col]
name <- names(LOCAL$editMeal)[col]
LOCAL$editMeal <<- editData(LOCAL$editMeal, input$editView_cell_edit, rownames = FALSE)
LOCAL$editMeal[row,11] <- paste0(LOCAL$editMeal[row,11],'revised',name,"from",oldValue,sep = '_')
if(col == 6){
ing <- as.character(LOCAL$editMeal[row,5])
showModal(editQuantityModal(session,ing = ing))
}#end if
})#end observeEvent
#Update all SSF button on editQuantityModal
observeEvent(input$updateIngredientSSF, {updateAllSSF(session,input,output,data = LOCAL)})
#No Update All SSF button on editQuantityModal
observeEvent(input$noUpdateIngredientSSF, {noUpdateAllSSF(session,input,output,data = LOCAL)})
#LAUNCH SELECT INGREDIENTS MODAL--------------------------
observeEvent(input$addIngredient,{
if(LOCAL$editMode == FALSE){
validate(
need(input$newmealinput, 'Enter Meal Name'),
need(input$choosemealtype, 'Choose Meal Type'),
need(input$newmealdesc, 'Enter Meal Description')
)
}#end if
addIngredientModal(session,input,output,data = LOCAL)
})#end observeEvent
#Make modal ingredient list
observe({
output$ingredients <- renderDT(dtStyle1(session, df = LOCAL$li[,c(3,2,4,5,1)],edit = FALSE, filter = 'top', dom = 'lft'))#end renderDT
})
#Observe modal commit buttons
observeEvent(input$modalAddIngredient,{addIngredients(session, input, output, data = LOCAL)})
observeEvent(input$modalCancel,{removeModal()})
#Observe modal button for committing new meal
observeEvent(input$modalCommitNewMeal, {
#Write to local session also
commitAddEdit(session,input,output, data = LOCAL)
#Append new meal data to the datatbase
withProgress(
LOCAL$newMeal %>% select(MEAL_ID,MEAL_NAME,MEAL_TYPE,MEAL_DESCRIPTION) %>%
mutate(UPTIME = Sys.Date()) %>%
unique(.) %>%
googlesheets4::sheet_append(dbURL, .,sheet = 'LU_MEAL'),
message = 'Writing to database')#end progress
#Append to XREF_INGREDIENT in database
LOCAL$newMeal %>% select(INGREDIENT_ID, MEAL_ID, INGREDIENT, MEAL_NAME) %>%
mutate(UPTIME = Sys.Date()) %>%
unique(.) %>%
googlesheets4::sheet_append(dbURL, .,sheet = 'XREF_INGREDIENT')
#reset editMode to FALSE -- this is probably redundant
LOCAL$editMode <- FALSE
#Clear newMeal
LOCAL$newMeal <- data.frame()
#Clear input fields
updateTextInput(session,'newmealinput',value = '')
updatePickerInput(session, 'choosemealtype', selected = '--Select Meal Type--')
updateTextInput(session, 'newmealdesc',value = '')
removeModal()
})#end observeEvent
observeEvent(input$modalThisMenuOnly,{
#Write new meal to local session
withProgress(
commitAddEdit(session,input,output, data = LOCAL),
message = 'Writing...'
)#end progress
#reset editMode to FALSE -- this is probably redundant
LOCAL$editMode <- FALSE
#Clear newMeal
LOCAL$newMeal <- data.frame()
#Clear input fields
updateTextInput(session,'newmealinput',value = '')
updatePickerInput(session, 'choosemealtype', selected = '--Select Meal Type--')
updateTextInput(session, 'newmealdesc',value = '')
removeModal()
})#end observeEvent
#DELETE INGREDIENT FROM MEAL BUTTON----------------------------------------
observeEvent(input$deleteIngredient,{deleteIngredient(session,input,output, data = LOCAL)})
#COMMIT NEW/REVISED MEAL BUTTON-----------------------------------------------------
observeEvent(input$commit,{
if(LOCAL$editMode == FALSE){newMealModal(session,input,output)} else
if(LOCAL$editMode == TRUE){commitAddEdit(session,input,output, data = LOCAL)}
})#end observeEvent
# CANCEL ADD/EDIT BUTTON---------------------------------------------
observeEvent(input$cancel,{cancelAddEdit(session, input, output, data = LOCAL)})
#TO EXPORT FROM MODULE----------------
toExport<-list(
LOCAL = LOCAL,
input_commit = reactive(input$commit),
input_cancel = reactive(input$cancel),
input_newIngredient = reactive(input$newIngredient)
)
})#end mod_editMeal_server
}
## To be copied in the UI
# mod_editMeal_ui("editMeal_1")
## To be copied in the server
# mod_editMeal_server("editMeal_1")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.