【问题标题】:Error in : `data` must be a data frame, or other object coercible by `fortify()`错误:`data` 必须是数据框,或其他可被`fortify()`强制的对象
【发布时间】:2018-12-26 17:03:05
【问题描述】:

我正在使用 2 个输入进行闪亮的可视化。

数据集:

est_popai <- data.frame(concat = c("A_1","B_1","C_1","A_2","B_2","C_2","A_1","B_1","C_1","A_2","B_2","C_2","A_1","B_1","C_1","A_2","B_2","C_2","A_1","B_1","C_1","A_2","B_2","C_2"),
                  variables  = c("quantity","quantity","quantity","quantity","quantity","quantity","price","price","price","price","price","price","quality","quality","quality","quality","quality","quality","size","size","size","size","size","size"),
                   values = round(runif(24, 5.0, 7.5),2)
                 )  

用户界面:

ui <- fluidPage(
  headerPanel(
    h1("Combinacao de atributos")
  ),
  sidebarPanel(
    selectInput("xcol"," Variavel X", unique(est_popai$variable),
                selected = 'price'),
    selectInput("ycol"," Variavel y", unique(est_popai$variable),
                selected = 'size')
  ),
  mainPanel(
    plotOutput("plot1") 
  )
)

服务器:

server <- function(input, output) {
  
  selectData <- reactive  ({
    
    est_popai[est_popai$variable == input$xcol | est_popai$variable == input$ycol,] %>%
      unique() %>%
      spread(variable,value)  
  })
  
  
  output$plot1 <- renderPlot({
    ggplot(data = selectData, aes(x = input$xcol, y = input$ycol)) +
      geom_point()
  })
  
  
}

运行:

shinyApp(ui = ui, server = server)

当我运行整个代码时,我收到了以下错误消息:

警告:错误:data 必须是数据框或其他对象 由fortify() 强制,而不是具有类的 S3 对象 reactiveExpr/reactive [没有可用的堆栈跟踪]

我尝试添加 as.data.frame() 函数但没有成功。有人可以帮我解决这个错误,我已经搜索了一段时间。

【问题讨论】:

  • 尝试将ggplot函数中的data参数改为data=selectData()
  • 我遇到了其他错误:警告:eval_tidy 中的错误:找不到对象“变量”[没有可用的堆栈跟踪]@JohnPaul
  • 试试 aes_string 而不是 aes
  • 请在解决后关闭主题。
  • 我投票结束这个问题,因为提问者已经回答了他们自己的问题,但没有将其标记为已回答。

标签: r shiny


【解决方案1】:

从 OP 复制粘贴。

请参阅下面更正的脚本。错误分为 3 个部分:

1-我忘了在数据ggplot函数data = selectData()中添加()

2- 对象有不同的名称,我忘记了 s 中的字母 variablevalue 对象

3- aes()ggplot 函数中应该是aes_string()

ui <- fluidPage(
  headerPanel(
    h1("Combinacao de atributos")
  ),
  sidebarPanel(
    selectInput("xcol"," Variavel X", unique(est_popai$variables),
                selected = 'price'),
    selectInput("ycol"," Variavel y", unique(est_popai$variables),
                selected = 'size')
  ),
  mainPanel(
    plotOutput("plot1") 
  )
)

server <- function(input, output) {
  
  selectData <- reactive  ({
    
    est_popai[est_popai$variables == input$xcol | est_popai$variables == input$ycol,] %>%
      unique() %>%
      spread(variables,values)  
  })
  
  
  output$plot1 <- renderPlot({
    ggplot(data = selectData(), aes_string(x = input$xcol, y = input$ycol)) +
      geom_point()
  })
  
  
}

shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    • 2020-07-24
    • 2020-04-27
    • 1970-01-01
    • 2021-07-01
    • 2020-10-15
    相关资源
    最近更新 更多