Reputation: 1
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 inflation input such as 0.05, 0.01, not integer such as 1,2 3
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(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
DF(0.05,0.05,0.05,0.01,0.01,0.01)
#> Error in DF(0.05, 0.05, 0.05, 0.01, 0.01, 0.01): could not find function "DF"
DF
#> Error in eval(expr, envir, enclos): object 'DF' not found
Created on 2020-08-12 by the reprex package (v0.3.0)
Upvotes: 0
Views: 436
Reputation: 21349
Try this
#Load packages
library(shiny)
library(data.table)
library(dplyr, warn.conflicts = FALSE)
library(DT)
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(width=2,
h3("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),
h3("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)
),
# Main panel for displaying outputs ----
mainPanel(
DTOutput("table"))
)
)
# Define server logic required to draw a histogram ----
server <- function(input, output) {
DF<-function(var1, var2, var3,var4, var5, var6){
df1<-df %>% rowwise %>%
transform(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))
return(df1)
}
data1<-reactive({
DF(as.numeric(input$b1),as.numeric(input$b2),as.numeric(input$b3),as.numeric(input$p1),as.numeric(input$p2),as.numeric(input$p3))
})
#plan awp table
output$table <- renderDT({
data1()
})
}
shinyApp(ui, server)
Upvotes: 0