Search code examples
htmlrshinyrenderaction

How to render HTML (written in R code) after pressing action button in R Shiny?


There is a block of text I wrote in R that I want to render when I press an action button in Shiny. It would look like this:

enter image description here

I think I could probably rewrite all the code in one long convoluted paste(), but it would take a while since it has quotes and a hyperlink. Is there an easier way to do this?

My (failed) attempted is below. The code I'm trying to render is commented out because the app wont run otherwise.

ui <- fluidPage(
  
  h4(strong("Project Description")),
  p(style="text-align: justify; font-size = 25px",
    "Shiny-Box is a Shiny application made with 
          the purpose of", 
    em("demonstrating various use of shiny features"), 
    "that are frequently asked by users but rarely 
          discussed in a beginner or formal class environment. 
          With an example app and its code, Shiny-Box hopefully 
          will ease your understanding on the mechanism 
          of various Shiny features. Go to",
    a(href = "https://github.com/NabiilahArdini/Shiny-Box",
      "Shiny-Box GitHub Page"),
    "to find more details on the source code."),
  
  tags$blockquote("Shiny-Box is still under continuous development. 
           Please look forward to future updates!"),
  hr(),
  
  actionButton("go", "Go"),
  numericInput("n", "n", 50),
  uiOutput("text")
)

server <- function(input, output) {
  
  randomVals <- eventReactive(input$go, {
    runif(input$n)
  })
  
  output$text <- renderUI({
    temp <- randomVals()
    
    h4(strong("Project Description"))
    # p(style="text-align: justify; font-size = 25px",
    #   "Shiny-Box is a Shiny application made with 
    #       the purpose of", 
    #   em("demonstrating various use of shiny features"), 
    #   "that are frequently asked by users but rarely 
    #       discussed in a beginner or formal class environment. 
    #       With an example app and its code, Shiny-Box hopefully 
    #       will ease your understanding on the mechanism 
    #       of various Shiny features. Go to",
    #   a(href = "https://github.com/NabiilahArdini/Shiny-Box",
    #     "Shiny-Box GitHub Page"),
    #   "to find more details on the source code."),
    # 
    # tags$blockquote("Shiny-Box is still under continuous development. 
    #        Please look forward to future updates!"),
    # hr()
    
  })
}

shinyApp(ui, server)

Solution

  • You can do:

    ui <- fluidPage(
      actionButton("go", "Go"),
      uiOutput("text")
    )
    
    server <- function(input, output) {
      
      output$text <- renderUI({
        tagList(
          h4(strong("Project Description")),
          p(style="text-align: justify; font-size = 25px",
            "Shiny-Box is a Shiny application made with 
              the purpose of", 
            em("demonstrating various use of shiny features"), 
            "that are frequently asked by users but rarely 
              discussed in a beginner or formal class environment. 
              With an example app and its code, Shiny-Box hopefully 
              will ease your understanding on the mechanism 
              of various Shiny features. Go to",
            a(href = "https://github.com/NabiilahArdini/Shiny-Box",
              "Shiny-Box GitHub Page"),
            "to find more details on the source code."),
          tags$blockquote("Shiny-Box is still under continuous development. 
               Please look forward to future updates!"),
          hr()
        )
      }) |> bindEvent(input$go)
    }
    

    or you can do:

    ui <- fluidPage(
      actionButton("go", "Go"),
      conditionalPanel(
        condition = "input.go > 0",
        h4(strong("Project Description")),
        p(style="text-align: justify; font-size = 25px",
          "Shiny-Box is a Shiny application made with 
              the purpose of", 
          em("demonstrating various use of shiny features"), 
          "that are frequently asked by users but rarely 
              discussed in a beginner or formal class environment. 
              With an example app and its code, Shiny-Box hopefully 
              will ease your understanding on the mechanism 
              of various Shiny features. Go to",
          a(href = "https://github.com/NabiilahArdini/Shiny-Box",
            "Shiny-Box GitHub Page"),
          "to find more details on the source code."),
        tags$blockquote("Shiny-Box is still under continuous development. 
               Please look forward to future updates!"),
        hr()
      )
    )
    
    server <- function(input, output) {}