【问题标题】:Shiny Dashboard "Error in unique.default(x, nmax = nmax)" when using table() function使用 table() 函数时,闪亮的仪表板“unique.default(x, nmax = nmax) 中的错误”
【发布时间】:2021-04-16 10:09:05
【问题描述】:

我试图在闪亮的仪表板上绘制一个相当简单的图,但是我收到了这个错误:

 Warnung: Error in unique.default: unique() kann nur auf Vektoren angewendet werden
  51: unique.default
  49: factor
  48: table
  47: server [C:/.../Dashboard.R#38]
Error in unique.default(x, nmax = nmax) : 
  unique() kann nur auf Vektoren angewendet werden

我的代码如下所示(与 hello world 示例几乎没有任何不同):

## app.R ##
library(shiny)
library(shinydashboard)

# my libraries #
library(dplyr)

ui <- dashboardPage(
  dashboardHeader(title = "My Dashboard"),
  
  dashboardSidebar(),
  
  dashboardBody(fluidRow(
    box(plotOutput("plot1", height = 400)),
    
    box(title = "Controls",
        sliderInput("slider", "Years:", 1970, 2017, 2000))
  ))
)

server <- function(input, output) {
  dat <- read.csv("filename.csv", sep = ",", header = TRUE, encoding = "UTF-8", fill = TRUE)
  
  dat <-
    reactive({
      dat %>%
        filter(year > input$slider) %>%
        select(year)
    })
  
  tab <- table(dat)
  
  output$plot1 <- renderPlot({
    plot(
      tab,
      main = "My Main Title",
      ylab = "Amount",
      xlab = "Year",
      type = "o"
    )
  })
}

shinyApp(ui, server)

错误必须是在我使用table() 函数时(因为它在原始代码中的#38 行),但错误消息太不具体,我无法找出问题所在...

有没有人建议如何解决这个问题?

【问题讨论】:

    标签: r dplyr shiny shinydashboard


    【解决方案1】:

    您的 dat 对象是响应式对象,因此请尝试调用 tab &lt;- table(dat())。此外,将响应式调用放在渲染函数中,如下所示:

      output$plot1 <- renderPlot({
        tab <- table(dat())
        plot(
          tab,
          main = "My Main Title",
          ylab = "Amount",
          xlab = "Year",
          type = "o"
        )
      })
    

    我也会避免重复使用变量名作为一般的编码实践。

    【讨论】:

    • 现在我收到以下错误:Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
    • 您必须在渲染函数中放置反应式评估
    • 哦,有道理。那解决了它。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 2019-02-12
    相关资源
    最近更新 更多