Continued: With R Shiny how can I select interactively and dynamically a column in a data frame based on a name returned by a reactive statement?

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)

Hi Andre.
You seem to have tried to format your text as code with triple single quotes, when it's triple backticks that would do it.

```
text as code
```

'''
still text

Hi Nirgrahamuk,
Thank you. I reformatted the code listing. I hope somebody can help!
Andre

If z_exposD is a reactive then to refer to its contents parenthesis are required. z_exposD()

Hello Nirgrahamuk,
Thank you for the very prompt reply. I think your comment put me on the track although I am not quite sure why my edited code does work. It is:

  z_months <- reactive({
    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()
  })

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.