尽管参数一致,但Shiny和ggplot2的输出不一致

时间:2019-06-29 08:15:30

标签: r ggplot2 shiny

下面显示带有Shiny和ggplot2的条形图的示例代码可以很好地工作。这是一个更大的代码的可复制示例,其中renderPlot()函数完全相同。条形图的基础数据(即,simulatedDataForPlot())是一个数据帧(已通过在原始代码中使用browser()命令确认-在浏览器模式下,请参见下面控制台的输出),我想相信这两种情况也完全一样。
不幸的是,我收到一条错误消息,其中包含似乎在renderPlot()函数内生成的原始代码:

Called from: renderPlot(...)
Browse[1]> n
debug at C:\Users\Josef\Desktop\final project\final   deployment/app.R#1311: if (!(is.null(simulatedDataForPlot()))) {
return(ggplot(data = simulatedDataForPlot(), aes(x = activity, 
    y = meanDuration)) + geom_bar(width = 0.7, stat = "identity") + 
    theme(axis.text.x = element_text(angle = 60, hjust = 1, 
        vjust = 1, size = 12)) + ggtitle("simulated results") + 
    xlab("activities/queues") + ylab("minutes"))
}
Browse[2]> simulatedDataForPlot() %>% class()
New names:
* `` -> ...1
* `` -> ...2
* `` -> ...3
* `` -> ...4
* `` -> ...5
* ... and 1 more problem
[1] "data.frame"
Browse[2]> simulatedDataForPlot()
               activity meanDuration
1       verifying queue    8.4754057
2    verifying activity    5.4447647
3      dispensing queue    0.1390553
4   dispensing activity    5.9895090
5    final check. queue   12.8071083
6 final check. activity    5.6514668
Browse[2]> n
debug at C:\Users\Josef\Desktop\final project\final deployment/app.R#1312: return(ggplot(data = simulatedDataForPlot(), aes(x = activity, 
y = meanDuration)) + geom_bar(width = 0.7, stat = "identity") + 
theme(axis.text.x = element_text(angle = 60, hjust = 1, vjust = 1, 
    size = 12)) + ggtitle("simulated results") + xlab("activities/queues") + 
ylab("minutes"))
Browse[2]> n
Called from: renderPlot(...)
Browse[1]> n
debug at C:\Users\Josef\Desktop\final project\final deployment/app.R#1311: if (!(is.null(simulatedDataForPlot()))) {
return(ggplot(data = simulatedDataForPlot(), aes(x = activity, 
    y = meanDuration)) + geom_bar(width = 0.7, stat = "identity") + 
    theme(axis.text.x = element_text(angle = 60, hjust = 1, 
        vjust = 1, size = 12)) + ggtitle("simulated results") + 
    xlab("activities/queues") + ylab("minutes"))
}
Browse[2]> n
Warning: Error in .subset2(public_bind_env, "initialize"): is.numeric(c  (timetable, period, values)) is not TRUE
191: <Anonymous>

谁能理解为什么renderPlot()的行为方式如此不一致?我不确定如何在不共享全部原始代码的情况下进一步简化问题。我最好的评估是该错误是在renderPlot()函数内生成的。在这两种情况下,Shiny代码的用户界面部分中的相关行也完全相同。 如果鉴于所显示的数据有限,上述问题仍然无法解决,那么如何在原始代码中找到有问题的行有任何有用的建议吗?

请参见控制台(RStudio)的输出,该输出在原始代码的renderPlot块(该块内的第一行)中调用browser()函数:

library(dplyr)

categoriesForPlot <- c("verifying queue", "verifying activity", 
                       "dispensing queue", "dispensing activity", 
                       "final check. queue", "final check. activity")

dataForPlot <- data.frame(
  activity = factor(categoriesForPlot, levels = categoriesForPlot, 
                    ordered = T),
  meanDuration = c(8.4754057,5.4447647,0.1390553,5.9895090,12.8071083,5.6514668)
)

#Defining the user interface:
ui <- fluidPage(
  flowLayout(
    plotOutput(outputId = "simulatedDurations")
  )
)

#Processing of inputs and preparing them for output on
#the user interface:
server <- function(input, output) {

  simulatedDataForPlot <- reactive({
    dataForPlot
  })

  #for simulation tab:
  #the following bar-chart is to display the durations (and later standard deviations)
  #of the various activities and queues of the process:
  output$simulatedDurations <- renderPlot({
    if (!(is.null(simulatedDataForPlot()))){
      return(
        ggplot(data = simulatedDataForPlot(), 
               aes(x = activity, y = meanDuration)) +
          geom_bar(width = 0.7, stat = "identity") +
          theme(axis.text.x = element_text(angle = 60, hjust = 1, vjust = 1, size = 12)) +
          ggtitle("simulated results") +
          xlab("activities/queues") +
          ylab("minutes")
      )
    }
  })

}

#Required to create the Shiny app:
shinyApp(ui = ui, server = server)

值得一提的是,上面的代码逐行进行,条形图出现在浏览器中,然后立即再次变灰。

请参阅示例代码:

db.Model(&dm.User{}).Related(&dm.EventReview{}, "user_id")
res:=QueryByStructExample(dm.User{Email: "email@example.com})



 func QueryByStructExample(userStruct User) []User {
        var results []dm.User
        GormDB.Where(userStruct).Find(&results)
        return results
    }

请参阅错误消息以及上面的预期。

0 个答案:

没有答案