2017-09-04 3 views
1

Ich bin relativ neu zu R und versuche, alleine zu lernen.R: Summe aus ausgewähltem Eingang erhalten

Ich möchte in einem glänzenden Dashboard ein Auswahlfeld erstellen, wo ich Produkte meiner Daten (.xls) auswählen und eine Summe erhalten kann.

Die Eingabe erfolgt über selectInput und selectize. Dies ist der Teil, der funktioniert :)

Wenn ich 1 Produkt wähle, bekomme ich die Kalorien dieses Produktes zurück ... so weit.

Mein Problem ist, dass wollen mehr Produkte als 1 und wählen Sie die Summe der Kalorien. Wie muss ich die Produkte des Eingabefeldes in meiner Tabelle identifizieren/suchen und wie bekomme ich die Summe davon?

Vielen Dank für Ihre Hilfe!

PS: Benötigen Sie weitere Informationen über Datei? nur zwei Säulen sind dafür wichtig: Produkt und Kalorien.

library(dplyr) 
library(plotly) 
library(readxl) 
library(shiny) 
library(shinydashboard) 

# Daten einlesen 
McDaten <- read_excel("~/Desktop/McDaten.xlsx") 
McDaten$kcal <- McDaten$`kcal (100g)` 

ui <- dashboardPage(
skin="red", 
dashboardHeader(title = "Analytics Dashboard", titleWidth = 290), 
dashboardSidebar(
width = 290, 
sidebarMenu(
    menuItem("Virtuelles Menü", tabName = "charts", icon = icon("cutlery")) 

) 
), 

    dashboardBody(


tabItems(

    tabItem(tabName = "charts", 
      fluidPage(
      br(), 
      fluidRow(
       column(4, 
        selectInput('in6', 'Menü', McDaten$Produkt, multiple=TRUE, selectize=TRUE)), 
       column(4,infoBoxOutput("progressBox")) 
      ) 
     ) 

)))) 


server <- function(input, output) { 

    output$progressBox <- renderInfoBox({ 
    b <- McDaten %>% 
    select(`kcal (Portion)`, Produkt) %>% 
    filter(McDaten$Produkt %in% input$in6) %>% 
    summarise(`kcal (Portion)`) 

infoBox(
    "Progress", paste0(b, " kcal"), icon = icon("list"), 
    color = "purple", fill = TRUE 
) 
    }) 

} 


shinyApp(ui, server) 

Antwort

0

Wir brauchen die choices = unique(McDaten$Produkt) in der 'ui' und in summarise den sum Bedarf für die Spalte von Interesse

-ui

ui <- dashboardPage(
    skin="red", 
    dashboardHeader(title = "Analytics Dashboard", titleWidth = 290), 
    dashboardSidebar(
    width = 290, 
    sidebarMenu(
     menuItem("Virtuelles Menü", tabName = "charts", icon = icon("cutlery")) 

    ) 
), 

    dashboardBody(


    tabItems(

     tabItem(tabName = "charts", 
       fluidPage(
       br(), 
       fluidRow(
        column(4, 
        selectInput('in6', 'Menü', 
         choices = unique(McDaten$Produkt), multiple=TRUE, selectize=TRUE)), 
        column(4,infoBoxOutput("progressBox")) 
       ) 
      ) 

    )))) 

-Server

server <- function(input, output) { 

    output$progressBox <- renderInfoBox({ 
    b <- McDaten %>% 
     select(`kcal (Portion)`, Produkt) %>% 
     filter(Produkt %in% input$in6) %>% 
     summarise(`kcal (Portion)` = sum(`kcal (Portion)`)) %>% 
     pull(`kcal (Portion)`) 


    infoBox(
     "Progress", paste0(b, " kcal"), icon = icon("list"), 
     color = "purple", fill = TRUE 
    ) 
    }) 

} 
angegeben werden

-run die App

shinyApp(ui, server) 

-Daten

set.seed(24) 
McDaten <- data.frame(Produkt = sample(LETTERS[1:5], 30, replace = TRUE), 
    `kcal (Portion)` = sample(1400:2000, 30, replace = TRUE), 
     stringsAsFactors= FALSE, check.names = FALSE) 

McDaten$kcal <- McDaten$`kcal (Portion)` 

-Ausgang

enter image description here

enter image description here

+1

Woooow es funktioniert! Ich danke dir sehr! :) Btw tolle Community! ;) – Berndit