Hi there,
I have a sample data frame with 5 variables: Network, BG, RD, Year, and Rate. I want to create a column for each possible combinations of Network, BG, RD, and Year. The logic behind this is that all this four variables with different combinations determine the incremental value for the Rate.
I've been struggling to use mutate and case_when and didn't figure out how to include all the possible combinations as input variables. For example, in the case below, I have 4 elements for Network, 2 elements for BG, 2 elements for RD, and 4 elements for Year. In total, that would be 4 x 2 x 2 x 4=64 combinations of new incremental values. However, I only want the user interface contains 4 numericinputs based on the filter I selected above. The case_when in my reprex example is not perfect, it is just giving you an idea on how the incremental value is applied based on the filters.
I wonder if there is an easy way to do this without using loop. Or any suggestions would be greatly appreciated.
library(shiny)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(data.table)
#>
#> Attaching package: 'data.table'
#> The following objects are masked from 'package:dplyr':
#>
#> between, first, last
library(tidyr)
df1<-data.frame("Network"=c("50K","50K","50K","50K", "45K","45K","45K","45K", "40K","40K","40K","40K","30K","30K","30K","30K"),
"BG"=c("B","B", "G","G","B","B", "G","G","B","B", "G","G","B","B", "G","G"),
"RD"=c("R","D","R","D","R","D","R","D","R","D","R","D","R","D","R","D"))
df2<- data.frame("Year"=c(2020,2021,2022,2023),
"Rate"=c(0.1,0.2,0.3,0.4))
df3<-merge(df1,df2)
df4<-function(input, var1, var2, var3, var4){
var1<-enexpr(var1)
var2<-enexpr(var2)
var3<-enexpr(var3)
var4<-enexpr(var4)
df3<-df3 %>% filter(Network %in% input$Network,BG %in% input$BG, RD %in% input$RD)
df3<-df3 %>% rowwise() %>%
mutate(delta= case_when(Network =="50K" & BG =="B" & RD == "R" & Year==2021 ~ as.numeric(!!var1),
Network =="45K" & BG =="B" & RD == "R" & Year==2022 ~ as.numeric(!!var2),
Network =="40K" & BG =="B" & RD == "R" & Year==2023 ~ as.numeric(!!var3),
Network =="30K" & BG =="B" & RD == "R" & Year==2024 ~ as.numeric(!!var4),
TRUE~0)) %>%
mutate(new_rate=Rate+delta) %>%
select(Network, BG, RD, Year, new_rate) %>% as.data.frame()
df3<-df3 %>% spread(key=Year, value=new_rate, fill = FALSE)
}
ui <- fluidPage(
titlePanel("Input"),
sidebarLayout(
sidebarPanel(
selectInput("Network","Choose a network:",
choices = c(unique(toupper(df3$Network))),
selected = '50K'),
selectInput("BG","Choose B or G:",
choices = c(unique(df3$BG)),
selected = "B"),
selectInput("RD","Choose R or D:",
choices = c(unique(df3$RD)),
selected = "R"),
br(),
numericInput("p1","Period 1:", 0, min = NA, max = NA),
numericInput("p2","Period 2", 0, min = NA, max = NA),
numericInput("p3","Period 3", 0, min = NA, max = NA),
numericInput("p4","Period 4", 0, min = NA, max = NA)),
mainPanel(
DT::dataTableOutput("Rate")
)
)
)
# Define server logic for random distribution app ----
server <- function(input, output) {
data<-reactive({
df4(input, input$p1, input$p2, input$p3, input$p4)
})
output$Rate <- DT::renderDataTable({
df3<- data()
})
}
shinyApp(ui=ui, server = server)
Shiny applications not supported in static R Markdown documents
Created on 2020-10-12 by the reprex package (v0.3.0)