开始使用免费开始使用

Explore cuisines: top ingredients redux

Each cuisine is distinct because of a small set of distinct ingredients. We can't surface these by looking at the most popular ingredients, since they're the bread-and-butter ingredients of cooking like salt or sugar.

Another metric that can aid us in this quest is the term frequency–inverse document frequency (TFIDF), a numerical statistic that is intended to reflect how important a word (ingredient) is to a document (cuisine) in a collection or corpus (recipes).

We already precomputed the tf_idf for you and created an enriched dataset named recipes_enriched. Your goal is to create a Shiny app that displays a horizontal bar plot of the top distinctive ingredients in a cuisine, as measured by tf_idf.

An app displaying an interactive horizontal bar plot of top ingredients by chosen cuisine

You will use a reactive expression to encapsulate the computations and let the plotting code focus only on creating the plot. This is good programming practice and helps create modular and performant Shiny apps.

We have loaded the packages shiny, dplyr, ggplot2 and plotly. Here are two handy snippets to filter for the top recipes by cuisine and create a horizontal bar plot. You can modify it appropriately.

top_ingredients <- recipes_enriched %>% 
  filter(cuisine == 'greek') %>% 
  arrange(desc(tf_idf)) %>% 
  head(5) 

ggplot(top_ingredients, aes(x = ingredient, y = tf_idf)) +
  geom_col() +
  coord_flip()

本练习是课程的一部分

Building Web Applications with Shiny in R

查看课程

练习说明

  • UI:

    • Add an interactive plotly output named plot_top_ingredients and wrap in a tabPanel() with an appropriate label.
  • Server:

    • Add a reactive expression named rval_top_ingredients that filters recipes_enriched for the selected cuisine and top distinctive ingredients based on the tf_idf value.
    • Render an interactive bar plot of top ingredients and their tf_idf and assign it to an output named plot_top_ingredients. As an extra challenge for yourself, see if you can get the bars to display in descending order of tf_idf.

交互式实操练习

通过完成这段示例代码来试试这个练习。

ui <- fluidPage(
  titlePanel('Explore Cuisines'),
  sidebarLayout(
    sidebarPanel(
      selectInput('cuisine', 'Select Cuisine', unique(recipes$cuisine)),
      sliderInput('nb_ingredients', 'Select No. of Ingredients', 1, 100, 10),
    ),
    mainPanel(
      tabsetPanel(
        # CODE BELOW: Add a plotly output named "plot_top_ingredients"
        
        tabPanel('Table', DT::DTOutput('dt_top_ingredients'))
      )
    )
  )
)

server <- function(input, output, session) {
  # CODE BELOW: Add a reactive expression named `rval_top_ingredients` that
  # filters `recipes_enriched` for the selected cuisine and top ingredients
  # based on the tf_idf value.

  
  
  
  
  
  
  # CODE BELOW: Render a horizontal bar plot of top ingredients and 
  # the tf_idf of recipes they get used in, and assign it to an output named 
  # `plot_top_ingredients` 
  
  
  
  
  
  output$dt_top_ingredients <- DT::renderDT({
    recipes %>% 
      filter(cuisine == input$cuisine) %>% 
      count(ingredient, name = 'nb_recipes') %>% 
      arrange(desc(nb_recipes)) %>% 
      head(input$nb_ingredients)
  })
}

shinyApp(ui, server)
编辑并运行代码