R Shinydashboard在值框中显示所选输入的总和

时间:2019-03-07 11:49:58

标签: r shiny shinydashboard selectinput

我的问题与“ value4”有关,这是下面的代码中的一个valuebox。

我创建了一个选择输入,允许用户选择一个名称,基于该名称,我希望应用程序查找与所选名称关联的项目数(项目数=“ X..setup” ),然后在valuebox(“ value4”)中显示项目总数。

我遇到的问题是获取所有项目的总和。 请在下面找到我的代码:

    setups <- read.csv("C:/Users/obria/Desktop/setUps/setUp.csv",stringsAsFactors = F, header = TRUE)
View(setups)
head(setups)
searchDF <- setups[c(1,2,3,4,7,8,9,10,11)]
#lst.Owners <- as.list(unique(setups$Owners))
lst.Owners = as.character(setups$Owners)
Owners <- unique(lst.Owners)

userInput <- sum(str_count(setups$Over.all.Status.of.Project,"WIP")) %>% groub_by(Owners)

install.packages("dplyr")
install.packages("ggplot2")
library(ggplot2)
library(dplyr)
library(shiny)
library(shinydashboard)
library(stringr)
library (DT)

ui = dashboardPage(
  #Header
  dashboardHeader(title = "Set ups dashboard"),

  #Sidebar
  dashboardSidebar(
    sidebarMenu(
      menuItem("Dashboard", tabName = "dashboard"),
      menuItem("Search", tabName = "search"),
      menuItem("Break Down", tabName = "breakDown")
    )
  ),

  #Body
  dashboardBody(tabItems(
    # First tab content
    tabItem(tabName = "dashboard",
            fluidRow(
              valueBoxOutput("value1")
              ,valueBoxOutput("value2")
              ,valueBoxOutput("value3"),

              fluidRow(
                box(
                  title= "Owner Vs Set Ups"
                  ,status = "primary"
                  ,solidheader = TRUE
                  ,collapsible = TRUE
                  ,plotOutput("nameStatus", height = "300px")
                )
                ,box(
                  title= " Pant Vs Set Ups"
                  ,status = "primary"
                  ,solidheader = TRUE
                  ,collapsible = TRUE
                  ,plotOutput("plantSetUps", height = "300px"))))
    ),
    # Second tab content #
    tabItem(tabName = "search",
            fluidRow(
              h2("Search Set ups"),
              DT::dataTableOutput("mytable")
            )),

    # Third tab content #
    tabItem(tabName = "breakDown",
              h2("Search Set ups"),
            fluidRow(
              box(
              selectInput("selectVariable", "Select Variable:",
                          choices = Owners, 
                          selected = 1))),
            fluidRow(
              valueBoxOutput("value4")
            ))))
)
server = function(input, output) {

  # Get some data #

  # Total Set ups #
  totalSetUps <- sum(setups$X..setups)

  # Number of WIPs #
  workIP1 <- sum(str_count(setups$Over.all.Status.of.Project,"WIP"))
  workIP2 <- sum(str_count(setups$Over.all.Status.of.Project,"wip"))
  workInProgress <- (workIP1 + workIP2)

  # Number of Outstanding #
  outstanding <- sum(str_count(setups$Over.all.Status.of.Project,"Outstanding"))

  # Colonia - Test Val;ue box #
  #colonia <- sum(str_count(setups$Plant,"Colonia"))

  setUpByName <- reactive ({
    setups %>%
      filter(Owners == input$selectVariable) %>%
      sum(.$X..setups)
  })
  # Create the valueBoxOutput Content #
  output$value1 <- renderValueBox({
    valueBox(
      format(totalSetUps, format="d", big.mark=",")
      ,"Total Number of Set Ups"
      ,icon = icon("stats",lib="glyphicon")
      ,color = "purple")
  })
  output$value2 <- renderValueBox({
    valueBox(
      format(workInProgress, format="d", big.mark=",")
      ,"No. of project that are WIP"
      ,icon = icon("gbp",lib="glyphicon")
      ,color = "green")
  })
  output$value3 <- renderValueBox({
    valueBox(
      format(outstanding, format="d", big.mark=",")
      ,"No. of project that are Outstanding"
      ,icon = icon("menu-hamburger",lib="glyphicon")
      ,color = "yellow")
  })
  output$value4 <- renderValueBox({
    valueBox(
      format(setUpByName(), format="d", big.mark=",")
      ,"total # Set ups"
      ,icon = icon("menu-hamburger",lib="glyphicon")
      ,color = "yellow")
  })

  # Creating plot output content #
  output$nameStatus <- renderPlot({
    ggplot(data = setups, 
           aes(x=setups$Owners, y=setups$X..setup, fill=factor(Over.all.Status.of.Project))) + 
      geom_bar(position = "dodge", stat = "identity") + ylab("No. of Set ups") + 
      xlab("Owners") + theme(legend.position="bottom" 
                             ,plot.title = element_text(size=15, face="bold")) + 
      ggtitle("Owners vs No. of Set Ups") + labs(fill = "Status")
  })
  output$plantSetUps <- renderPlot({
    ggplot(data=setups, aes(x=setups$Plant, y= setups$X..setup)) +
      geom_bar(stat="identity", col="blue", fill="blue") +
      labs(title ="No of Set ups by plant")
  })
  output$mytable = DT::renderDataTable({
    setups
  })

  output$result <- renderText({
    paste("You chose", input$selectVariable)
  })

}


shinyApp(ui, server)

str(setups)

DF Columns Error

CodeError2 ShinyError2

Data Data Types

任何帮助将不胜感激。 谢谢

1 个答案:

答案 0 :(得分:0)

调用select的工作方式类似于SQL中的select语句,这意味着在该语句之后X..setups是唯一保留的列。如果只想为输入$ selectVariable中选定的人包括设置,则应首先过滤setups数据框。其次,dplyr中的函数返回与输入对象属于同一类的对象。您正在将tibble传递到函数中,因此它将返回tibble。但是,您需要将其设为标量才能在valueBox中呈现。您可以通过将过滤后的数据传递给基本的sum函数并仅对X..setups列求和,从而使其成为标量。

setUpByName <- reactive ({
  setups_filtered <- setups %>%
    filter(Owners == input$selectVariable)
  sum(setups_filtered$X..setups)
})