Can I pass data from renderUI to child HTML Elements?


#1

Hi All,

I need to put some conditional HTML into my Shiny App, so depending on what dropdown a user chooses, it will render the appropriate data. The idea is that will render a Tabset with a bunch of tabs that each have some of the chosen data.

This seems straightforward enough (code below) but I want to know if, I define a some nested HTML Elements in a renderUI method, can I pass the the data down to those elements?

I am probably phrasing this really badly, same problem as in react.js if you have components inside components and want to pass parent data to child elements using props. The code and comments should make this clear.

Apologies if this is a newbie question, still very new to Shiny and probably not using the right keywords to find an answer. :grinning:

library(shiny)
library(dplyr)


assetList <- read.csv("./data/assets.csv")


ui <- fluidPage(
  

  
  fluidRow(
    
    column(3,
          ""  
    ),
    
    column(9,
           selectInput("assetChoice", 
                       "", 
                       width='700px',
                       choices = c("Choose a data asset", as.list(sort(assetList$name))), 
                       selected = NULL)
          
    ),
    hr()
  ),
  
  fluidRow(
    column(2,
           ""),
    column(8,
           uiOutput("allAssets"))
  )
 
)

server <- function(input, output) {
  
  output$allAssets = renderUI({
    if (input$assetChoice != "Choose a data asset") {

      div(
        textOutput("assetDetailDescription"),
        br(),
        tabsetPanel(type = "tabs",
                    tabPanel("Overview", tableOutput("assetDetailOverview")),
                    tabPanel("Users and roles", verbatimTextOutput("summary")),
                    tabPanel("Supporting documentation", tableOutput("tableAgain"))
        )
        
      )
    } else {
      includeHTML("overPartial.html")
    }
  })
  
  
  ## Child of all output$allAssets - can I get hold of the data of the parent here
  output$assetDetailDescription = renderText({
    if (input$assetChoice != "Choose a data asset") {
      choice <- assetList %>% 
        filter(name == input$assetChoice)
      return(paste0("", choice$description))
      
    } 
  })
  
  
  ## Child of all output$allAssets - can I get hold of the data of the parent here
  output$assetDetailOverview = renderTable({
    if (input$assetChoice != "Choose a data asset") {
      choice <- assetList %>% 
        filter(name == input$assetChoice)
      
      df <- as.data.frame(t(choice))
      df$Description <- colnames(choice)
      colnames(df) = c("Data", "Description")
      df$Description <- tools::toTitleCase(base::gsub("_", " ", df$Description))
      df$Data <- base::gsub("0", "No", df$Data)
      df = df[2:1]
      
      
      return(df[2:21,])
    }
    
  })

}


shinyApp(ui = ui, server = server)