Search code examples
rshinyflexdashboard

Move a renderUI object from flexdashboard to shiny app


First I have the code that creates a flexadashboard. When the user selects a button the relative pop-up message is enabled. As you can see everything happens inside a renderUI() object. The issue is that I do not know how to create the same result within a shiny app since I cannot use the renderUI() like this inside the server.r part.

flex

---
title: "Single Column (Fill)"
output: 
  flexdashboard::flex_dashboard:
    vertical_layout: fill
runtime: shiny
---

### Chart 1

```{r}
library(flexdashboard)
library(shiny)
library(shinyWidgets)
radioButtons("radio", label = h3("Radio buttons"),
               choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3), 
               selected = 1)

renderUI({


if (input$radio==1) {
  #paste("The following tickers already exist in the database:", bad_tickers, collapse = " ")
  sendSweetAlert(session, title = "Bad tickers", text = paste("The following tickers already exist in the database:"))
} else if (input$radio==2) {
  #paste("The following names already exist in the database:", bad_names, collapse = " ")
  sendSweetAlert(session, title = "Bad Names", text = paste("The following names already exist in the database:"))
} 
  else {
sendSweetAlert(session, title = "Nice!", text = "Your tickers, names and weights have been saved. You'll see them appear as available funds if you refresh your browser.")



}
})



```

shiny(not working)

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  tags$h2("Sweet Alert examples"),
  radioButtons("radio", label = h3("Radio buttons"),
               choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3), 
               selected = 1)
)

server <- function(input, output, session) {


    sendSweetAlert(
      session = session,
      title = "1",
      text = "All in order",
      type = "success"
    )



    sendSweetAlert(
      session = session,
      title = "2",
      text = "It's broken...",
      type = "error"
    )

    sendSweetAlert(
      session = session,
      title = "3",
      text = "Non exist...",
      type = "error"
    )



}

shinyApp(ui, server)

Solution

  • If you want to translate your flex dash board into shiny, just create an actual uiOutput object in your layout and render it in the server function. (renderUI is a shiny function as well.)

    library(shiny)
    library(shinyWidgets)
    
    ui <- fluidPage(
      tags$h2("Sweet Alert examples"),
      radioButtons("radio", label = h3("Radio buttons"),
                   choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3), 
                   selected = 1),
      uiOutput("result")
    )
    
    server <- function(input, output, session) {
      output$result <- renderUI({
        if (input$radio==1) {
          #paste("The following tickers already exist in the database:", bad_tickers, collapse = " ")
          sendSweetAlert(session, title = "Bad tickers", text = paste("The following tickers already exist in the database:"))
        } else if (input$radio==2) {
          #paste("The following names already exist in the database:", bad_names, collapse = " ")
          sendSweetAlert(session, title = "Bad Names", text = paste("The following names already exist in the database:"))
        } 
        else {
          sendSweetAlert(session, title = "Nice!", text = "Your tickers, names and weights have been saved. You'll see them appear as available funds if you refresh your browser.")
        }
      })
    }
    shinyApp(ui, server)