Grocery List in Shiny

Viewed 51

I'm just trying to make my life easier and make a shiny app where I can select certain recipes I always make. My recipes are stored as objects (ChickenPotPie) and these objects are character lists of ingredients (i.e. "chicken","pie crust","broccoli"). I have a nice script that already can take my recipe lists and spit out a data frame with the item names and quantities. However, I want to make something that my husband will be able to use (he does not know any R), and I think creating a shiny app might be good for that.

This is very stupid question but I am very novice to shiny and wondering if anyone has any advice for resources where I could make a very simple app where I can check boxes with which recipes (objects) I am planning on making, and it will just print the objects (ie. character lists) out into a data frame?

Example code for what I've created so far is below..

#### RECIPE MASTER LIST ########

"The staples" <- c("milk", "12 eggs", "cheddar cheese", "sparkling water", "butter", "flour")
"Honey Mustard Dijon Chicken" <- c("1 lb chicken breast", "1 lb chicken breast", "panko crumbs", "brussel sprouts","shallot","shallot","fresh thyme")
"Sheet pan gnocchi" <- c("32 oz gnocchi","parmesan cheese","onion","cherry tomatoes","arugulua")
"Sheet pan chicken meatballs" <- c("panko","6 oz feta","egg", "minced garlic","ground meat/vegan meat", "1 pint cherry tomatoes", "1 can chickpeas")
# https://www.bonappetit.com/recipe/sheet-pan-chicken-meatballs-with-tomatoes-and-chickpeas


MasterList <- names(.GlobalEnv)

###### WHAT ARE YOU MAKING THIS WEEK?! #########

# What are my options?
print(MasterList)

# Of this, what do I want to include? 
currentlist <- c(`Honey Mustard Dijon Chicken`, `The staples`,`Sheet pan gnocchi`)



#### Display List #####
CurrentGroceries<-as.data.frame(table(currentlist))
colnames(CurrentGroceries) <- c("Item","Quantity")

View(CurrentGroceries)
1 Answers

We can do something like this.

First we put all of the recipes inside a list object and then we proceed to create one selectInput to choose from the available recipes. Finally subset the recipes list using the value of the selectInput and display it inside a table.

App:

#### RECIPE MASTER LIST ########
library(dplyr)
library(tibble)
library(shiny)
library(purrr)

recipes <- list(
  "The staples" = c(
    "milk", "12 eggs", "cheddar cheese", "sparkling water", "butter", "flour"
  ),
  "Honey Mustard Dijon Chicken" = c(
    "1 lb chicken breast", "1 lb chicken breast", "panko crumbs",
    "brussel sprouts", "shallot", "shallot", "fresh thyme"
  ),
  "Sheet pan gnocchi" = c(
    "32 oz gnocchi", "parmesan cheese", "onion", "cherry tomatoes", "arugulua"
  ),
  "Sheet pan chicken meatballs" = c(
    "panko", "6 oz feta", "egg", "minced garlic", "ground meat/vegan meat",
    "1 pint cherry tomatoes", "1 can chickpeas"
  )
)

# https://www.bonappetit.com/recipe/sheet-pan-chicken-meatballs-with-tomatoes-and-chickpeas

MasterList <- names(recipes)

ui <- fluidPage(
  titlePanel("What Are You Making This Week?"),
  sidebarLayout(
    sidebarPanel(
      selectInput(
        "masterclass",
        "What are my options?",
        choices  = MasterList,
        multiple = TRUE
      )
    ),
    mainPanel(tableOutput("ingredients"))
  )
)

server <- function(input, output, session) {
  current_groceries <- eventReactive(input$masterclass, {
    recipes[input$masterclass] %>%
      purrr::reduce(c) %>%
      table() %>%
      as.data.frame() %>%
      purrr::set_names(c("Item", "Quantity"))
  })

  output$ingredients <- renderTable({
    current_groceries()
  })
}

shinyApp(ui, server)
Related