I have data as shown in the Shiny app code below. This data is a filtered part of a dataframe which has many Keys.
The way I have filled in the gaps in the tsibble, there are N in the RSEXCL column in between Y's. I would like to make it so that if the maximum of the index of a column vector (RSEXCL) where the value is "Y", is greater than or equal to the index for a given value of the vector, the vector's value at that position will be "Y", and if not, then "N".
For example, if I try to create a reactive object in Shiny such as below, I get the following warning.
Warning: Problem with `mutate()` input `RSEXCL`.
ℹ no non-missing arguments to max; returning -Inf
ℹ Input `RSEXCL` is `ifelse(...)`.
Here is example Shiny code giving the error/warning. The input$series is a list of Keys such as "My_Key" which is used to filter the larger tsibble/data.frame which is outlier.DT.file. EDIT: The app now includes an example of two different kinds of data that might be seen in the larger data set. Also, the code adjusted for ifelse()
now gives a warning, but it executes as expected.
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)
I've also tried using case_when()
as below, but still get the warning indicated above, although the code does work as intended.
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"))