我有如下 Shiny 应用程序代码所示的数据。该数据是具有许多键的数据帧的过滤部分。
我填补 tsibble 空白的方式,在 Y 之间的 RSEXCL 列中有 N。我想这样做,如果值为“Y”的列向量(RSEXCL)的索引的最大值大于或等于向量的给定值的索引,则向量的值在那个位置将为“Y”,如果不是,则为“N”。
例如,如果我尝试在 Shiny 中创建一个反应性对象,如下所示,我会收到以下警告。
Warning: Problem with `mutate()` input `RSEXCL`.
ℹ no non-missing arguments to max; returning -Inf
ℹ Input `RSEXCL` is `ifelse(...)`.
这是给出错误/警告的示例闪亮代码。input$series 是一个 Key 列表,例如“My_Key”,用于过滤较大的 tsibble/data.frame,即 outlier.DT.file。编辑:该应用程序现在包含可能在较大数据集中看到的两种不同类型数据的示例。此外,ifelse()
现在调整的代码会发出警告,但它会按预期执行。
library(shiny)
library(tidyverse)
library(tsibble)
library(feasts)
library(fable)
library(lubridate)
outlier.DT.file <- structure(list(Key = c("My_Key1", "My_Key1", "My_Key1", "My_Key1",
"My_Key1", "My_Key1", "My_Key1", "My_Key1", "My_Key1", "My_Key1",
"My_Key1", "My_Key1", "My_Key1", "My_Key1", "My_Key1", "My_Key1",
"My_Key1", "My_Key1", "My_Key1", "My_Key1", "My_Key1", "My_Key1",
"My_Key1", "My_Key1", "My_Key1", "My_Key1", "My_Key1", "My_Key1",
"My_Key1", "My_Key1", "My_Key1", "My_Key1", "My_Key2", "My_Key2",
"My_Key2", "My_Key2", "My_Key2", "My_Key2", "My_Key2", "My_Key2",
"My_Key2", "My_Key2", "My_Key2", "My_Key2", "My_Key2", "My_Key2",
"My_Key2", "My_Key2", "My_Key2", "My_Key2", "My_Key2", "My_Key2",
"My_Key2", "My_Key2", "My_Key2", "My_Key2", "My_Key2", "My_Key2",
"My_Key2", "My_Key2", "My_Key2", "My_Key2", "My_Key2", "My_Key2",
"My_Key2", "My_Key2", "My_Key2", "My_Key2", "My_Key2"), RSFMTH = structure(c(17897,
17928, 17956, 17987, 18017, 18048, 18078, 18109, 18140, 18170,
18201, 18231, 18262, 18293, 18322, 18353, 18383, 18414, 18444,
18475, 18506, 18536, 18567, 18597, 18628, 18659, 18687, 18718,
18748, 18779, 18809, 18840, 17744, 17775, 17805, 17836, 17866,
17897, 17928, 17956, 17987, 18017, 18048, 18078, 18109, 18140,
18170, 18201, 18231, 18262, 18293, 18322, 18353, 18383, 18414,
18444, 18475, 18506, 18536, 18567, 18597, 18628, 18659, 18687,
18718, 18748, 18779, 18809, 18840), class = "Date"), RSFQTY = c(288,
108, 120, 84, 168, 48, 108, 144, 108, 108, 444, 300, 24, 108,
132, 120, 156, 48, 84, 96, 144, 120, 144, 108, 120, 72, 108,
108, 144, 156, 156, 168, 876, 0, 1668, 0, 2472, 13680, 4068,
84, 6492, 732, 0, 0, 4968, 6456, 648, 1776, 0, 0, 432, 120, 756,
156, 636, 12, 84, 36, 180, 456, 276, 108, 468, 180, 1044, 672,
492, 972, 1392), RSOQTY = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 350,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
220, 0, 220, 0, 0, 0, 0, 0, 0, 0, 0, 220, 0, 0, 0, 0), RSEXCL = c("N",
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "N", "Y", "N", "Y", "N", "Y", "Y", "Y", "Y",
"Y", "Y", "N", "N", "Y", "Y", "Y", "Y", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "N", "N")), row.names = c(NA, -69L), key = structure(list(
Key = c("My_Key1", "My_Key2"), .rows = structure(list(1:32,
33:69), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), index = structure("RSFMTH", ordered = TRUE), index2 = "RSFMTH", interval = structure(list(
year = 0, quarter = 0, month = 0, week = 0, day = 1, hour = 0,
minute = 0, second = 0, millisecond = 0, microsecond = 0,
nanosecond = 0, unit = 0), .regular = TRUE, class = c("interval",
"vctrs_rcrd", "vctrs_vctr")), class = c("tbl_ts", "tbl_df", "tbl",
"data.frame"))
ui <- fluidPage(
selectInput("series", "Select SKU:", choices = unique(outlier.DT.file$Key)),
tableOutput("table")
)
server <- function(input, output, session){
my.table <- reactive({
req(input$series)
outlier.DT.file %>% filter(Key == input$series) %>% mutate(RSEXCL = ifelse('Y' %in% RSEXCL & max(which(RSEXCL == 'Y')) >= which(RSEXCL == 'Y'|RSEXCL == 'N'), "Y", "N"))
})
output$table <- renderTable({my.table()})
}
shinyApp(ui = ui, server = server)
我也尝试过case_when()
如下使用,但仍然收到上面指示的警告,尽管代码确实按预期工作。
mutate(RSEXCL = case_when('Y' %in% RSEXCL & max(which(RSEXCL == 'Y')) >= which(RSEXCL == 'Y'|RSEXCL == 'N') ~ "Y",
!('Y' %in% RSEXCL) ~ "N",
'Y' %in% RSEXCL & max(which(RSEXCL == 'Y')) < which(RSEXCL == 'Y'|RSEXCL == 'N') ~ "N"))