1

My question relates to "value4" which is a valuebox in the below code.

I have created a select input which allows the user to choose a name, based on that name I want the app to find the number of projects that are associated to the name picked (number of projects = "X..setup") and then display the total number of projects in a valuebox("value4").

The problem I am having is getting the sum of all projects. Please find my code below:

    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

Any help would be greatly appreciated. Thank you

sqlworrier
  • 53
  • 5
  • Welcome to StackOverflow! Please read the info about [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and [how to give a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). This will make it much easier for others to help you. – thothal Mar 07 '19 at 12:27
  • it's hard to tell without seeing your data objects, but `select(X..setups)` in `setUpByName` looks strange before the `group_by()` – RolandASc Mar 07 '19 at 13:23
  • @RolandASc I put the select(X..setups) after the group_by() and unfortunately it still didn't work/, – sqlworrier Mar 07 '19 at 13:41
  • @RolandASc I have also uploaded 2 pictures, one is DF headers and other is valuebox error, these pictures can be accessed through the link just under the code above "DF Columns" & "Error" – sqlworrier Mar 07 '19 at 13:43

1 Answers1

0

calling select works like a select statement in SQL, meaning that after that statement X..setups is the only column that remains. If you want to include only setups for the person selected in input$selectVariable you should first filter the setups data frame. Secondly, the functions in dplyr return objects that are of the same class as the input object. Your are passing a tibble into the function, so it is returning a tibble. However, you need it to be a scalar in order to be rendered in the valueBox. You can make it a scalar by passing the filtered data to the base sum function and only summing the X..setups column.

setUpByName <- reactive ({
  setups_filtered <- setups %>%
    filter(Owners == input$selectVariable)
  sum(setups_filtered$X..setups)
})
Wil
  • 3,076
  • 2
  • 12
  • 31
  • Thank you for your answer, I have added the above code, the tibble error is now gone(thanks for pointing that problem out), but now I have another error: Only defined on a dataframe with all numeric variables. Do you know how I could fix this?, This is my first shiny dashboard so thanks for the help – sqlworrier Mar 07 '19 at 18:46
  • can you post the full error message text or image? If the problem in your original question is solved you should accept this answer and ask a new question with the new error message if it is unrelated to your original question about the `valueBox`. – Wil Mar 07 '19 at 21:55
  • The "new" error is relating to the valuebox in question, I have attached the error in shiny and the error in the code above(in the form of links "ErrorCode2" & "ShinyError2" – sqlworrier Mar 08 '19 at 11:08
  • without your data it's hard to figure out where the error is coming from, but it sounds like one of the pieces of data you're summing is not numeric. I would run `str(setups)` outside of your Shiny session to see how the columns are formatted (or, if it is a `tbl`, just call `print()` on the data and the column types will be displayed). – Wil Mar 08 '19 at 13:09
  • I have uploaded a link above with sample data (link is called "data"), The setups column contains all numbers, and the owners contains names (no missing data in either column) – sqlworrier Mar 08 '19 at 13:38
  • can you update the code to include the changes you have made so far? The error is indicating that you are summing an entire data frame (not a column, as in my code). The same error is produced if you run `sum(iris)`, but if you sum a data frame that is all numeric (`sum(mtcars)`), no error is returned. Somewhere you are summing a full data frame that has non-numeric columns. Also note that just because there is a number in that cell in your spreadsheet does not mean R will read the column as numeric, which is why it is important to run `str` or `print`. – Wil Mar 08 '19 at 13:51
  • I have added the updated code and I haver also added a screenshot of the data types (there is a link called Data Types), all the help is greatly appreciated. – sqlworrier Mar 08 '19 at 17:18
  • see my update. I added code using the `mtcars` dataset showing that my example should yield a functioning value box. I believe the error is likely coming from somewhere else. Try commenting out `output$value4 <- renderValueBox(...)` and see if you still get the error. – Wil Mar 08 '19 at 19:50
  • I commented out output$value4 <- renderValueBox(...) and didn't get any error so the error is associated to "Value4". I'm wondering because Owners is a string column is that why I am getting the error? – sqlworrier Mar 10 '19 at 08:58
  • no I think the issue was in my use of `sum` after a pipe. I have made a small change to the reactive function in my answer and separated the filtering from the summing. Let me know if that works. I replicated it on the `iris` dataset because it has non-numeric columns, unlike the `mtcars` dataset I used to test last time. – Wil Mar 11 '19 at 12:41
  • Okay so it seems that you nearly have it, the value box is now showing at least, the only problem is it shows a value of "0" for everything. – sqlworrier Mar 11 '19 at 13:52
  • that means the `filter(Owners == input$selectVariable)` is not returning `TRUE` for any of the values selected for selectVariable. What lead you to run `as.character` on `setups$Owners`? did it not read from the CSV as a character column? – Wil Mar 11 '19 at 16:06
  • you are a legend IT WORKS!!, This problem has been eating away at me the last few weeks, I really appreciate your time – sqlworrier Mar 11 '19 at 17:27
  • no problem at all, I'm sorry it took a few tries! If the answer is acceptable please make sure to accept it: https://stackoverflow.com/help/someone-answers – Wil Mar 11 '19 at 17:38