R Shiny 无法使反应式过滤器工作
RShiny cannot get reactive filter to work
就像我在标题中所说的那样,我无法让这个 R Shiny 应用程序的过滤器部分正常工作。当我只有图的过滤器时,我得到错误“美学必须是长度 1 或与数据 (1) 相同:x”。在此代码块中,我在表中也有过滤器,但出现错误“ .getReactiveEnvironment()$currentContext() 中的错误:
没有活动的反应上下文不允许操作。
- 您试图做一些只能在被动消费者内部完成的事情。"
我的反应式语句抛出这些错误我做错了什么?
'''
library(shiny)
library(ggplot2)
library(tidyverse)
library(DT)
library(dplyr)
drinks <- read_csv("data/starbucks-menu-nutrition-drinks.csv")
foods <- read_csv("data/starbucks-menu-nutrition-food.csv")
category_drinks <- c("Calories", "Fat (g)", "Carb. (g)", "Fiber (g)", "Protein", "Sodium")
category_food <- c("Calories", "Fat (g)", "Carb. (g)", "Fiber (g)", "Protein (g)")
ui <- fluidPage(
# Application title
titlePanel("Starbucks Nutition Information"),
sidebarLayout(
sidebarPanel(
h5("Use the dropdown to choose the desired nutritional information."),
selectInput(inputId = "drink_criteria", label = "Drink Criteria", choices = category_drinks, selected = "Calories"),
selectInput(inputId = "food_criteria", label = "Food Criteria", choices = category_food, selected = "Calories"),
h5("Use the sliders to select the desired range for the selected nutritional information."),
sliderInput(inputId = "max", label = "Maximum", min = 0, max = 700, value = 300),
sliderInput(inputId = "min", label = "Minimum", min = 0, max = 500, value = 50),
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel(title = "Drink Chart",
plotOutput(outputId = "drink_plot")
),
tabPanel(title= "Drink Table",
DT::dataTableOutput(outputId = "drink_table")
),
tabPanel(title = "Food Chart",
plotOutput(outputId = "food_plot")
),
tabPanel(title = "Food Table",
DT::dataTableOutput(outputId = "food_table")
),
tabPanel(title = "About",
h2("About the App"),
h3("This app was built to show the nutritional values for a variety of Starbucks drinks and foods."),
h2("How to Use"),
h3("All the controls for this app are located in the sidebar on the left. To choose the nutritional information you would like to see in the graphs
select it from the dropdown. Once the desired nutitional information is picked use the sliders below it to select the range you would like to
view."),
h2("What You See"),
h3("Each of Food and Drink has two tabs. The first is a graph showing a list of items within the selected range.
The second is a table of all the resusts within the selected range. This allows you to view the other nutritional
infromation about the items." ),
h5("This app was created by Adam Maier for DSBA 5122"),
h5("The data was sourced from: https://www.kaggle.com/datasets/starbucks/starbucks-menu")
)
)
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
drinksinput <- reactive({
drinks %>%
filter(input$drink_criteria != "-",
input$drink_criteria >= input$min,
input$drink_criteria <= input$max)
}),
drinksdf <- drinksinput())})
#render both of the plots
output$drink_plot <- renderPlot({
drinksinput <- reactive({
drinks %>%
filter(input$drink_criteria != "-",
input$drink_criteria >= input$min,
input$drink_criteria <= input$max)
})
drinksdf <- drinksinput()
ggplot(drinksdf) +
geom_col(aes(x = drink_name, y = input$drink_criteria)) +
labs(x = "Drink", y = input$drink_criteria) +
theme_bw() +
scale_x_discrete(guide = guide_axis(angle = 90))
})
output$food_plot <- renderPlot({
foodsinput <- reactive({
foods %>%
filter(input$food_criteria != "-",
input$food_criteria >= input$min,
input$food_criteria <= input$max)
})
foodsdf <- foodsinput()
ggplot(foodsdf) +
geom_col(aes(x = food_name, y = input$food_criteria)) +
labs(x = "Food", y = input$food_criteria) +
theme_bw() +
scale_x_discrete(guide = guide_axis(angle = 90))
})
#render both of the tables
output$drink_table <- DT::renderDataTable({DT::datatable(
drinksinput <- reactive({
drinks %>%
filter(input$drink_criteria != "-",
input$drink_criteria >= input$min,
input$drink_criteria <= input$max)
}),
drinksdf <- drinksinput())})
output$food_table <- DT::renderDataTable({DT::datatable(
foodsinput <- reactive({
foods %>%
filter(input$food_criteria != "-",
input$food_criteria >= input$min,
input$food_criteria <= input$max)
}),
foodsdf <- foodsinput()
)})
}
# Run the application
shinyApp(ui = ui, server = server)
'''
dput输出
> dput(drinks)
structure(list(drink_name = c("Cool Lime Starbucks Refreshers1 Beverage",
"Ombré Pink Drink", "Pink Drink", "Strawberry Acai Starbucks Refreshers1 Beverage",
"Very Berry Hibiscus Starbucks Refreshers1 Beverage", "Violet Drink",
"Evolution Fresh1 Cold-Pressed Apple Berry Juice", "Evolution Fresh1 Defense Up",
"Evolution Fresh1 Organic Ginger Limeade", "Iced Coffee",
"Iced Coffee with Milk", "Iced Espresso Classics - Vanilla Latte",
"Iced Espresso Classics - Caffe Mocha", "Iced Espresso Classics - Caramel Macchiato",
"Starbucks® Caramel Iced Coffee", "Starbucks® Iced Coffee + Milk",
"Starbucks® Low Calorie Iced Coffee + Milk", "Starbucks® Vanilla Iced Coffee",
"Shaken Sweet Tea", "Tazo® Bottled Berry Blossom White", "Tazo® Bottled Black Mango",
"Tazo® Bottled Black with Lemon", "Tazo® Bottled Brambleberry",
"Tazo® Bottled Giant Peach", "Tazo® Bottled Iced Passion", "Tazo® Bottled Lemon Ginger",
"Tazo® Bottled Organic Black Lemonade", "Tazo® Bottled Organic Iced Black Tea",
"Tazo® Bottled Organic Iced Green Tea", "Tazo® Bottled Plum Pomegranate",
"Tazo® Bottled Tazoberry", "Tazo® Bottled White Cranberry", "Teavana® Shaken Iced Black Tea",
"Teavana® Shaken Iced Black Tea Lemonade", "Teavana® Shaken Iced Green Tea",
"Teavana® Shaken Iced Green Tea Lemonade", "Teavana® Shaken Iced Passion Tango1 Tea",
"Teavana® Shaken Iced Passion Tango1 Tea Lemonade", "Teavana® Shaken Iced Peach Green Tea",
"Iced Espresso Classics - Vanilla Latte", "Iced Espresso Classics - Caffe Mocha",
"Iced Espresso Classics - Caramel Macchiato", "Iced Espresso Classics - Skinny Caramel Macchiato",
"Iced Espresso Classics - Skinny Vanilla Latte", "Starbucks Doubleshot® Energy Coffee Drink",
"Starbucks Doubleshot® Energy Mexican Mocha", "Starbucks Doubleshot® Energy Mocha Drink",
"Starbucks Doubleshot® Energy Vanilla Drink", "Starbucks Doubleshot® Energy White Chocolate Drink",
"Starbucks Doubleshot® Energy+Cinnamon Dolce Drink", "Starbucks Doubleshot® Espresso Cubano",
"Starbucks Doubleshot® Espresso Drink", "Starbucks Doubleshot® Espresso Salted Caramel Cream",
"Starbucks Doubleshot® Light Espresso Drink", "Starbucks Doubleshot® Protein Caramel",
"Starbucks Refreshers1 Raspberry Pomegranate", "Starbucks Refreshers1 Strawberry Lemonade",
"Starbucks® Bottled Caramel Frappuccino® Coffee Drink", "Starbucks® Bottled Coffee Frappuccino® Coffee Drink",
"Starbucks® Bottled Cold Brew Black, Unsweetened", "Starbucks® Bottled Cold Brew Cocoa and Honey with Cream",
"Starbucks® Bottled Dulce de Leche Frappuccino® Coffee Drink",
"Starbucks® Bottled Mocha Frappuccino® Coffee Drink", "Starbucks® Bottled Mocha Light Frappuccino® Coffee Drink",
"Starbucks® Bottled S'mores Frappuccino® Coffee Drink", "Starbucks® Bottled Vanilla Frappuccino® Coffee Drink",
"Starbucks® Bottled Vanilla Light Frappuccino® Coffee Drink",
"Starbucks® Caramel Iced Coffee", "Starbucks® Doubleshot Protein Dark Chocolate",
"Starbucks® Doubleshot Protein Vanilla", "Starbucks® Iced Coffee + Milk",
"Starbucks® Iced Coffee Caramel", "Starbucks® Iced Coffee Light Sweetened",
"Starbucks® Iced Coffee Unsweetened", "Starbucks® Low Calorie Iced Coffee + Milk",
"Starbucks® Refreshers1 - Black Cherry Limeade with Coconut Water",
"Starbucks® Vanilla Iced Coffee", "Tazo® Bottled Berry Blossom White",
"Tazo® Bottled Black Mango", "Tazo® Bottled Black with Lemon",
"Tazo® Bottled Brambleberry", "Tazo® Bottled Giant Peach", "Tazo® Bottled Iced Passion",
"Tazo® Bottled Lemon Ginger", "Tazo® Bottled Organic Black Lemonade",
"Tazo® Bottled Organic Iced Black Tea", "Tazo® Bottled Organic Iced Green Tea",
"Tazo® Bottled Plum Pomegranate", "Tazo® Bottled Tazoberry",
"Tazo® Bottled White Cranberry", "Teavana® Mango Black Tea",
"Teavana® Passion Tango® Herbal Tea", "Teavana® Peach Green Tea",
"White Chocolate Mocha Bottled Frappuccino", "Blonde Roast",
"Caffè Misto", "Clover® Brewed Coffee", "Coffee Traveler", "Decaf Pike Place® Roast",
"Featured Dark Roast", "Iced Coffee", "Iced Coffee with Milk",
"Nariño 70 Cold Brew", "Nariño 70 Cold Brew with Milk", "Nitro Cold Brew",
"Nitro Cold Brew with Sweet Cream", "Pike Place® Roast", "Toasted Coconut Cold Brew",
"Vanilla Sweet Cream Cold Brew", "Hot Chocolate", "Starbucks® Signature Hot Chocolate",
"Caffè Americano", "Caffè Latte", "Caffè Mocha", "Cappuccino",
"Caramel Macchiato", "Cascara Coconutmilk Latte", "Cinnamon Dolce Latte",
"Coconutmilk Mocha Macchiato", "Espresso", "Espresso Con Panna",
"Espresso Macchiato", "Flat White", "Iced Caffè Americano", "Iced Caffè Latte",
"Iced Caffè Mocha", "Iced Caramel Macchiato", "Iced Cascara Coconutmilk Latte",
"Iced Cinnamon Dolce Latte", "Iced Coconutmilk Mocha Macchiato",
"Iced Skinny Cinnamon Dolce Latte", "Iced Skinny Mocha", "Iced Vanilla Latte",
"Frappuccino® Blended Crème",
"The Pokémon1 GO Frappuccino® Blended Beverage", "Vanilla Bean Crème Frappuccino® Blended Crème",
"White Chocolate Mocha Frappuccino® Blended Coffee", "Cinnamon Dolce Crème",
"Organic Chocolate Milk Box", "Steamed Apple Juice", "Vanilla Crème",
"Chocolate Smoothie", "Strawberry Smoothie", "Ginger Ale", "Lemon Ale",
"Orange Cream Soda"), Calories = c("45.00", "-", "-", "80.00",
"60.00", "-", "-", "-", "110.00", "0.00", "-", "130.00", "140.00",
"130.00", "-", "-", "-", "-", "80.00", "60.00", "150.00", "140.00",
"140.00", "150.00", "70.00", "120.00", "140.00", "60.00", "120.00",
"140.00", "150.00", "140.00", "30.00", "70.00", "30.00", "70.00",
"30.00", "90.00", "60.00", "130.00", "140.00", "130.00", "-",
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "90.00",
"90.00", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-",
"210.00", "200.00", "-", "60.00", "50.00", "10.00", "-", "-",
"-", "60.00", "150.00", "140.00", "140.00", "150.00", "70.00",
"120.00", "140.00", "60.00", "120.00", "140.00", "150.00", "140.00",
"-", "-", "-", "-", "5.00", "-", "10.00", "-", "5.00", "5.00",
"5.00", "-", "5.00", "0.00", "5.00", "70.00", "5.00", "-", "110.00",
"320.00", "430.00", "-", "190.00", "290.00", "120.00", "250.00",
"-", "260.00", "250.00", "-", "-", "-", "180.00", "-", "130.00",
"230.00", "250.00", "-", "200.00", "260.00", "-", "-", "190.00",
"300.00", "190.00", "190.00", "-", "45.00", "250.00", "360.00",
"-", "-", "-", "-", "-", "-", "350.00", "-", "-", "-", "110.00",
"-", "-", "-", "-", "-", "-", "-", "280.00", "140.00", "-", "-",
"-", "-", "-", "-", "-", "-", "200.00", "-", "-", "200.00", "320.00",
"300.00", "-", "-", "-"), `Fat (g)` = c("0.00", "-", "-", "0.00",
"0.00", "-", "-", "-", "0.00", "0.00", "-", "2.50", "2.50", "2.50",
"-", "-", "-", "-", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
"0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
"0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "2.50",
"2.50", "2.50", "-", "-", "-", "-", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "0.00", "0.00", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "-", "-", "2.50", "2.50", "-", "0.00", "0.00",
"0.00", "-", "-", "-", "0.00", "0.00", "0.00", "0.00", "0.00",
"0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
"-", "-", "-", "-", "0.00", "-", "0.00", "-", "0.00", "0.00",
"0.00", "-", "0.00", "0.00", "0.00", "5.00", "0.00", "-", "6.00",
"9.00", "26.00", "-", "7.00", "8.00", "4.00", "7.00", "-", "6.00",
"9.00", "-", "-", "-", "7.00", "-", "4.50", "6.00", "7.00", "-",
"4.00", "9.00", "-", "-", "4.00", "8.00", "7.00", "7.00", "-",
"1.00", "6.00", "11.00", "-", "-", "-", "-", "-", "-", "4.50",
"-", "-", "-", "0.00", "-", "-", "-", "-", "-", "-", "-", "2.50",
"0.50", "-", "-", "-", "-", "-", "-", "-", "-", "6.00", "-",
"-", "6.00", "5.00", "2.00", "-", "-", "-"), `Carb. (g)` = c("11.00",
"-", "-", "18.00", "14.00", "-", "-", "-", "28.00", "0.00", "-",
"21.00", "23.00", "21.00", "-", "-", "-", "-", "19.00", "15.00",
"38.00", "35.00", "35.00", "37.00", "17.00", "31.00", "35.00",
"15.00", "31.00", "35.00", "38.00", "35.00", "8.00", "17.00",
"8.00", "17.00", "8.00", "24.00", "15.00", "21.00", "23.00",
"21.00", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-",
"-", "-", "27.00", "27.00", "-", "-", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "33.00", "34.00", "-", "13.00", "11.00",
"2.00", "-", "-", "-", "15.00", "38.00", "35.00", "35.00", "37.00",
"17.00", "31.00", "35.00", "15.00", "31.00", "35.00", "38.00",
"35.00", "-", "-", "-", "-", "0.00", "-", "0.00", "-", "0.00",
"0.00", "0.00", "-", "0.00", "0.00", "0.00", "5.00", "0.00",
"-", "14.00", "47.00", "45.00", "-", "19.00", "42.00", "12.00",
"35.00", "-", "40.00", "32.00", "-", "-", "-", "18.00", "-",
"13.00", "36.00", "37.00", "-", "34.00", "34.00", "-", "-", "30.00",
"47.00", "19.00", "19.00", "-", "5.00", "37.00", "53.00", "-",
"-", "-", "-", "-", "-", "64.00", "-", "-", "-", "24.00", "-",
"-", "-", "-", "-", "-", "-", "60.00", "28.00", "-", "-", "-",
"-", "-", "-", "-", "-", "28.00", "-", "-", "28.00", "53.00",
"60.00", "-", "-", "-"), `Fiber (g)` = c("0.00", "-", "-", "1.00",
"1.00", "-", "-", "-", "0.00", "0.00", "-", "0.00", "0.00", "0.00",
"-", "-", "-", "-", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
"0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
"0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
"0.00", "0.00", "-", "-", "-", "-", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "0.00", "0.00", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "-", "-", "2.00", "2.00", "-", "0.00", "0.00",
"0.00", "-", "-", "-", "0.00", "0.00", "0.00", "0.00", "0.00",
"0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
"-", "-", "-", "-", "0.00", "-", "0.00", "-", "0.00", "0.00",
"0.00", "-", "0.00", "0.00", "0.00", "0.00", "0.00", "-", "0.00",
"4.00", "5.00", "-", "0.00", "4.00", "0.00", "0.00", "-", "0.00",
"0.00", "-", "-", "-", "0.00", "-", "0.00", "4.00", "0.00", "-",
"0.00", "0.00", "-", "-", "0.00", "0.00", "0.00", "0.00", "-",
"0.00", "0.00", "0.00", "-", "-", "-", "-", "-", "-", "0.00",
"-", "-", "-", "0.00", "-", "-", "-", "-", "-", "-", "-", "2.00",
"1.00", "-", "-", "-", "-", "-", "-", "-", "-", "0.00", "-",
"-", "0.00", "8.00", "7.00", "-", "-", "-"), Protein = c("0.00",
"-", "-", "0.00", "0.00", "-", "-", "-", "0.00", "0.00", "-",
"5.00", "5.00", "5.00", "-", "-", "-", "-", "0.00", "0.00", "0.00",
"0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
"0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
"0.00", "0.00", "5.00", "5.00", "5.00", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "-", "-", "-", "-", "0.00", "0.00", "-",
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "20.00", "20.00",
"-", "1.00", "1.00", "1.00", "-", "-", "-", "0.00", "0.00", "0.00",
"0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
"0.00", "0.00", "-", "-", "-", "-", "1.00", "-", "1.00", "-",
"1.00", "1.00", "0.00", "-", "0.00", "0.00", "0.00", "1.00",
"1.00", "-", "1.00", "14.00", "12.00", "-", "13.00", "13.00",
"8.00", "10.00", "-", "11.00", "12.00", "-", "-", "-", "12.00",
"-", "8.00", "9.00", "10.00", "-", "7.00", "11.00", "-", "-",
"7.00", "10.00", "12.00", "12.00", "-", "3.00", "12.00", "14.00",
"-", "-", "-", "-", "-", "-", "15.00", "-", "-", "-", "3.00",
"-", "-", "-", "-", "-", "-", "-", "4.00", "4.00", "-", "-",
"-", "-", "-", "-", "-", "-", "10.00", "-", "-", "10.00", "20.00",
"16.00", "-", "-", "-"), Sodium = c("10.00", "-", "-", "10.00",
"10.00", "-", "-", "-", "5.00", "0.00", "-", "65.00", "90.00",
"65.00", "-", "-", "-", "-", "10.00", "10.00", "15.00", "10.00",
"15.00", "15.00", "10.00", "10.00", "10.00", "10.00", "10.00",
"10.00", "15.00", "10.00", "5.00", "0.00", "5.00", "5.00", "5.00",
"0.00", "0.00", "65.00", "90.00", "65.00", "-", "-", "-", "-",
"-", "-", "-", "-", "-", "-", "-", "-", "-", "0.00", "0.00",
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "115.00",
"120.00", "-", "0.00", "0.00", "0.00", "-", "-", "-", "10.00",
"15.00", "10.00", "15.00", "15.00", "10.00", "10.00", "10.00",
"10.00", "10.00", "10.00", "15.00", "10.00", "-", "-", "-", "-",
"10.00", "-", "10.00", "-", "10.00", "10.00", "5.00", "-", "15.00",
"0.00", "10.00", "20.00", "10.00", "-", "25.00", "160.00", "115.00",
"-", "170.00", "140.00", "100.00", "150.00", "-", "150.00", "180.00",
"-", "-", "-", "160.00", "-", "115.00", "90.00", "150.00", "-",
"95.00", "180.00", "-", "-", "100.00", "190.00", "160.00", "160.00",
"-", "40.00", "150.00", "240.00", "-", "-", "-", "-", "-", "-",
"0.00", "-", "-", "-", "200.00", "-", "-", "-", "-", "-", "-",
"-", "220.00", "180.00", "-", "-", "-", "-", "-", "-", "-", "-",
"135.00", "-", "-", "135.00", "170.00", "130.00", "-", "-", "-"
)), row.names = c(NA, -177L), spec = structure(list(cols = list(
drink_name = structure(list(), class = c("collector_character",
"collector")), Calories = structure(list(), class = c("collector_character",
"collector")), `Fat (g)` = structure(list(), class = c("collector_character",
"collector")), `Carb. (g)` = structure(list(), class = c("collector_character",
"collector")), `Fiber (g)` = structure(list(), class = c("collector_character",
"collector")), Protein = structure(list(), class = c("collector_character",
"collector")), Sodium = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x0000017f750959e0>, class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))
> dput(foods)
structure(list(food_name = c("Chonga Bagel", "8-Grain Roll",
"Almond Croissant", "Apple Fritter", "Banana Nut Bread", "Blueberry Muffin with Yogurt and Honey",
"Blueberry Scone", "Butter Croissant", "Butterfly Cookie", "Cheese Danish",
"Chewy Chocolate Cookie", "Chocolate Chip Cookie", "Chocolate Chunk Muffin",
"Chocolate Croissant", "Chocolate Hazelnut Croissant", "Chocolate Marble Loaf Cake",
"Cinnamon Morning Bun", "Cinnamon Raisin Bagel", "Classic Coffee Cake",
"Cookie Butter Bar", "Cranberry Orange Scone", "Devil's Food Doughnut",
"Double Chocolate Brownie", "Double Chocolate Chunk Brownie",
"Double Chocolate Loaf Cake", "Everybody's Favorite - Bantam Bagel (2 Pack)",
"Everything Bagel with Cheese", "Frappuccino® Cookie Straw",
"French Toast - Bantam Bagels (2 Pack)", "Gluten-Free Marshmallow Dream Bar",
"Ice Cream Cone Sugar Cookie", "Iced Lemon Pound Cake", "Michigan Cherry Oat Bar",
"Multigrain Bagel", "Oatmeal Cookie", "Octopus Cookie", "Old-Fashioned Glazed Doughnut",
"Peanut butter cup cookie", "Pecan Tart", "Petite Vanilla Bean Scone",
"Plain Bagel", "Pumpkin Bread", "Raspberry Swirl Loaf Cake",
"Raspberry Whoopie Pie", "Reduced-Fat Cinnamon Swirl Coffee Cake",
"S'mores Bar", "S'mores Bar Multipack", "Sprouted Grain Vegan Bagel",
"Strawberry Cake Pop", "The Classic - Bantam Bagels (2 Pack)",
"Toffeedoodle", "Vanilla Bean Whoopie Pie", "Vermont Maple Walnut Muffin",
"Volpi1 Pepperoni & Tomato Savory Foldover", "Strawberries & Jam Sandwich",
"Burrata & Basil Pesto Small Sandwich", "Cage-Free Eggs & Seasoned Grains Side Salad",
"Cauliflower Tabbouleh Side Salad", "Cubano Sandwich", "Farmers' Market Protein Box",
"Garden Greens & Shaved Parmesan Side Salad", "Green Goddess Avocado Salad",
"Herbed Chicken & Fig Spread Small Sandwich", "Honey BBQ Sriracha Chicken Sandwich",
"Roasted Carrot & Kale Side Salad", "Roasted Turkey & Dill Havarti Sandwich",
"Seared Steak & Cheddar Sandwich", "Slow-Roasted Ham & Beecher's Flagship Cheese Small Sandwich",
"Smoked Mozzarella & Roasted Pepper Sandwich", "Turkey & Fire-Roasted Corn Salad",
"Za'atar Chicken & Lemon Tahini Salad", "BBQ Chicken Protein Box",
"Cheese & Fruit Protein Box", "Chicken Wrap Protein Box", "Eggs & Cheese Protein Box",
"PB&J Protein Box", "Smoked Turkey Protein Box", "Birthday Cake Pop",
"Chocolate Cake Pop", "Chocolate Chip Cookie Dough Cake Pop",
"Pirate Cake Pop", "Bacon Gouda & Egg Breakfast Sandwich", "Certified Gluten-Free Breakfast Sandwich",
"Classic Whole-Grain Oatmeal", "Double-Smoked Bacon Cheddar & Egg Sandwich",
"Ham & Cheese Croissant", "Hearty Blueberry Oatmeal", "Reduced-Fat Turkey Bacon & Cage Free Egg White Breakfast Sandwich",
"Sausage Cheddar & Egg Breakfast Sandwich", "Seared Steak Egg & Tomatillo Wrap",
"Slow-Roasted Ham Swiss & Egg Breakfast Sandwich", "Sous Vide Egg Bites: Bacon & Gruyere",
"Sous Vide Egg Bites: Egg White & Red Pepper", "Spicy Chorizo Monterey Jack & Egg Breakfast Sandwich",
"Spinach Feta & Cage Free Egg White Breakfast Wrap", "Ancho Chipotle Chicken Panini",
"Chicken & Quinoa Protein Bowl with Black Beans and Greens",
"Chicken Artichoke on Ancient Grain Flatbread", "Chicken BLT Salad Sandwich",
"Egg Salad Sandwich", "Homestyle Chicken & Double-Smoked Bacon",
"Italian-Style Ham & Spicy Salami", "Lentils & Vegetable Protein Bowl with Brown Rice",
"Roasted Tomato & Mozzarella Panini", "Turkey & Havarti Sandwich",
"Turkey Pesto Panini", "Berry Trio Yogurt", "Fresh Blueberries and Honey Greek Yogurt Parfait",
"Justin's Chocolate Hazelnut Butter", "Justin's Classic Almond Butter",
"Lemon Chiffon Yogurt", "Organic Avocado (Spread)", "Seasonal Fruit Blend"
), Calories = c(300, 380, 410, 460, 420, 380, 420, 240, 350,
320, 170, 310, 440, 330, 390, 490, 390, 270, 390, 360, 420, 430,
410, 490, 440, 100, 290, 90, 200, 240, 250, 470, 310, 350, 290,
330, 480, 360, 330, 120, 280, 410, 420, 360, 370, 330, 330, 330,
180, 200, 300, 360, 390, 270, 600, 300, 360, 130, 430, 470, 170,
620, 300, 360, 440, 600, 530, 350, 500, 560, 570, 420, 450, 460,
460, 520, 360, 170, 160, 180, 180, 370, 280, 160, 490, 320, 220,
230, 500, 410, 450, 310, 170, 500, 290, 500, 420, 510, 470, 480,
480, 480, 650, 420, 460, 560, 240, 240, 180, 190, 340, 90, 90
), `Fat (g)` = c(5, 6, 22, 23, 22, 16, 17, 12, 22, 16, 5, 15,
21, 18, 22, 24, 15, 1, 16, 23, 15, 23, 24, 28, 23, 3.5, 3.5,
3, 8, 5, 14, 20, 10, 4, 12, 18, 27, 21, 18, 4.5, 1.5, 15, 17,
18, 9, 17, 17, 6, 9, 8, 13, 18, 21, 14, 25, 14, 18, 12, 23, 29,
13, 37, 8, 7, 26, 32, 29, 18, 28, 29, 23, 16, 27, 22, 24, 26,
12, 9, 8, 9, 9, 19, 13, 2.5, 27, 17, 2.5, 6, 28, 18, 23, 22,
7, 30, 10, 19, 17, 27, 25, 27, 17, 20, 29, 18, 21, 23, 2.5, 2.5,
14, 18, 13, 8, 0), `Carb. (g)` = c(50, 70, 45, 56, 52, 53, 61,
28, 38, 36, 30, 42, 60, 38, 43, 64, 56, 58, 57, 36, 64, 53, 46,
55, 54, 14, 53, 14, 30, 45, 29, 68, 47, 64, 40, 39, 56, 36, 39,
18, 56, 63, 61, 45, 67, 44, 44, 57, 23, 28, 44, 45, 45, 27, 80,
34, 33, 7, 34, 29, 9, 62, 43, 52, 44, 47, 44, 31, 46, 53, 67,
49, 34, 45, 40, 53, 42, 23, 23, 23, 24, 32, 18, 28, 40, 28, 43,
28, 41, 43, 42, 9, 13, 35, 33, 57, 42, 37, 35, 42, 57, 49, 80,
47, 31, 55, 39, 42, 12, 6, 38, 5, 24), `Fiber (g)` = c(3, 7,
3, 2, 2, 1, 2, 1, 0, 1, 2, 2, 2, 1, 2, 2, 2, 3, 1, 0, 1, 2, 3,
3, 3, 0, 2, 0, 0, 0, 1, 1, 2, 8, 3, 1, 1, 3, 1, 0, 2, 2, 1, 1,
2, 1, 1, 7, 0, 0, 1, 1, 2, 2, 10, 2, 3, 3, 3, 4, 2, 10, 2, 3,
7, 6, 2, 1, 4, 7, 11, 5, 4, 4, 5, 5, 5, 0, 1, 0, 0, 1, 6, 4,
1, 1, 5, 2, 1, 2, 1, 0, 1, 3, 6, 4, 9, 5, 3, 7, 3, 3, 21, 3,
2, 3, 3, 2, 3, 3, 0, 4, 4), `Protein (g)` = c(12, 10, 10, 7,
6, 6, 5, 5, 2, 8, 2, 4, 7, 6, 7, 6, 8, 9, 5, 2, 6, 5, 6, 7, 5,
3, 11, 1, 6, 2, 3, 6, 3, 17, 5, 3, 5, 8, 4, 2, 9, 6, 6, 3, 5,
2, 2, 12, 2, 6, 3, 3, 6, 10, 19, 11, 17, 2, 21, 21, 7, 13, 19,
22, 10, 32, 23, 16, 18, 24, 27, 22, 20, 20, 23, 20, 24, 1, 2,
2, 1, 18, 18, 5, 21, 14, 5, 16, 15, 21, 24, 19, 13, 26, 19, 26,
27, 24, 21, 16, 23, 22, 23, 11, 29, 34, 14, 14, 4, 7, 18, 1,
1)), row.names = c(NA, -113L), spec = structure(list(cols = list(
food_name = structure(list(), class = c("collector_character",
"collector")), Calories = structure(list(), class = c("collector_double",
"collector")), `Fat (g)` = structure(list(), class = c("collector_double",
"collector")), `Carb. (g)` = structure(list(), class = c("collector_double",
"collector")), `Fiber (g)` = structure(list(), class = c("collector_double",
"collector")), `Protein (g)` = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x0000017f75094f80>, class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))
你这里有一些问题。
- 请post一些样本数据,比如说,在你未来的MRE中大约有10条记录。您当前的示例数据在创建数据框时出现了一些问题。
- 通过只创建一次反应对象并重复使用它们来简化您的代码。无需在输出对象中创建反应对象。
- 由于卡路里变量是字符,您需要 non-missing 个数值来将其与您的 sliderInput 范围进行比较以执行适当的过滤。
- 使用
.data[[input$var]]
绘图,因为 input$var
仅提供字符值
完整的工作代码:
category_drinks <- c("Calories", "Fat (g)", "Carb. (g)", "Fiber (g)", "Protein", "Sodium")
category_food <- c("Calories", "Fat (g)", "Carb. (g)", "Fiber (g)", "Protein (g)")
drink_name = c("Cool Lime Starbucks Refreshers1 Beverage",
"Ombré Pink Drink", "Pink Drink", "Strawberry Acai Starbucks Refreshers1 Beverage",
"Very Berry Hibiscus Starbucks Refreshers1 Beverage", "Violet Drink",
"Evolution Fresh1 Cold-Pressed Apple Berry Juice", "Evolution Fresh1 Defense Up",
"Evolution Fresh1 Organic Ginger Limeade", "Iced Coffee",
"Iced Coffee with Milk", "Iced Espresso Classics - Vanilla Latte")
Calories = c("45.00", "-", "-", "80.00",
"60.00", "350", "400", "-", "110.00", "0.00", "-", "130.00")
food_name = c("Chonga Bagel", "8-Grain Roll", "Chewy Chocolate Cookie", "Chocolate Chip Cookie",
"Almond Croissant", "Apple Fritter", "Banana Nut Bread", "Blueberry Muffin with Yogurt and Honey",
"Blueberry Scone", "Butter Croissant", "Butterfly Cookie", "Cheese Danish")
drinks <- data.frame(drink_name,Calories)
foods <- data.frame(food_name,Calories)
ui <- fluidPage(
# Application title
titlePanel("Starbucks Nutition Information"),
sidebarLayout(
sidebarPanel(
h5("Use the dropdown to choose the desired nutritional information."),
selectInput(inputId = "drink_criteria", label = "Drink Criteria", choices = category_drinks, selected = "Calories"),
selectInput(inputId = "food_criteria", label = "Food Criteria", choices = category_food, selected = "Calories"),
h5("Use the sliders to select the desired range for the selected nutritional information."),
sliderInput(inputId = "max", label = "Maximum", min = 0, max = 700, value = 300),
sliderInput(inputId = "min", label = "Minimum", min = 0, max = 500, value = 50),
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel(title = "Drink Chart",
plotOutput(outputId = "drink_plot")
),
tabPanel(title= "Drink Table",
DTOutput(outputId = "drink_table")
),
tabPanel(title = "Food Chart",
plotOutput(outputId = "food_plot")
),
tabPanel(title = "Food Table",
DTOutput(outputId = "food_table")
),
tabPanel(title = "About",
h2("About the App"),
h3("This app was built to show the nutritional values for a variety of Starbucks drinks and foods."),
h2("How to Use"),
h3("All the controls for this app are located in the sidebar on the left. To choose the nutritional information you would like to see in the graphs
select it from the dropdown. Once the desired nutitional information is picked use the sliders below it to select the range you would like to
view."),
h2("What You See"),
h3("Each of Food and Drink has two tabs. The first is a graph showing a list of items within the selected range.
The second is a table of all the resusts within the selected range. This allows you to view the other nutritional
infromation about the items." ),
h5("This app was created by Adam Maier for DSBA 5122"),
h5("The data was sourced from: https://www.kaggle.com/datasets/starbucks/starbucks-menu")
)
)
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
drinksinput <- reactive({
drinks$mycal <- as.numeric(drinks[[input$drink_criteria]])
drinks %>% dplyr::filter(input$drink_criteria != "-" ) %>%
dplyr::filter(mycal >= input$min, mycal <= input$max) %>% select(-c(mycal))
})
foodsinput <- reactive({
foods$mycal <- as.numeric(foods[[input$food_criteria]])
foods %>% dplyr::filter(input$food_criteria != "-" ) %>%
dplyr::filter(mycal >= input$min, mycal <= input$max) %>% select(-c(mycal))
})
#render both of the plots
output$drink_plot <- renderPlot({
drinksdf <- drinksinput()
ggplot(drinksdf) +
geom_col(aes(x = drink_name, y = .data[[input$drink_criteria]])) +
labs(x = "Drink", y = input$drink_criteria) +
theme_bw() +
scale_x_discrete(guide = guide_axis(angle = 90))
})
output$food_plot <- renderPlot({
foodsdf <- foodsinput()
ggplot(foodsdf) +
geom_col(aes(x = food_name, y = .data[[input$food_criteria]])) +
labs(x = "Food", y = input$food_criteria) +
theme_bw() +
scale_x_discrete(guide = guide_axis(angle = 90))
})
#render both of the tables
output$drink_table <- renderDT({DT::datatable(drinksinput())})
output$food_table <- renderDT({DT::datatable(foodsinput())})
}
# Run the application
shinyApp(ui = ui, server = server)
就像我在标题中所说的那样,我无法让这个 R Shiny 应用程序的过滤器部分正常工作。当我只有图的过滤器时,我得到错误“美学必须是长度 1 或与数据 (1) 相同:x”。在此代码块中,我在表中也有过滤器,但出现错误“ .getReactiveEnvironment()$currentContext() 中的错误: 没有活动的反应上下文不允许操作。
- 您试图做一些只能在被动消费者内部完成的事情。"
我的反应式语句抛出这些错误我做错了什么?
'''
library(shiny)
library(ggplot2)
library(tidyverse)
library(DT)
library(dplyr)
drinks <- read_csv("data/starbucks-menu-nutrition-drinks.csv")
foods <- read_csv("data/starbucks-menu-nutrition-food.csv")
category_drinks <- c("Calories", "Fat (g)", "Carb. (g)", "Fiber (g)", "Protein", "Sodium")
category_food <- c("Calories", "Fat (g)", "Carb. (g)", "Fiber (g)", "Protein (g)")
ui <- fluidPage(
# Application title
titlePanel("Starbucks Nutition Information"),
sidebarLayout(
sidebarPanel(
h5("Use the dropdown to choose the desired nutritional information."),
selectInput(inputId = "drink_criteria", label = "Drink Criteria", choices = category_drinks, selected = "Calories"),
selectInput(inputId = "food_criteria", label = "Food Criteria", choices = category_food, selected = "Calories"),
h5("Use the sliders to select the desired range for the selected nutritional information."),
sliderInput(inputId = "max", label = "Maximum", min = 0, max = 700, value = 300),
sliderInput(inputId = "min", label = "Minimum", min = 0, max = 500, value = 50),
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel(title = "Drink Chart",
plotOutput(outputId = "drink_plot")
),
tabPanel(title= "Drink Table",
DT::dataTableOutput(outputId = "drink_table")
),
tabPanel(title = "Food Chart",
plotOutput(outputId = "food_plot")
),
tabPanel(title = "Food Table",
DT::dataTableOutput(outputId = "food_table")
),
tabPanel(title = "About",
h2("About the App"),
h3("This app was built to show the nutritional values for a variety of Starbucks drinks and foods."),
h2("How to Use"),
h3("All the controls for this app are located in the sidebar on the left. To choose the nutritional information you would like to see in the graphs
select it from the dropdown. Once the desired nutitional information is picked use the sliders below it to select the range you would like to
view."),
h2("What You See"),
h3("Each of Food and Drink has two tabs. The first is a graph showing a list of items within the selected range.
The second is a table of all the resusts within the selected range. This allows you to view the other nutritional
infromation about the items." ),
h5("This app was created by Adam Maier for DSBA 5122"),
h5("The data was sourced from: https://www.kaggle.com/datasets/starbucks/starbucks-menu")
)
)
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
drinksinput <- reactive({
drinks %>%
filter(input$drink_criteria != "-",
input$drink_criteria >= input$min,
input$drink_criteria <= input$max)
}),
drinksdf <- drinksinput())})
#render both of the plots
output$drink_plot <- renderPlot({
drinksinput <- reactive({
drinks %>%
filter(input$drink_criteria != "-",
input$drink_criteria >= input$min,
input$drink_criteria <= input$max)
})
drinksdf <- drinksinput()
ggplot(drinksdf) +
geom_col(aes(x = drink_name, y = input$drink_criteria)) +
labs(x = "Drink", y = input$drink_criteria) +
theme_bw() +
scale_x_discrete(guide = guide_axis(angle = 90))
})
output$food_plot <- renderPlot({
foodsinput <- reactive({
foods %>%
filter(input$food_criteria != "-",
input$food_criteria >= input$min,
input$food_criteria <= input$max)
})
foodsdf <- foodsinput()
ggplot(foodsdf) +
geom_col(aes(x = food_name, y = input$food_criteria)) +
labs(x = "Food", y = input$food_criteria) +
theme_bw() +
scale_x_discrete(guide = guide_axis(angle = 90))
})
#render both of the tables
output$drink_table <- DT::renderDataTable({DT::datatable(
drinksinput <- reactive({
drinks %>%
filter(input$drink_criteria != "-",
input$drink_criteria >= input$min,
input$drink_criteria <= input$max)
}),
drinksdf <- drinksinput())})
output$food_table <- DT::renderDataTable({DT::datatable(
foodsinput <- reactive({
foods %>%
filter(input$food_criteria != "-",
input$food_criteria >= input$min,
input$food_criteria <= input$max)
}),
foodsdf <- foodsinput()
)})
}
# Run the application
shinyApp(ui = ui, server = server)
'''
dput输出
> dput(drinks)
structure(list(drink_name = c("Cool Lime Starbucks Refreshers1 Beverage",
"Ombré Pink Drink", "Pink Drink", "Strawberry Acai Starbucks Refreshers1 Beverage",
"Very Berry Hibiscus Starbucks Refreshers1 Beverage", "Violet Drink",
"Evolution Fresh1 Cold-Pressed Apple Berry Juice", "Evolution Fresh1 Defense Up",
"Evolution Fresh1 Organic Ginger Limeade", "Iced Coffee",
"Iced Coffee with Milk", "Iced Espresso Classics - Vanilla Latte",
"Iced Espresso Classics - Caffe Mocha", "Iced Espresso Classics - Caramel Macchiato",
"Starbucks® Caramel Iced Coffee", "Starbucks® Iced Coffee + Milk",
"Starbucks® Low Calorie Iced Coffee + Milk", "Starbucks® Vanilla Iced Coffee",
"Shaken Sweet Tea", "Tazo® Bottled Berry Blossom White", "Tazo® Bottled Black Mango",
"Tazo® Bottled Black with Lemon", "Tazo® Bottled Brambleberry",
"Tazo® Bottled Giant Peach", "Tazo® Bottled Iced Passion", "Tazo® Bottled Lemon Ginger",
"Tazo® Bottled Organic Black Lemonade", "Tazo® Bottled Organic Iced Black Tea",
"Tazo® Bottled Organic Iced Green Tea", "Tazo® Bottled Plum Pomegranate",
"Tazo® Bottled Tazoberry", "Tazo® Bottled White Cranberry", "Teavana® Shaken Iced Black Tea",
"Teavana® Shaken Iced Black Tea Lemonade", "Teavana® Shaken Iced Green Tea",
"Teavana® Shaken Iced Green Tea Lemonade", "Teavana® Shaken Iced Passion Tango1 Tea",
"Teavana® Shaken Iced Passion Tango1 Tea Lemonade", "Teavana® Shaken Iced Peach Green Tea",
"Iced Espresso Classics - Vanilla Latte", "Iced Espresso Classics - Caffe Mocha",
"Iced Espresso Classics - Caramel Macchiato", "Iced Espresso Classics - Skinny Caramel Macchiato",
"Iced Espresso Classics - Skinny Vanilla Latte", "Starbucks Doubleshot® Energy Coffee Drink",
"Starbucks Doubleshot® Energy Mexican Mocha", "Starbucks Doubleshot® Energy Mocha Drink",
"Starbucks Doubleshot® Energy Vanilla Drink", "Starbucks Doubleshot® Energy White Chocolate Drink",
"Starbucks Doubleshot® Energy+Cinnamon Dolce Drink", "Starbucks Doubleshot® Espresso Cubano",
"Starbucks Doubleshot® Espresso Drink", "Starbucks Doubleshot® Espresso Salted Caramel Cream",
"Starbucks Doubleshot® Light Espresso Drink", "Starbucks Doubleshot® Protein Caramel",
"Starbucks Refreshers1 Raspberry Pomegranate", "Starbucks Refreshers1 Strawberry Lemonade",
"Starbucks® Bottled Caramel Frappuccino® Coffee Drink", "Starbucks® Bottled Coffee Frappuccino® Coffee Drink",
"Starbucks® Bottled Cold Brew Black, Unsweetened", "Starbucks® Bottled Cold Brew Cocoa and Honey with Cream",
"Starbucks® Bottled Dulce de Leche Frappuccino® Coffee Drink",
"Starbucks® Bottled Mocha Frappuccino® Coffee Drink", "Starbucks® Bottled Mocha Light Frappuccino® Coffee Drink",
"Starbucks® Bottled S'mores Frappuccino® Coffee Drink", "Starbucks® Bottled Vanilla Frappuccino® Coffee Drink",
"Starbucks® Bottled Vanilla Light Frappuccino® Coffee Drink",
"Starbucks® Caramel Iced Coffee", "Starbucks® Doubleshot Protein Dark Chocolate",
"Starbucks® Doubleshot Protein Vanilla", "Starbucks® Iced Coffee + Milk",
"Starbucks® Iced Coffee Caramel", "Starbucks® Iced Coffee Light Sweetened",
"Starbucks® Iced Coffee Unsweetened", "Starbucks® Low Calorie Iced Coffee + Milk",
"Starbucks® Refreshers1 - Black Cherry Limeade with Coconut Water",
"Starbucks® Vanilla Iced Coffee", "Tazo® Bottled Berry Blossom White",
"Tazo® Bottled Black Mango", "Tazo® Bottled Black with Lemon",
"Tazo® Bottled Brambleberry", "Tazo® Bottled Giant Peach", "Tazo® Bottled Iced Passion",
"Tazo® Bottled Lemon Ginger", "Tazo® Bottled Organic Black Lemonade",
"Tazo® Bottled Organic Iced Black Tea", "Tazo® Bottled Organic Iced Green Tea",
"Tazo® Bottled Plum Pomegranate", "Tazo® Bottled Tazoberry",
"Tazo® Bottled White Cranberry", "Teavana® Mango Black Tea",
"Teavana® Passion Tango® Herbal Tea", "Teavana® Peach Green Tea",
"White Chocolate Mocha Bottled Frappuccino", "Blonde Roast",
"Caffè Misto", "Clover® Brewed Coffee", "Coffee Traveler", "Decaf Pike Place® Roast",
"Featured Dark Roast", "Iced Coffee", "Iced Coffee with Milk",
"Nariño 70 Cold Brew", "Nariño 70 Cold Brew with Milk", "Nitro Cold Brew",
"Nitro Cold Brew with Sweet Cream", "Pike Place® Roast", "Toasted Coconut Cold Brew",
"Vanilla Sweet Cream Cold Brew", "Hot Chocolate", "Starbucks® Signature Hot Chocolate",
"Caffè Americano", "Caffè Latte", "Caffè Mocha", "Cappuccino",
"Caramel Macchiato", "Cascara Coconutmilk Latte", "Cinnamon Dolce Latte",
"Coconutmilk Mocha Macchiato", "Espresso", "Espresso Con Panna",
"Espresso Macchiato", "Flat White", "Iced Caffè Americano", "Iced Caffè Latte",
"Iced Caffè Mocha", "Iced Caramel Macchiato", "Iced Cascara Coconutmilk Latte",
"Iced Cinnamon Dolce Latte", "Iced Coconutmilk Mocha Macchiato",
"Iced Skinny Cinnamon Dolce Latte", "Iced Skinny Mocha", "Iced Vanilla Latte",
"Frappuccino® Blended Crème",
"The Pokémon1 GO Frappuccino® Blended Beverage", "Vanilla Bean Crème Frappuccino® Blended Crème",
"White Chocolate Mocha Frappuccino® Blended Coffee", "Cinnamon Dolce Crème",
"Organic Chocolate Milk Box", "Steamed Apple Juice", "Vanilla Crème",
"Chocolate Smoothie", "Strawberry Smoothie", "Ginger Ale", "Lemon Ale",
"Orange Cream Soda"), Calories = c("45.00", "-", "-", "80.00",
"60.00", "-", "-", "-", "110.00", "0.00", "-", "130.00", "140.00",
"130.00", "-", "-", "-", "-", "80.00", "60.00", "150.00", "140.00",
"140.00", "150.00", "70.00", "120.00", "140.00", "60.00", "120.00",
"140.00", "150.00", "140.00", "30.00", "70.00", "30.00", "70.00",
"30.00", "90.00", "60.00", "130.00", "140.00", "130.00", "-",
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "90.00",
"90.00", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-",
"210.00", "200.00", "-", "60.00", "50.00", "10.00", "-", "-",
"-", "60.00", "150.00", "140.00", "140.00", "150.00", "70.00",
"120.00", "140.00", "60.00", "120.00", "140.00", "150.00", "140.00",
"-", "-", "-", "-", "5.00", "-", "10.00", "-", "5.00", "5.00",
"5.00", "-", "5.00", "0.00", "5.00", "70.00", "5.00", "-", "110.00",
"320.00", "430.00", "-", "190.00", "290.00", "120.00", "250.00",
"-", "260.00", "250.00", "-", "-", "-", "180.00", "-", "130.00",
"230.00", "250.00", "-", "200.00", "260.00", "-", "-", "190.00",
"300.00", "190.00", "190.00", "-", "45.00", "250.00", "360.00",
"-", "-", "-", "-", "-", "-", "350.00", "-", "-", "-", "110.00",
"-", "-", "-", "-", "-", "-", "-", "280.00", "140.00", "-", "-",
"-", "-", "-", "-", "-", "-", "200.00", "-", "-", "200.00", "320.00",
"300.00", "-", "-", "-"), `Fat (g)` = c("0.00", "-", "-", "0.00",
"0.00", "-", "-", "-", "0.00", "0.00", "-", "2.50", "2.50", "2.50",
"-", "-", "-", "-", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
"0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
"0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "2.50",
"2.50", "2.50", "-", "-", "-", "-", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "0.00", "0.00", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "-", "-", "2.50", "2.50", "-", "0.00", "0.00",
"0.00", "-", "-", "-", "0.00", "0.00", "0.00", "0.00", "0.00",
"0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
"-", "-", "-", "-", "0.00", "-", "0.00", "-", "0.00", "0.00",
"0.00", "-", "0.00", "0.00", "0.00", "5.00", "0.00", "-", "6.00",
"9.00", "26.00", "-", "7.00", "8.00", "4.00", "7.00", "-", "6.00",
"9.00", "-", "-", "-", "7.00", "-", "4.50", "6.00", "7.00", "-",
"4.00", "9.00", "-", "-", "4.00", "8.00", "7.00", "7.00", "-",
"1.00", "6.00", "11.00", "-", "-", "-", "-", "-", "-", "4.50",
"-", "-", "-", "0.00", "-", "-", "-", "-", "-", "-", "-", "2.50",
"0.50", "-", "-", "-", "-", "-", "-", "-", "-", "6.00", "-",
"-", "6.00", "5.00", "2.00", "-", "-", "-"), `Carb. (g)` = c("11.00",
"-", "-", "18.00", "14.00", "-", "-", "-", "28.00", "0.00", "-",
"21.00", "23.00", "21.00", "-", "-", "-", "-", "19.00", "15.00",
"38.00", "35.00", "35.00", "37.00", "17.00", "31.00", "35.00",
"15.00", "31.00", "35.00", "38.00", "35.00", "8.00", "17.00",
"8.00", "17.00", "8.00", "24.00", "15.00", "21.00", "23.00",
"21.00", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-",
"-", "-", "27.00", "27.00", "-", "-", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "33.00", "34.00", "-", "13.00", "11.00",
"2.00", "-", "-", "-", "15.00", "38.00", "35.00", "35.00", "37.00",
"17.00", "31.00", "35.00", "15.00", "31.00", "35.00", "38.00",
"35.00", "-", "-", "-", "-", "0.00", "-", "0.00", "-", "0.00",
"0.00", "0.00", "-", "0.00", "0.00", "0.00", "5.00", "0.00",
"-", "14.00", "47.00", "45.00", "-", "19.00", "42.00", "12.00",
"35.00", "-", "40.00", "32.00", "-", "-", "-", "18.00", "-",
"13.00", "36.00", "37.00", "-", "34.00", "34.00", "-", "-", "30.00",
"47.00", "19.00", "19.00", "-", "5.00", "37.00", "53.00", "-",
"-", "-", "-", "-", "-", "64.00", "-", "-", "-", "24.00", "-",
"-", "-", "-", "-", "-", "-", "60.00", "28.00", "-", "-", "-",
"-", "-", "-", "-", "-", "28.00", "-", "-", "28.00", "53.00",
"60.00", "-", "-", "-"), `Fiber (g)` = c("0.00", "-", "-", "1.00",
"1.00", "-", "-", "-", "0.00", "0.00", "-", "0.00", "0.00", "0.00",
"-", "-", "-", "-", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
"0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
"0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
"0.00", "0.00", "-", "-", "-", "-", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "0.00", "0.00", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "-", "-", "2.00", "2.00", "-", "0.00", "0.00",
"0.00", "-", "-", "-", "0.00", "0.00", "0.00", "0.00", "0.00",
"0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
"-", "-", "-", "-", "0.00", "-", "0.00", "-", "0.00", "0.00",
"0.00", "-", "0.00", "0.00", "0.00", "0.00", "0.00", "-", "0.00",
"4.00", "5.00", "-", "0.00", "4.00", "0.00", "0.00", "-", "0.00",
"0.00", "-", "-", "-", "0.00", "-", "0.00", "4.00", "0.00", "-",
"0.00", "0.00", "-", "-", "0.00", "0.00", "0.00", "0.00", "-",
"0.00", "0.00", "0.00", "-", "-", "-", "-", "-", "-", "0.00",
"-", "-", "-", "0.00", "-", "-", "-", "-", "-", "-", "-", "2.00",
"1.00", "-", "-", "-", "-", "-", "-", "-", "-", "0.00", "-",
"-", "0.00", "8.00", "7.00", "-", "-", "-"), Protein = c("0.00",
"-", "-", "0.00", "0.00", "-", "-", "-", "0.00", "0.00", "-",
"5.00", "5.00", "5.00", "-", "-", "-", "-", "0.00", "0.00", "0.00",
"0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
"0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
"0.00", "0.00", "5.00", "5.00", "5.00", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "-", "-", "-", "-", "0.00", "0.00", "-",
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "20.00", "20.00",
"-", "1.00", "1.00", "1.00", "-", "-", "-", "0.00", "0.00", "0.00",
"0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
"0.00", "0.00", "-", "-", "-", "-", "1.00", "-", "1.00", "-",
"1.00", "1.00", "0.00", "-", "0.00", "0.00", "0.00", "1.00",
"1.00", "-", "1.00", "14.00", "12.00", "-", "13.00", "13.00",
"8.00", "10.00", "-", "11.00", "12.00", "-", "-", "-", "12.00",
"-", "8.00", "9.00", "10.00", "-", "7.00", "11.00", "-", "-",
"7.00", "10.00", "12.00", "12.00", "-", "3.00", "12.00", "14.00",
"-", "-", "-", "-", "-", "-", "15.00", "-", "-", "-", "3.00",
"-", "-", "-", "-", "-", "-", "-", "4.00", "4.00", "-", "-",
"-", "-", "-", "-", "-", "-", "10.00", "-", "-", "10.00", "20.00",
"16.00", "-", "-", "-"), Sodium = c("10.00", "-", "-", "10.00",
"10.00", "-", "-", "-", "5.00", "0.00", "-", "65.00", "90.00",
"65.00", "-", "-", "-", "-", "10.00", "10.00", "15.00", "10.00",
"15.00", "15.00", "10.00", "10.00", "10.00", "10.00", "10.00",
"10.00", "15.00", "10.00", "5.00", "0.00", "5.00", "5.00", "5.00",
"0.00", "0.00", "65.00", "90.00", "65.00", "-", "-", "-", "-",
"-", "-", "-", "-", "-", "-", "-", "-", "-", "0.00", "0.00",
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "115.00",
"120.00", "-", "0.00", "0.00", "0.00", "-", "-", "-", "10.00",
"15.00", "10.00", "15.00", "15.00", "10.00", "10.00", "10.00",
"10.00", "10.00", "10.00", "15.00", "10.00", "-", "-", "-", "-",
"10.00", "-", "10.00", "-", "10.00", "10.00", "5.00", "-", "15.00",
"0.00", "10.00", "20.00", "10.00", "-", "25.00", "160.00", "115.00",
"-", "170.00", "140.00", "100.00", "150.00", "-", "150.00", "180.00",
"-", "-", "-", "160.00", "-", "115.00", "90.00", "150.00", "-",
"95.00", "180.00", "-", "-", "100.00", "190.00", "160.00", "160.00",
"-", "40.00", "150.00", "240.00", "-", "-", "-", "-", "-", "-",
"0.00", "-", "-", "-", "200.00", "-", "-", "-", "-", "-", "-",
"-", "220.00", "180.00", "-", "-", "-", "-", "-", "-", "-", "-",
"135.00", "-", "-", "135.00", "170.00", "130.00", "-", "-", "-"
)), row.names = c(NA, -177L), spec = structure(list(cols = list(
drink_name = structure(list(), class = c("collector_character",
"collector")), Calories = structure(list(), class = c("collector_character",
"collector")), `Fat (g)` = structure(list(), class = c("collector_character",
"collector")), `Carb. (g)` = structure(list(), class = c("collector_character",
"collector")), `Fiber (g)` = structure(list(), class = c("collector_character",
"collector")), Protein = structure(list(), class = c("collector_character",
"collector")), Sodium = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x0000017f750959e0>, class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))
> dput(foods)
structure(list(food_name = c("Chonga Bagel", "8-Grain Roll",
"Almond Croissant", "Apple Fritter", "Banana Nut Bread", "Blueberry Muffin with Yogurt and Honey",
"Blueberry Scone", "Butter Croissant", "Butterfly Cookie", "Cheese Danish",
"Chewy Chocolate Cookie", "Chocolate Chip Cookie", "Chocolate Chunk Muffin",
"Chocolate Croissant", "Chocolate Hazelnut Croissant", "Chocolate Marble Loaf Cake",
"Cinnamon Morning Bun", "Cinnamon Raisin Bagel", "Classic Coffee Cake",
"Cookie Butter Bar", "Cranberry Orange Scone", "Devil's Food Doughnut",
"Double Chocolate Brownie", "Double Chocolate Chunk Brownie",
"Double Chocolate Loaf Cake", "Everybody's Favorite - Bantam Bagel (2 Pack)",
"Everything Bagel with Cheese", "Frappuccino® Cookie Straw",
"French Toast - Bantam Bagels (2 Pack)", "Gluten-Free Marshmallow Dream Bar",
"Ice Cream Cone Sugar Cookie", "Iced Lemon Pound Cake", "Michigan Cherry Oat Bar",
"Multigrain Bagel", "Oatmeal Cookie", "Octopus Cookie", "Old-Fashioned Glazed Doughnut",
"Peanut butter cup cookie", "Pecan Tart", "Petite Vanilla Bean Scone",
"Plain Bagel", "Pumpkin Bread", "Raspberry Swirl Loaf Cake",
"Raspberry Whoopie Pie", "Reduced-Fat Cinnamon Swirl Coffee Cake",
"S'mores Bar", "S'mores Bar Multipack", "Sprouted Grain Vegan Bagel",
"Strawberry Cake Pop", "The Classic - Bantam Bagels (2 Pack)",
"Toffeedoodle", "Vanilla Bean Whoopie Pie", "Vermont Maple Walnut Muffin",
"Volpi1 Pepperoni & Tomato Savory Foldover", "Strawberries & Jam Sandwich",
"Burrata & Basil Pesto Small Sandwich", "Cage-Free Eggs & Seasoned Grains Side Salad",
"Cauliflower Tabbouleh Side Salad", "Cubano Sandwich", "Farmers' Market Protein Box",
"Garden Greens & Shaved Parmesan Side Salad", "Green Goddess Avocado Salad",
"Herbed Chicken & Fig Spread Small Sandwich", "Honey BBQ Sriracha Chicken Sandwich",
"Roasted Carrot & Kale Side Salad", "Roasted Turkey & Dill Havarti Sandwich",
"Seared Steak & Cheddar Sandwich", "Slow-Roasted Ham & Beecher's Flagship Cheese Small Sandwich",
"Smoked Mozzarella & Roasted Pepper Sandwich", "Turkey & Fire-Roasted Corn Salad",
"Za'atar Chicken & Lemon Tahini Salad", "BBQ Chicken Protein Box",
"Cheese & Fruit Protein Box", "Chicken Wrap Protein Box", "Eggs & Cheese Protein Box",
"PB&J Protein Box", "Smoked Turkey Protein Box", "Birthday Cake Pop",
"Chocolate Cake Pop", "Chocolate Chip Cookie Dough Cake Pop",
"Pirate Cake Pop", "Bacon Gouda & Egg Breakfast Sandwich", "Certified Gluten-Free Breakfast Sandwich",
"Classic Whole-Grain Oatmeal", "Double-Smoked Bacon Cheddar & Egg Sandwich",
"Ham & Cheese Croissant", "Hearty Blueberry Oatmeal", "Reduced-Fat Turkey Bacon & Cage Free Egg White Breakfast Sandwich",
"Sausage Cheddar & Egg Breakfast Sandwich", "Seared Steak Egg & Tomatillo Wrap",
"Slow-Roasted Ham Swiss & Egg Breakfast Sandwich", "Sous Vide Egg Bites: Bacon & Gruyere",
"Sous Vide Egg Bites: Egg White & Red Pepper", "Spicy Chorizo Monterey Jack & Egg Breakfast Sandwich",
"Spinach Feta & Cage Free Egg White Breakfast Wrap", "Ancho Chipotle Chicken Panini",
"Chicken & Quinoa Protein Bowl with Black Beans and Greens",
"Chicken Artichoke on Ancient Grain Flatbread", "Chicken BLT Salad Sandwich",
"Egg Salad Sandwich", "Homestyle Chicken & Double-Smoked Bacon",
"Italian-Style Ham & Spicy Salami", "Lentils & Vegetable Protein Bowl with Brown Rice",
"Roasted Tomato & Mozzarella Panini", "Turkey & Havarti Sandwich",
"Turkey Pesto Panini", "Berry Trio Yogurt", "Fresh Blueberries and Honey Greek Yogurt Parfait",
"Justin's Chocolate Hazelnut Butter", "Justin's Classic Almond Butter",
"Lemon Chiffon Yogurt", "Organic Avocado (Spread)", "Seasonal Fruit Blend"
), Calories = c(300, 380, 410, 460, 420, 380, 420, 240, 350,
320, 170, 310, 440, 330, 390, 490, 390, 270, 390, 360, 420, 430,
410, 490, 440, 100, 290, 90, 200, 240, 250, 470, 310, 350, 290,
330, 480, 360, 330, 120, 280, 410, 420, 360, 370, 330, 330, 330,
180, 200, 300, 360, 390, 270, 600, 300, 360, 130, 430, 470, 170,
620, 300, 360, 440, 600, 530, 350, 500, 560, 570, 420, 450, 460,
460, 520, 360, 170, 160, 180, 180, 370, 280, 160, 490, 320, 220,
230, 500, 410, 450, 310, 170, 500, 290, 500, 420, 510, 470, 480,
480, 480, 650, 420, 460, 560, 240, 240, 180, 190, 340, 90, 90
), `Fat (g)` = c(5, 6, 22, 23, 22, 16, 17, 12, 22, 16, 5, 15,
21, 18, 22, 24, 15, 1, 16, 23, 15, 23, 24, 28, 23, 3.5, 3.5,
3, 8, 5, 14, 20, 10, 4, 12, 18, 27, 21, 18, 4.5, 1.5, 15, 17,
18, 9, 17, 17, 6, 9, 8, 13, 18, 21, 14, 25, 14, 18, 12, 23, 29,
13, 37, 8, 7, 26, 32, 29, 18, 28, 29, 23, 16, 27, 22, 24, 26,
12, 9, 8, 9, 9, 19, 13, 2.5, 27, 17, 2.5, 6, 28, 18, 23, 22,
7, 30, 10, 19, 17, 27, 25, 27, 17, 20, 29, 18, 21, 23, 2.5, 2.5,
14, 18, 13, 8, 0), `Carb. (g)` = c(50, 70, 45, 56, 52, 53, 61,
28, 38, 36, 30, 42, 60, 38, 43, 64, 56, 58, 57, 36, 64, 53, 46,
55, 54, 14, 53, 14, 30, 45, 29, 68, 47, 64, 40, 39, 56, 36, 39,
18, 56, 63, 61, 45, 67, 44, 44, 57, 23, 28, 44, 45, 45, 27, 80,
34, 33, 7, 34, 29, 9, 62, 43, 52, 44, 47, 44, 31, 46, 53, 67,
49, 34, 45, 40, 53, 42, 23, 23, 23, 24, 32, 18, 28, 40, 28, 43,
28, 41, 43, 42, 9, 13, 35, 33, 57, 42, 37, 35, 42, 57, 49, 80,
47, 31, 55, 39, 42, 12, 6, 38, 5, 24), `Fiber (g)` = c(3, 7,
3, 2, 2, 1, 2, 1, 0, 1, 2, 2, 2, 1, 2, 2, 2, 3, 1, 0, 1, 2, 3,
3, 3, 0, 2, 0, 0, 0, 1, 1, 2, 8, 3, 1, 1, 3, 1, 0, 2, 2, 1, 1,
2, 1, 1, 7, 0, 0, 1, 1, 2, 2, 10, 2, 3, 3, 3, 4, 2, 10, 2, 3,
7, 6, 2, 1, 4, 7, 11, 5, 4, 4, 5, 5, 5, 0, 1, 0, 0, 1, 6, 4,
1, 1, 5, 2, 1, 2, 1, 0, 1, 3, 6, 4, 9, 5, 3, 7, 3, 3, 21, 3,
2, 3, 3, 2, 3, 3, 0, 4, 4), `Protein (g)` = c(12, 10, 10, 7,
6, 6, 5, 5, 2, 8, 2, 4, 7, 6, 7, 6, 8, 9, 5, 2, 6, 5, 6, 7, 5,
3, 11, 1, 6, 2, 3, 6, 3, 17, 5, 3, 5, 8, 4, 2, 9, 6, 6, 3, 5,
2, 2, 12, 2, 6, 3, 3, 6, 10, 19, 11, 17, 2, 21, 21, 7, 13, 19,
22, 10, 32, 23, 16, 18, 24, 27, 22, 20, 20, 23, 20, 24, 1, 2,
2, 1, 18, 18, 5, 21, 14, 5, 16, 15, 21, 24, 19, 13, 26, 19, 26,
27, 24, 21, 16, 23, 22, 23, 11, 29, 34, 14, 14, 4, 7, 18, 1,
1)), row.names = c(NA, -113L), spec = structure(list(cols = list(
food_name = structure(list(), class = c("collector_character",
"collector")), Calories = structure(list(), class = c("collector_double",
"collector")), `Fat (g)` = structure(list(), class = c("collector_double",
"collector")), `Carb. (g)` = structure(list(), class = c("collector_double",
"collector")), `Fiber (g)` = structure(list(), class = c("collector_double",
"collector")), `Protein (g)` = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x0000017f75094f80>, class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))
你这里有一些问题。
- 请post一些样本数据,比如说,在你未来的MRE中大约有10条记录。您当前的示例数据在创建数据框时出现了一些问题。
- 通过只创建一次反应对象并重复使用它们来简化您的代码。无需在输出对象中创建反应对象。
- 由于卡路里变量是字符,您需要 non-missing 个数值来将其与您的 sliderInput 范围进行比较以执行适当的过滤。
- 使用
.data[[input$var]]
绘图,因为input$var
仅提供字符值
完整的工作代码:
category_drinks <- c("Calories", "Fat (g)", "Carb. (g)", "Fiber (g)", "Protein", "Sodium")
category_food <- c("Calories", "Fat (g)", "Carb. (g)", "Fiber (g)", "Protein (g)")
drink_name = c("Cool Lime Starbucks Refreshers1 Beverage",
"Ombré Pink Drink", "Pink Drink", "Strawberry Acai Starbucks Refreshers1 Beverage",
"Very Berry Hibiscus Starbucks Refreshers1 Beverage", "Violet Drink",
"Evolution Fresh1 Cold-Pressed Apple Berry Juice", "Evolution Fresh1 Defense Up",
"Evolution Fresh1 Organic Ginger Limeade", "Iced Coffee",
"Iced Coffee with Milk", "Iced Espresso Classics - Vanilla Latte")
Calories = c("45.00", "-", "-", "80.00",
"60.00", "350", "400", "-", "110.00", "0.00", "-", "130.00")
food_name = c("Chonga Bagel", "8-Grain Roll", "Chewy Chocolate Cookie", "Chocolate Chip Cookie",
"Almond Croissant", "Apple Fritter", "Banana Nut Bread", "Blueberry Muffin with Yogurt and Honey",
"Blueberry Scone", "Butter Croissant", "Butterfly Cookie", "Cheese Danish")
drinks <- data.frame(drink_name,Calories)
foods <- data.frame(food_name,Calories)
ui <- fluidPage(
# Application title
titlePanel("Starbucks Nutition Information"),
sidebarLayout(
sidebarPanel(
h5("Use the dropdown to choose the desired nutritional information."),
selectInput(inputId = "drink_criteria", label = "Drink Criteria", choices = category_drinks, selected = "Calories"),
selectInput(inputId = "food_criteria", label = "Food Criteria", choices = category_food, selected = "Calories"),
h5("Use the sliders to select the desired range for the selected nutritional information."),
sliderInput(inputId = "max", label = "Maximum", min = 0, max = 700, value = 300),
sliderInput(inputId = "min", label = "Minimum", min = 0, max = 500, value = 50),
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel(title = "Drink Chart",
plotOutput(outputId = "drink_plot")
),
tabPanel(title= "Drink Table",
DTOutput(outputId = "drink_table")
),
tabPanel(title = "Food Chart",
plotOutput(outputId = "food_plot")
),
tabPanel(title = "Food Table",
DTOutput(outputId = "food_table")
),
tabPanel(title = "About",
h2("About the App"),
h3("This app was built to show the nutritional values for a variety of Starbucks drinks and foods."),
h2("How to Use"),
h3("All the controls for this app are located in the sidebar on the left. To choose the nutritional information you would like to see in the graphs
select it from the dropdown. Once the desired nutitional information is picked use the sliders below it to select the range you would like to
view."),
h2("What You See"),
h3("Each of Food and Drink has two tabs. The first is a graph showing a list of items within the selected range.
The second is a table of all the resusts within the selected range. This allows you to view the other nutritional
infromation about the items." ),
h5("This app was created by Adam Maier for DSBA 5122"),
h5("The data was sourced from: https://www.kaggle.com/datasets/starbucks/starbucks-menu")
)
)
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
drinksinput <- reactive({
drinks$mycal <- as.numeric(drinks[[input$drink_criteria]])
drinks %>% dplyr::filter(input$drink_criteria != "-" ) %>%
dplyr::filter(mycal >= input$min, mycal <= input$max) %>% select(-c(mycal))
})
foodsinput <- reactive({
foods$mycal <- as.numeric(foods[[input$food_criteria]])
foods %>% dplyr::filter(input$food_criteria != "-" ) %>%
dplyr::filter(mycal >= input$min, mycal <= input$max) %>% select(-c(mycal))
})
#render both of the plots
output$drink_plot <- renderPlot({
drinksdf <- drinksinput()
ggplot(drinksdf) +
geom_col(aes(x = drink_name, y = .data[[input$drink_criteria]])) +
labs(x = "Drink", y = input$drink_criteria) +
theme_bw() +
scale_x_discrete(guide = guide_axis(angle = 90))
})
output$food_plot <- renderPlot({
foodsdf <- foodsinput()
ggplot(foodsdf) +
geom_col(aes(x = food_name, y = .data[[input$food_criteria]])) +
labs(x = "Food", y = input$food_criteria) +
theme_bw() +
scale_x_discrete(guide = guide_axis(angle = 90))
})
#render both of the tables
output$drink_table <- renderDT({DT::datatable(drinksinput())})
output$food_table <- renderDT({DT::datatable(foodsinput())})
}
# Run the application
shinyApp(ui = ui, server = server)