I assign a name to a Reactive Value via a Radio Button. I then try to select in the data frame the column with that name. But my code does not work. Perhaps I need an "observe()" statement, which I am not familiar with.
The code listed below will run fine if you leave the statement "test_problem <- FALSE".
If you change it to "test_problem <- TRUE" you will get the error:
"Error: cannot coerce class ‘c("reactiveExpr", "reactive", "function")’ to a data.frame".
Somehow I cannot find the solution to this. Help, please! Regards, Andre
#
# v 4.0 mb 052121 - 050421 - 042921
# Test4_VsA.R
#
# R Code to illustrate problem assigning data array based on label retrieved from Shiny Radio button
#
library(shiny)
library(plotly)
library(purrr)
z_curves_expos_delta <-
data.frame(
Tenor = c("SPOT", "BALM", "PRPT", "1_YR"),
Month = c(0, 0.5, 1, 12),
NG_NYMEX = c(10000, 5000, 1000, 3000),
LG_JKM = c(1000, 7000, 1000, 4000),
CR_WTI = c(8000, 4000, 2500, 1000)
)
ui <- fluidPage(titlePanel("Code to illustrate problem"),
sidebarLayout(sidebarPanel(
radioButtons(
"curve",
"Curve name:",
c(
"NG_NYMEX" = "NG_NYMEX",
"LG_JKM" = "LG_JKM",
"CR_WTI" = "CR_WTI"
),
selected = "NG_NYMEX"
),
),
mainPanel(tabsetPanel(
type = "tabs",
tabPanel("TABLE",
fluidRow(column(
1, tableOutput("table_expoD")
), ))
))))
server <- function(input, output, session) {
########## FLAG TO TEST PROBLEM #########
test_problem <- FALSE
#########################################
if (test_problem)
{
z_curve <- reactive({
input$curve
})
z_exposD <- reactive({
z_curves_expos_delta %>% select(z_curve())
})
}
else
{
z_curve <- "NG_NYMEX"
# z_curve <- "LG_JKM"
# z_curve <- "CR_WTI"
z_exposD <- z_curves_expos_delta[, z_curve]
}
z_months <- z_curves_expos_delta$Month
rv_expoD <- reactiveValues(x = z_months,
y = z_exposD)
data_expoD <- reactive({
d <-
data.frame(Deltas = rv_expoD$x,
Original = z_exposD,
Scenario = rv_expoD$y)
d
})
output$table_expoD <- renderTable({
data_expoD()
})
}
shinyApp(ui, server)