case_when mutate integer conversion issue

Hi guys,

I wonder how to fix the integer and double vector issue in case_when. I need double vector as my numericinput, However, the "case_when" always throws me an error like "x must be an integer vector, not a double vector".

It only allows me to enter integer input when I change "TRUE ~ 0" to "TRUE ~0L"

I need something like this
provider year inflation GFR

1 CVS 2020 0.05 0.1
2 CVS 2021 0.05 0.1
3 CVS 2022 0.05 0.1
4 CVS 2023 0 0
5 CVS 2024 0 0
6 CVS 2025 0 0

I've been frustrated with the error for a few days. Can anyone help me on this?

Thanks!

#Load packages
library(shiny)
library(data.table)
library(dplyr, warn.conflicts = FALSE)
library(DT)
#> 
#> Attaching package: 'DT'
#> The following objects are masked from 'package:shiny':
#> 
#>     dataTableOutput, renderDataTable
library(tidyr)


df<-data.frame('provider'="CVS",  'year'=c(2020,2021,2022,2023,2024,2025))

# Define UI for app that draws a histogram ----
ui <- fluidPage(
  
  # App title ----
  titlePanel("Hello Shiny!"),
  
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
    
    # Sidebar panel for inputs ----
    sidebarPanel(
      tabsetPanel(
        tabPanel("Brand Inf",
                 numericInput("b1","Brand 1", 0, min = NA, max = NA),
                 numericInput("b2","Brand 2", 0, min = NA, max = NA),
                 numericInput("b3","Brand 3", 0, min = NA, max = NA)),
                
        tabPanel("GFR",
                 numericInput("p1","GFR 1", 0, min = NA, max = NA),
                 numericInput("p2","GFR 2", 0, min = NA, max = NA),
                 numericInput("p3","GFR 3", 0, min = NA, max = NA))),
      width = 2),
    
    # Main panel for displaying outputs ----
    mainPanel(
      DT::dataTableOutput("table"))
  ))


# Define server logic required to draw a histogram ----
server <- function(input, output) {
  
  DF<-function(input, var1, var2, var3,var4, var5, var6){
    var1<-enexpr(var1)
    var2<-enexpr(var2)
    var3<-enexpr(var3)
    var4<-enexpr(var4)
    var5<-enexpr(var5)
    var6<-enexpr(var6)
    
    df<-df %>% rowwise %>% 
      mutate(inflation= case_when(year== 2020 ~ !!var1, year == 2021 ~ !!var2, year == 2022 ~ !!var3, TRUE ~ 0),
             GFR=case_when(year== 2020 ~ !!var4, year == 2021 ~ !!var5, year == 2022 ~ !!var6, TRUE ~ 0))
  }
  
  data<-reactive({
    
    DF(input,input$b1,input$b2,input$b3,input$p1,input$p2,input$p3) 
    
  })
  
  #plan awp table
  output$table <- DT::renderDataTable({
    DATA <- data()
  })
  
}

shinyApp(ui, server)

Shiny applications not supported in static R Markdown documents

Created on 2020-08-12 by the reprex package (v0.3.0)

case_when simply doesnt want you mixing types.
you need to choose a consistent type to work in.
It seems that you found a special case where if the numeric inputs are integer, you can match them to the default 0 with specifying 0L, but this would break when you add a decimal point to the numeric input value.
The fix is to go the other way, and insist that any 'apparently' integer value from numetic input is actually a numeric/double.

 df<-df %>% rowwise %>%
      mutate(inflation= case_when(year== 2020 ~ as.numeric(!!var1),

etc...

Yes! I just figured it out one hour after I posted the question. Thank you so much for you help!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.