【问题标题】:Move a renderUI object from flexdashboard to shiny app将 renderUI 对象从 flexdashboard 移动到闪亮的应用程序
【发布时间】:2020-05-03 17:06:48
【问题描述】:

首先我有创建弹性仪表板的代码。当用户选择一个按钮时,将启用相关的弹出消息。如您所见,一切都发生在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)

【问题讨论】:

    标签: r shiny flexdashboard


    【解决方案1】:

    如果您想将您的 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)
    

    【讨论】:

    • 好的,这就是答案
    【解决方案2】:

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

    【讨论】:

    • 谢谢,但我想避免观察我的实际情况
    • 我很好奇你为什么不想使用观察者? sendSweetAlert 实际上并不需要(或使用)相应的 UI 元素。
    猜你喜欢
    • 2019-12-21
    • 2021-05-25
    • 2021-11-21
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 2021-12-28
    相关资源
    最近更新 更多