将renderUI对象从Flexdashboard移至闪亮的应用程序

时间:2020-01-16 19:41:24

标签: r shiny flexdashboard

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

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



}
})



```

闪闪发亮(不起作用)

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)

2 个答案:

答案 0 :(得分:1)

将所有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"
      )
    )
  })

答案 1 :(得分:1)

如果要将柔性仪表板转换为发光的面板,只需在布局中创建一个实际的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)