r - R Shiny 表格下方的中心表格

标签 r shiny

我希望在同样居中的表格下方居中放置一个小表单。 我使用 fluidRowcolumn 将表格居中,如下所示:

fluidRow(
    column(12, align="center", reactableOutput("table")),
  ),

如果我对表单执行相同的操作,表单的每个组件都会在页面中居中,这是错误的。如何将外观正确的表单置于居中的表格下方?

示例代码

library(shiny)
library(reactable)

ui <- fluidPage(
  fluidRow(
    column(12, align="center", reactableOutput("table")),
  ),
  fluidRow(  
    column(12, 
           div(id = "form", 
               textInput("email", "Email", width = "250px", placeholder = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2b8bdb792b7aab3bfa2beb7fcb1bdbf" rel="noreferrer noopener nofollow">[email protected]</a>"),
               radioButtons(inputId = "pref", 
                            label ="Here's a label:",
                            choiceNames = list(
                              "First choice", 
                              "Second choice"),
                            choiceValues = list(
                              "v1", "v2"
                            )),
               actionButton("submit", "Enter", class = "btn-primary", width = 250, 
                            style="color: #FFF; background-color: #132EBA;"),
             
            )
    )
  )
)

server <- function(input, output, session) {
  output$table <- renderReactable({
    reactable(iris,
              fullWidth = FALSE)
  })
  
  observeEvent(input$submit, {
    # Do something!
  })
}

shinyApp(ui, server)

最佳答案

您需要创建 2 个 div 元素并赋予它们 CSS 属性:

  • 第一个居中
  • 第二个是内联 block ,左对齐

Result screenshot

来源:CSS: Center block, but align contents to the left

所以它给出了

library(shiny)
library(reactable)

ui <- fluidPage(
  fluidRow(
    column(12, align="center", reactableOutput("table")),
  ),
  fluidRow(  
    column(12, 
           div(id = "form", 
               style = "text-align: center;",
               div(
                 id = "form_content",
                 style = "display:inline-block; text-align: left;",
                 textInput("email", "Email", width = "250px", placeholder = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5a30353f1a3f223b372a363f74393537" rel="noreferrer noopener nofollow">[email protected]</a>"),
                 radioButtons(inputId = "pref", 
                              label ="Here's a label:",
                              choiceNames = list(
                                "First choice", 
                                "Second choice"),
                              choiceValues = list(
                                "v1", "v2"
                              )),
                 actionButton("submit", "Enter", class = "btn-primary", width = 250, 
                              style="color: #FFF; background-color: #132EBA;")
               )
           )
    )
  )
)

server <- function(input, output, session) {
  output$table <- renderReactable({
    reactable(iris,
              fullWidth = FALSE)
  })
  
  observeEvent(input$submit, {
    # Do something!
  })
}
shinyApp(ui, server)

关于r - R Shiny 表格下方的中心表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64997122/

相关文章:

r - 使用 data.table 的垂直条件创建一个新列

R:将表达式传递给内部函数

r - R语言中的变量冲突

runapp从其他带有操作按钮的 Shiny 应用程序中 Shiny

r - 尝试部署 Shiny 的应用程序 : multiple existing deployments from this application directory already exist 时出错

r - 如何在R中查找数据集

r - 使用 spsample() 对 SpatialPolygonsDataFrame 进行采样会导致 seq.default() 出现错误

r - 无法使用 Shiny 的 actionButton 和 eventReactive 方法在 flexdashboard 的 tabset 面板中获得预测图

r - 对使用 RStudio 的 Shiny 在函数之间传递数据帧感到困惑

r - 如何在 DT 的左侧有一个像 Excel 中那样的行号?