[英]How to eliminate "Warning: Error in seq.default: 'from' must be a finite number" when running App?
I'm running a version of the below reduced code in an App that allows the user to stratify the elements in a large data frame as of a single point-in-time.我正在一个应用程序中运行以下简化代码的一个版本,它允许用户在单个时间点对大型数据框中的元素进行分层。 The user can choose which measure to use for time:
Period_1<\/code> or
Period_2<\/code> , and which time period to select within each of those 2 time categories.
用户可以选择用于时间的度量:
Period_1<\/code>或
Period_2<\/code> ,以及在这 2 个时间类别中选择哪个时间段。
The full App offers the user several more stratification variables, and in the
renderTable<\/code> section below there remain vestiges of this broader functionality.
完整的 App 为用户提供了更多的分层变量,并且在下面的
renderTable<\/code>部分中仍然保留了这种更广泛功能的痕迹。
The example produces the intended results, but it generates in the R console: " Warning: Error in seq.default: 'from' must be a finite number<\/em> ".该示例产生了预期的结果,但它在 R 控制台中生成:“警告:seq.default 中的错误:'from' 必须是有限数<\/em>”。 (I get this same warning in both the below example and the full App this example derives from).
(我在下面的示例和此示例派生的完整应用程序中都收到了相同的警告)。
Other warnings (" Warning in min..., na.rm = TRUE) : no non-missing arguments to min; returning Inf<\/em> ") were popping up before I added the
custom_min<\/code> and
custom_max<\/code> functions below.
在我添加下面的
custom_min<\/code>和
custom_max<\/code>函数之前,会弹出其他警告(“ Mining 中的警告...,na.rm = TRUE):min 没有非缺失参数;返回 Inf<\/em> ”)。
I don't want to suppress the warning, I'd rather have the code work correctly.我不想抑制警告,我宁愿让代码正常工作。 Any clues how to address?
任何线索如何解决?
library(shiny)
library(tidyverse)
library(shinyWidgets)
custom_min <- function(x) {if (length(x)>0) min(x, na.rm=TRUE) else Inf}
custom_max <- function(x) {if (length(x)>0) max(x, na.rm=TRUE) else Inf}
ui <-
fluidPage(
uiOutput("period"),
uiOutput("strat_time"),
tableOutput("stratData"))
server <- function(input, output, session) {
stratData <- reactive({
data.frame(
ID = c(1,1,2,2,2,2,3,3,3),
Period_1 = c("2020-03", "2020-04", "2020-01", "2020-02", "2020-03", "2020-04", "2020-02", "2020-03", "2020-04"),
Period_2 = c(1, 2, 1, 2, 3, 4, 1, 2, 3),
Values = c(-5, 25, 35, 45, 55, 87, 10, 20, 30)
)
})
output$period <- renderUI({
radioButtons(
inputId = 'period',
label = NULL,
choiceNames = c('By Period_1','By Period_2'),
choiceValues = c('Period_1','Period_2'),
selected = 'Period_1',
inline = TRUE
)
})
output$strat_time <- renderUI({
req(input$period)
chc <- unique(na.omit(stratData()[[input$period]]))
selectInput(inputId = "strat_time",
label = "Specify stratification point-in-time:",
choices = chc,
selected = chc[1])
})
output$stratData <- renderTable({
qs <- ifelse(is.character(stratData()[[input$period]]), "'", "")
filter_exp1 <- parse(text=paste0(input$period, "==", qs,input$strat_time, qs))
stratData_1 <- reactive({stratData() %>% filter(eval(filter_exp1))})
breaks <- seq(custom_min(stratData_1()[[4]]),
custom_max(stratData_1()[[4]]),
by=10)
if(max(breaks) < max(stratData_1()[[4]], na.rm=TRUE)){breaks <- c(breaks, max(breaks) + 10)}
tmp <- stratData() %>%
filter(eval(filter_exp1)) %>%
mutate(sumvar = cut(!!sym("Values"), breaks=breaks, include.lowest=TRUE, right = TRUE, dig.lab = 5)) %>%
group_by(sumvar) %>%
summarise(Count = n(),Values = sum(!!sym("Values"))) %>%
complete(sumvar, fill = list(Count = 0,Values = 0)) %>%
ungroup %>%
mutate(Count_pct = Count/sum(Count)*100, Values_pct = Values/sum(Values)*100) %>%
dplyr::select(everything(), Count, Count_pct, Values, Values_pct)
tmp
})
}
shinyApp(ui, server)
This happen because your function custom_min(stratData_1()[[4]])<\/code> can return non-finite numbers, namely
Inf<\/code> , when
length(x) > 0<\/code> .
发生这种情况是因为您的函数
custom_min(stratData_1()[[4]])<\/code>可以在
length(x) > 0<\/code>时返回非有限数字,即
Inf<\/code> 。
You can change your code to something like :您可以将代码更改为:
min <- custom_min(stratData_1()[[4]])
max <- custom_max(stratData_1()[[4]])
breaks <- if(any(is.infinite(c(min,max)))) c(0, 10) else seq(min, max, by = 10)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.