将 renderUI 对象从 flexdashboard 移动到 shiny app

Move a renderUI object from flexdashboard to shiny app

首先我有创建 flexadashboard 的代码。当用户选择一个按钮时,将启用相关的弹出消息。如您所见,一切都发生在 renderUI() 对象中。问题是我不知道如何在闪亮的应用程序中创建相同的结果,因为我不能在 server.r 部分中使用这样的 renderUI()

弹性

---
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.")



}
})



```

闪亮(不工作)

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)

将所有 sendSweetAlert 调用(以及确定发送哪个调用的代码)放入 observeEvent(input$radio, { ... })

  observeEvent(input$radio, {
    switch(input$radio,
     `1`= sendSweetAlert(
        session = session,
        title = "1",
        text = "All in order",
        type = "success"
      ),

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

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

如果您想将 flex 仪表板转换为闪亮的,只需在您的布局中创建一个实际的 uiOutput 对象并在服务器函数中呈现它。 (renderUI 也是一个闪亮的函数。)

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)