SHINY&R-错误:“ data”必须是数据框或其他可通过“ fortify()”强制执行的对象

时间:2018-07-18 17:20:42

标签: r shiny

我正在使用2个输入进行闪亮的vizualization。

数据集:

> 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:

> 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()强制,而不是带有class的S3对象   reactExpr / reactive [没有可用的堆栈跟踪]

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

答案

正确执行整个脚本

错误分为三部分:

  

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

     

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

     

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

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)

0 个答案:

没有答案