【问题标题】:`data` must be a data frame, or other object coercible by `fortify()`, not a logical vector`data` 必须是一个数据框,或者其他可以被 `fortify()` 强制转换的对象,而不是一个逻辑向量
【发布时间】:2020-04-27 02:19:20
【问题描述】:

我正在尝试在 R Shiny 中制作仪表板。但我不断收到错误,无法弄清楚如何解决这个问题。

错误:

data 必须是一个数据框,或其他可被fortify() 强制转换的对象,而不是一个逻辑向量

有人可以帮忙吗?谢谢!

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)

【问题讨论】:

  • 您好,很高兴您提供了重现应用程序的代码,但提供生成数据帧的代码会更好。在您的数据上使用dput,或使用集成数据集(例如mtcars
  • 你也检查过here吗?
  • dput 很容易使用,这里有一个如何使用的示例(只需将mtcars 替换为您的数据框的名称):dput(head(mtcars))。然后,您只需复制此代码的输出并将其粘贴到您的帖子中。检查here了解更多信息
  • 您好@bretauv,感谢您的回复!是的,我已经在那里检查过,但我仍然无法弄清楚如何解决这个问题。我不知道如何使用 dput,所以我将数据导出为 .rdata 文件,您可以在这里下载:linklink。哦,一秒钟我要尝试使用 dput
  • 这是dput生成的代码:structure(list(J = c("2017", "2017", "2017", "2017", "2017", "2017"), M = c("01", "02", "03", "04", "05", "06"), Ordermaand =结构(c(17167, 17198, 17226, 17257, 17287, 17318), class= "日期"), AantalProducten = c(706L, 644L, 727L, 724L, 787L, 1018L)), row.names = c(NA, 6L), class= "data.frame")

标签: r dataframe vector shiny dashboard


【解决方案1】:

您首先必须根据日期的输入使KPI1_shiny 成为反应数据框,然后您可以在绘图中使用此数据框。

这是一个带有绘图点的示例:

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)

【讨论】:

    猜你喜欢
    • 2022-12-10
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 2020-08-16
    相关资源
    最近更新 更多