1

I'm trying to make a Dashboard in R Shiny. But I keep getting an error and can't figure out how to solve this.

Error:

data must be a data frame, or other object coercible by fortify(), not a logical vector

Could anyone please help? Thanks!

library(shiny)
library(shinydashboard)
library(tidyverse)
library(lubridate)

KPI1_shiny <- structure(list(J = c("2017", "2017", "2017", "2017", "2017", "2017"), M = c("01", "02", "03", "04", "05", "06"), Ordermaand = structure(c(17167, 17198, 17226, 17257, 17287, 17318), class = "Date"), AantalProducten = c(706L, 644L, 727L, 724L, 787L, 1018L)), row.names = c(NA, 6L), class = "data.frame")

ui <- dashboardPage(
  dashboardHeader(title = "Dashboard by: Bjorn"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("KPI1", tabName = "KPI1", icon = icon("th")),
      menuItem("KPI2", tabName = "KPI2 | ", icon = icon("th")),
      menuItem("Dashboard", tabName = "KPI7", icon = icon("chart-bar")),
      menuItem("Widgets", tabName = "KPI9", icon = icon("th")))
    ),
  dashboardBody(
    tabItems(
# KPI 1 Aantal verkochte producten binnen een tijdsperiode----------------------------------------------------------------------------------------------------  
      tabItem(tabName = "KPI1",
        fluidRow(    
        box(title = "Begindatum",
          selectInput(
                inputId = "Beginjaar", label = "Beginjaar:",
                list("2017", "2018"
                )),
        selectInput(inputId = "Beginmaand", label = "Beginmaand",
                    list("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12")
                    )),
        box(title = "Einddatum",
        selectInput(
                inputId = "Eindjaar", label = "Eindjaar:",
                list("2017", "2018"
                )),
        selectInput(inputId = "Eindmaand", label = "Eindmaand",
                    list("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12")
                    )),
        box(plotOutput("iPlot1"))
      )
    )
  )
))

server <- function(input, output) {

  v <- reactiveValues(value = NULL)

  observe({
    v$Begin <- make_date(input$Beginjaar, input$Beginmaand)
    v$Eind <- make_date(input$Eindjaar, input$Eindmaand)
  })

  output$iPlot1 <- renderPlot({KPI1_shiny %>% filter(between(Ordermaand, v$Begin, v$Eind)  %>%
                                                        ggplot(aes(x = Ordermaand, y = AantalProducten, group = 1))
                                                )})
}


shinyApp(ui, server)
bretauv
  • 7,756
  • 2
  • 20
  • 57
  • Hi, it's good that you provided the code to reproduce your app, but it would be even better to provide the code to generate your dataframe. Use ```dput``` on your data, or use an integrated dataset (```mtcars``` for example) – bretauv Jan 09 '20 at 13:06
  • also did you check [here](https://stackoverflow.com/questions/54050320/error-data-must-be-a-data-frame-or-other-object-coercible-by-fortify-no) for example? – bretauv Jan 09 '20 at 13:09
  • ```dput``` is quite easy to use, here's an example of how to do it (just replace ```mtcars``` by the name of your dataframe): ```dput(head(mtcars))```. Then, you just have to copy the output of this code and paste it in your post. Check [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to have more info – bretauv Jan 09 '20 at 13:43
  • Hello @bretauv, thanks for replying! Yes I've checked there, but still I couldn't figure out how to solve the issue. I couldn't figure out how to use dput, so I've exported the data to a .rdata file, you can download it here: [link](https://wetransfer.com/downloads/b9d038dc5417b03a489c4ed46b684abe20200109133906/eb4d7d)`link`. Oh one second I'm going to try to use dput – Bjorn Verdonk Jan 09 '20 at 13:45
  • 1
    This is the code generated by dput: structure(list(J = c("2017", "2017", "2017", "2017", "2017", "2017"), M = c("01", "02", "03", "04", "05", "06"), Ordermaand = structure(c(17167, 17198, 17226, 17257, 17287, 17318), class = "Date"), AantalProducten = c(706L, 644L, 727L, 724L, 787L, 1018L)), row.names = c(NA, 6L), class = "data.frame") – Bjorn Verdonk Jan 09 '20 at 13:49
  • okay I edited your post to make it more reproducible – bretauv Jan 09 '20 at 13:56

1 Answers1

0

You first have to make KPI1_shiny a reactive dataframe according to the inputs of date, and then you can use this dataframe in your plot.

Here's an example with plotting points:

library(shiny)
library(shinydashboard)
library(tidyverse)
library(lubridate)

KPI1_shiny <- structure(list(J = c("2017", "2017", "2017", "2017", "2017", "2017"), M = c("01", "02", "03", "04", "05", "06"), Ordermaand = structure(c(17167, 17198, 17226, 17257, 17287, 17318), class = "Date"), AantalProducten = c(706L, 644L, 727L, 724L, 787L, 1018L)), row.names = c(NA, 6L), class = "data.frame")

ui <- dashboardPage(
  dashboardHeader(title = "Dashboard by: Bjorn"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("KPI1", tabName = "KPI1", icon = icon("th")),
      menuItem("KPI2", tabName = "KPI2 | ", icon = icon("th")),
      menuItem("Dashboard", tabName = "KPI7", icon = icon("chart-bar")),
      menuItem("Widgets", tabName = "KPI9", icon = icon("th")))
  ),
  dashboardBody(
    tabItems(
      # KPI 1 Aantal verkochte producten binnen een tijdsperiode----------------------------------------------------------------------------------------------------  
      tabItem(tabName = "KPI1",
              fluidRow(    
                box(title = "Begindatum",
                    selectInput(
                      inputId = "Beginjaar", label = "Beginjaar:",
                      list("2017", "2018"
                      )),
                    selectInput(inputId = "Beginmaand", label = "Beginmaand",
                                list("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12")
                    )),
                box(title = "Einddatum",
                    selectInput(
                      inputId = "Eindjaar", label = "Eindjaar:",
                      list("2017", "2018"
                      )),
                    selectInput(inputId = "Eindmaand", label = "Eindmaand",
                                list("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12")
                    )),
                box(plotOutput("iPlot1"))
              )
      )
    )
  ))

server <- function(input, output) {

  v <- reactiveValues(value = NULL)

  observe({
    v$Begin <- make_date(input$Beginjaar, input$Beginmaand)
    v$Eind <- make_date(input$Eindjaar, input$Eindmaand)
  })

  data <- reactive({
    KPI1_shiny %>% 
      filter(between(Ordermaand, v$Begin, v$Eind))
  })

  output$iPlot1 <- renderPlot({
      ggplot(data = data(), aes(x = Ordermaand, y = AantalProducten, group = 1)) +
      geom_point()
  })
}


shinyApp(ui, server)
bretauv
  • 7,756
  • 2
  • 20
  • 57