R Shiny access session namespace

shiny
rstudio

#1

Hello,

I would like to return different dataframe based on the namespace. In order to implement it, I am doing if statement with session&ns value as boolean. However, code does not perform as needed.
Could you please advise how to do this correctly?

ui.R

source("HFS_module.R")
library(shiny)
library(rhandsontable)



shinyUI(navbarPage("Transaction", fluid = TRUE,
                   tabPanel("Historical Financials",
                            titlePanel("Input Adjusted Historical Financial Statements"),
                            tabsetPanel(
                              tabPanel("Historical Income Statement", rHandsontableOutputHFSUI("Income")), 
                              tabPanel("Historical Balance Sheet", rHandsontableOutputHFSUI("Balance Sheet")), 
                              tabPanel("Historical Cash Flow Statement", rHandsontableOutputHFSUI("Cash Flow"))
                            )                
                      )
                   )
)

server.R

source("HFS_module.R")

library(rhandsontable)
library(shiny)

shinyServer(function(input, output) {
  
  callModule(rHandsontableOutputHFS,"Income")
  callModule(rHandsontableOutputHFS,"Balance Sheet")
  callModule(rHandsontableOutputHFS,"Cash Flow")
  
})

HFS_module.R

library(shiny)
library(rhandsontable)

# Define default statements' tables

HIS <-  data.frame(matrix(0.0, ncol = 3, nrow = 3))
col_names <- c("2015", "2016", "2017")
row_names <- c("Revenue", "COGS", "Gross Margin")
colnames(HIS) <- col_names
rownames(HIS) <- row_names

HBS <-  data.frame(matrix(0.0, ncol = 3, nrow = 3))
col_names <- c("2015", "2016", "2017")
row_names <- c("Assets", "Liabilities", "Equity")
colnames(HBS) <- col_names
rownames(HBS) <- row_names

HCFS <-  data.frame(matrix(0.0, ncol = 3, nrow = 3))
col_names <- c("2015", "2016", "2017")
row_names <- c("CF Operating", "CF Financing", "CF Investing")
colnames(HCFS) <- col_names
rownames(HCFS) <- row_names

# Historical financial statements module

rHandsontableOutputHFSUI <- function(id) {
  ns <- NS(id)
  tagList(
    tags$h1(ns("Statement")),
    rHandsontableOutput(ns("Historical_Statement"), width = 350)
  )
}

rHandsontableOutputHFS <- function(input, output, session) {
  
  values = reactiveValues()
  
  observe({input$Historical_Statement
    if (!is.null(input$Historical_Statement)) {
      values$df <- hot_to_r(input$Historical_Statement)
    } else {
      if (session$ns("Income"))  values$df <- HIS
      else if (session$ns("Balance Sheet")) values$df <- HBS
      else if (session$ns("Cash Flow")) values$df <- HCFS      
    }
    
  })
  
  output$Historical_Statement <- renderRHandsontable({
    rhandsontable(values$df)
  })
  
}