【发布时间】: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 和汇总函数导致了问题,但我很难确定到底出了什么问题。
谁能引导我朝着正确的方向前进?谢谢
【问题讨论】:
-
既然你说它与
<CTRL> + <Enter>配合得很好,我猜当你运行应用程序时你的代码中缺少一些东西。假设您使用 RStudio,您是否尝试过清理您的工作区并使用<CTRL> + <Enter>重新运行该应用程序以确保它真的工作? -
这听起来像是发生了无限递归。也许您以某种方式从其内部调用了一个函数,或者您不小心在全局环境中保留了一些不应该存在的东西。我会尝试重新启动 R 看看是否有帮助
-
我重新启动了 R 并将
group_by函数移到了管道中的其他位置,但问题仍然存在, -
试试
dplyr::summarize() -
这就是我正在使用的功能
标签: r shiny group-by tidyverse