I am attempting to develop a Shiny page where folks can view the 22 metrics used to calculate an index via sliderInputs, see the already calculated result of the index, adjust the sliders to recalculate the index, and view the newly recalculated index value as text.
The values displayed on the sliderInputs represent the actual values of the metric (in deg C, %s, etc.), so to recalculate the index, I need to first scale them from 0-1 and then use the scaled values in the index equation.
The original dataset has 179 obs. and 31 variables. For reprex purposes, I am including some condensed sample data and simplified ui/server code chunks.
I don't fully understand the reactive/reactiveValues/eventReactive vs. observe/observeEvent elements, so if anyone has time to explain potential solutions, that would be greatly appreciated.
Sample Code:
cridf <- data.frame(WRIA = c(20,20,21),
name = c("A","B","C"),
CRI = c(0.08,0.15,0.23),
MAT.a = c(14,14.7,16),
MnJA.a = c(-32,-5,-26),
MnS.a = c(-8,-29,-12)
)
UI Code:
ui <- fixedPage(
sidebarPanel(
h4(strong("1. Select Watershed")),
selectInput('selectWRIA', label = h4("WRIA:"),
choices = unique(as.integer(cri$wria))),
uiOutput("selectHUC"), #select HUC input, see code in server
h4(strong("2. Click on button to view metrics")),
actionButton("updateSlidersButton", #inputID
"Display Metrics"), #label
br(),
h4(strong("3. Adjust sliders and click button to view change
in Climate Resilience Score.")),
actionButton("updateCRIButton", #inputID
"Recalculate CRI"), #label
h4(strong(HTML(paste0("<u>", "Climate Resilience Score:", "</u>")))),
uiOutput("CRIactual"),
uiOutput("CRIadjusted"),
width = 3 #width of sidebar
),
mainPanel(
uiOutput("MATslider"),
uiOutput("MnJAslider"),
uiOutput("MnSslider"),
)
)
Server Code:
server <- function(input, output, session) {
#Sub-Watershed Drop Down
output$selectHUC <- renderUI({
selectInput("selectHUC", label = h4("Sub-Watershed:"),
choices = as.character(cri[cri$wria==input$selectWRIA,"name"]))
})
# New DF of selected HUC --> used for sliders, cri scores (actual and adjusted)
selectedHUC <- eventReactive(input$updateSlidersButton, {
focalHUC <- if (is.null(input$selectHUC)){cri[1]} else{paste0(input$selectHUC)}
cri_huc <- cri %>% filter(name==focalHUC)
})
# Actual CRI Score for Selected HUC
output$CRIactual <- renderText({
cri_sliderHUC <- selectedHUC()
paste0(h4(strong("Actual Score: "," ", round(cri_sliderHUC$CRI, digits = 2))))
})
# Sliders for Selected HUC
output$MATslider <- renderUI({
cri_sliderHUC <- selectedHUC()
sliderInput("MAT", "Mean August Stream Temp (C):",
min = 7.5, max = 20,
value = cri_sliderHUC$MAT.a)
})
output$MnJAslider <- renderUI({
cri_sliderHUC <- selectedHUC()
sliderInput("MnJA", "Mean June-August Flow (% Change):",
min = -84, max = -9,
value = cri_sliderHUC$MnJA.a)
})
output$MnSslider <- renderUI({
cri_sliderHUC <- selectedHUC()
sliderInput("MnS", "Mean September Flow (% Change):",
min = -71, max = -18,
value = cri_sliderHUC$MnS.a)
})
# This is where I am running into issues...
scaled_values <- eventReactive(input$updateCRIButton, {
MAT.i <- exp(4.5*(input$MATslider-7.5)/(20-7.5))*-1
MAT.s <- (scaled_values$MAT.i-(-90))/((-1) - (-90))
#})
# Adjusted CRI Score for Selected HUC --> == MAT intermediate or scaled value to test
output$CRIadjusted <- eventReactive(input$updateCRIButton, {
renderText({
paste0(h4(strong("Adjusted Score: "," ", round(scaled_values()$MAT.i, digits = 2)
)))
})
})