【问题标题】:Group_by and summarize is causing issues with "Run App" and app deploymentGroup_by 和 summarise 导致“运行应用程序”和应用程序部署出现问题
【发布时间】:2021-06-23 09:48:26
【问题描述】:

我在下面创建了一个小代表来说明我正在使用 Shiny 处理的问题

library(shiny)
library(plotly)
library(tidyverse)
library(nycflights13)

flights_reprex <- flights %>%
    mutate_if(sapply(flights, is.character), as.factor) %>%
    mutate(time_hour = as_date(time_hour))

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("NYC Flights Reprex"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            checkboxGroupInput("origin_loc", label = "choose an origin location",
                               choices = unique(flights_reprex$origin),
                               selected = unique(flights_reprex$origin)),
            dateRangeInput("date_range", "select date range",
                           start = "2013-01-01", end = "2013-12-31",
                           min = "2013-01-01", max = "2013-12-31", format = "yyyy-mm-dd")
            
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotlyOutput("plot1"),
           plotlyOutput("plot2"),
           dataTableOutput("flight")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    data_reac <- reactive({
        flights_reprex %>%
            filter(., between(time_hour, input$date_range[1], input$date_range[2]), origin %in% input$origin_loc)
    })
    
    output$flight <- renderDataTable({
        data_reac()
    })
    
    output$plot1 <- renderPlotly({
        flights_reprex %>%
            filter(., between(time_hour, input$date_range[1], input$date_range[2]), origin %in% input$origin_loc) %>%
            group_by(origin, time_hour) %>%
            summarize(total_dep_delay = sum(dep_delay)) %>%
            ungroup() %>%
            plot_ly(x = ~time_hour, y= ~total_dep_delay) %>%
            add_lines(color = ~origin)
    })
    
    output$plot2 <- renderPlotly({
        flights_reprex %>%
            filter(., between(time_hour, input$date_range[1], input$date_range[2]), origin %in% input$origin_loc) %>%
            group_by(origin, carrier) %>%
            summarize(total_arr_delay = sum(arr_delay)) %>%
            ungroup() %>%
            distinct(.) %>%
            plot_ly(x = ~carrier, y = ~total_arr_delay, color = ~origin, type = "bar")
            
    })
    
}

# Run the application 
shinyApp(ui = ui, server = server)

runApp()

当我选择整个代码并按 CTRL + Enter 时,应用程序显示没有任何问题,但是当我单击“运行应用程序”按钮时,控制台中几乎无限循环显示此消息以及我的另一个变量名称数据集,但问题仍然相同。

`summarise()` has grouped output by 'origin'. You can override using the `.groups` argument.

当循环最终结束时,会出现一条关于 C 包不足以支持它的消息。现在我知道接触 C 包是一个很大的不,所以我基本上被卡住了。因此,我也无法分享我的应用。

我几乎可以确定是我的 group_by 和汇总函数导致了问题,但我很难确定到底出了什么问题。

谁能引导我朝着正确的方向前进?谢谢

【问题讨论】:

  • 既然你说它与&lt;CTRL&gt; + &lt;Enter&gt; 配合得很好,我猜当你运行应用程序时你的代码中缺少一些东西。假设您使用 RStudio,您是否尝试过清理您的工作区并使用 &lt;CTRL&gt; + &lt;Enter&gt; 重新运行该应用程序以确保它真的工作?
  • 这听起来像是发生了无限递归。也许您以某种方式从其内部调用了一个函数,或者您不小心在全局环境中保留了一些不应该存在的东西。我会尝试重新启动 R 看看是否有帮助
  • 我重新启动了 R 并将 group_by 函数移到了管道中的其他位置,但问题仍然存在,
  • 试试dplyr::summarize()
  • 这就是我正在使用的功能

标签: r shiny group-by tidyverse


【解决方案1】:

简短回答,将runApp() 放在代码底部。

当您单击运行应用程序时,它会获取文件,然后执行runApp()。这反过来又获取文件,该文件又执行runApp()。以此类推。

当您与&lt;CTRL&gt; + &lt;Enter&gt; 交互运行时,应用程序实际上会在到达shinyApp(ui = ui, server = server) 行时启动。此函数作为 Shiny app 对象返回。当交互运行时,它会打印,并且它的 print 方法实际上会启动应用程序,暂停代码执行。当您关闭应用程序时,脚本的其余部分会继续,但您已经关闭了视图,因此您看不到它。单击“运行应用程序”时,该文件的来源没有回显,因此不会打印 shinyApp 对象,并且仅启动 runApp 循环,因为它正在寻找您的文件以生成 shinyApp 对象,而不是调用运行应用程序。

当以交互方式运行时,您实际上是在 R 执行进入循环之前停止它。使用“运行应用程序”按钮时,应用程序对象的打印没有暂停,您会立即进入循环。

更改代码的末尾添加一条消息,您可以看到:

# Run the application 
shinyApp(ui = ui, server = server)

message("Time to run the app. Yep, yep, yep")

runApp()

使用&lt;CTRL&gt; + &lt;Enter&gt; 您应该启动应用程序,但该消息未显示在控制台中。当您关闭应用程序时,您会看到该消息。当您单击“运行应用程序”时,您会看到消息不断打印。

【讨论】:

  • 是的,就是这样!该应用程序运行,我可以在 shinyapps.io 上分享它,有一个情节没有呈现,但这是另一个问题!非常感谢!!!
猜你喜欢
  • 2014-11-25
  • 1970-01-01
  • 2019-03-14
  • 1970-01-01
  • 2011-09-07
  • 1970-01-01
  • 2011-04-24
  • 2020-03-05
  • 1970-01-01
相关资源
最近更新 更多