简体   繁体   English

在 R/Shiny 的引导卡中渲染 DT 数据表

[英]Render DT Datatables in Bootstrap Card in R/Shiny

Below is a minimal reproducible example of my problem.下面是我的问题的最小可重现示例。 What I need to do is render a datatable inside of a bootstrap card.我需要做的是在引导卡内呈现数据表。 In the example below, the rendering of output$somethingMore does just that.在下面的示例中, output$somethingMore的呈现就是这样做的。 However, in my real world example, I have a slightly more complicated scenario where I cannot pass the data table to the card as I have done there.然而,在我的真实世界的例子中,我有一个稍微复杂的场景,我无法像在那里那样将数据表传递给卡。

Instead, I need to create a table rendered as in my output$brokenIdea example and then take that object and put it inside the card.相反,我需要创建一个在我的output$brokenIdea示例中呈现的表格,然后将该对象放入卡片中。 Of course, my brokenIdea example below is indeed broken, or perhaps even more fallible than that in that's conceptually a bad idea.当然,我下面的 brokenIdea 示例确实是错误的,或者可能比这更容易出错,因为这在概念上是一个坏主意。

However, I am looking to see if there is a solution to this idea so that output$brokenIdea can be created and then passed to the card in a renderUI.但是,我希望看看是否有解决这个想法的方法,以便可以创建 output$brokenIdea,然后在 renderUI 中将其传递给卡。

For some context for those who might ask why, this is needed because I have an editable DT table in my real world app and (to my knowledge) being able to edit a data table as in this example here requires an observer paying attention to whether the table outputted in the browser is edited.对于那些可能会问为什么的人来说,这是需要的,因为我在现实世界的应用程序中有一个可编辑的 DT 表,并且(据我所知)能够像本例中那样编辑数据表, 需要观察者注意是否编辑浏览器中输出的表格。

My code doesn't have those details in it, but the example above does show the context of the overall situation.我的代码中没有这些细节,但上面的示例确实显示了整体情况的上下文。

library(shiny)
library(bslib)
library(shinyWidgets)
library(DT)

card <- function(body, title) {
  div(class = "card",
    div(icon("chart-line", style = "color:white"), class = "card-header bg-success text-white text-center font-weight-bold", title),
    div(class = "card-body d-flex justify-content-center", body)
  )
}

ui <- fluidPage(

    navbarPage(
        theme = bs_theme(bootswatch = "flatly", version = 4),
        title = 'Methods',
        tabPanel('One'),
    ),
    mainPanel(
        h1('Hello World'),      
        
    uiOutput('something'),
    br(),
    DTOutput('somethingElse'),
    br(),
    uiOutput('somethingMore'),
    #uiOutput('brokenIdea')
        
    )
)

server <- function(input, output) {

    output$something <- renderUI({
        card('Test', 'Hello')
    })

    output$somethingElse <- renderDT({
        tab <- data.frame(x= rnorm(5), y = rnorm(5))
        DT::datatable(tab)
    })
    
    ### I could do this
    output$somethingMore <- renderUI({
        tab <- data.frame(x= rnorm(5), y = rnorm(5))
        out <- DT::datatable(tab)
        card(out, 'Hi')
    })
    
    ### But what I need is
    output$brokenIdea <- renderUI({
        card(output$somethingElse, 'Can this work')
    })
}

shinyApp(ui, server) 

Put your DTOutput('somethingElse')...) inside renderUI将您的DTOutput('somethingElse')...)放入renderUI

library(shiny)
library(bslib)
library(shinyWidgets)
library(DT)

card <- function(body, title) {
    div(class = "card",
        div(icon("chart-line", style = "color:white"), class = "card-header bg-success text-white text-center font-weight-bold", title),
        div(class = "card-body d-flex justify-content-center", body)
    )
}

ui <- fluidPage(
    
    navbarPage(
        theme = bs_theme(bootswatch = "flatly", version = 4),
        title = 'Methods',
        tabPanel('One'),
    ),
    mainPanel(
        h1('Hello World'),      
        
        uiOutput('something'),
        br(),
        br(),
        uiOutput('somethingMore'),
        uiOutput('brokenIdea')
        
    )
)

server <- function(input, output) {
    
    output$something <- renderUI({
        card('Test', 'Hello')
    })
    
    tab <- reactive({
        invalidateLater(3000)
        data.frame(x= rnorm(5), y = rnorm(5))
    })
    output$somethingElse <- renderDT({
        DT::datatable(tab())
    })
    
    ### I could do this
    output$somethingMore <- renderUI({
        tab <- data.frame(x= rnorm(5), y = rnorm(5))
        out <- DT::datatable(tab)
        card(out, 'Hi')
    })
    
    ### But what I need is
    output$brokenIdea <- renderUI({
        card(DTOutput('somethingElse'), 'Can this work')
    })
}

shinyApp(ui, server) 

reactive is used in my example to hold the table.在我的示例中使用reactive来保存表格。 To simulate the table changing dynamically, I make it change every 5 seconds.为了模拟表格动态变化,我让它每 5 秒改变一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM