【问题标题】:Centering rHandsontableOutput - Shiny居中 rHandsontableOutput - Shiny
【发布时间】:2019-05-04 15:29:51
【问题描述】:

我正在尝试将使用rhandsontable 创建的两个表居中列并获得两个weirdly split tables

当我不尝试居中时,一切都是fine

有谁知道出了什么问题以及如何使我的表格居中?

这是我的 MWE:

rm(list=ls())
library(shiny)
library(rhandsontable)

# Create initial data frames 
x1 <- x2 <- x3 <- x4 <- data.frame(v1=c(1,2), v2 <- c(3,4))

# Server
server <- shinyServer(function(input, output, session) {

  # Data frame 1 to hot 
  previousX1 <- reactive({x1})
  changeX1 <- reactive({
    if(is.null(input$hot_x1)){return(previousX1())}
    else{X1 <- as.data.frame(hot_to_r(input$hot_x1))
    # Perform some operations on X1
    }
  })
  output$hot_x1 <- renderRHandsontable({rhandsontable(changeX1())})

  # Data frame 2 to hot
  previousX2 <- reactive({x2})
  changeX2 <- reactive({
    if(is.null(input$hot_x2)){return(previousX2())}
    else{X2 <- as.data.frame(hot_to_r(input$hot_x2))
    # Perform some operations on X2
    }
  })
  output$hot_x2 <- renderRHandsontable({rhandsontable(changeX2())})

  # Data frame 3 to hot
  previousX3 <- reactive({x3})
  changeX3 <- reactive({
    if(is.null(input$hot_x3)){return(previousX3())}
    else{X3 <- as.data.frame(hot_to_r(input$hot_x3))
    # Perform some operations on X3
    }
  })
  output$hot_x3 <- renderRHandsontable({rhandsontable(changeX3())})

  # Data frame 4 to hot
  previousX4 <- reactive({x4})
  changeX4 <- reactive({
    if(is.null(input$hot_x4)){return(previousX4())}
    else{X4 <- as.data.frame(hot_to_r(input$hot_x4))
    # Perform some operations on X2
    }
  })
  output$hot_x4 <- renderRHandsontable({rhandsontable(changeX4())})
})

# User interface
ui <- shinyUI(fluidPage(
  navbarPage("MWE",
  tabPanel("Center",
            fluidRow(
              column(width=12, align="center", "Alignment (center) with split")),
            fluidRow(
              column(width = 6, align="center", rHandsontableOutput("hot_x1")),
              column(width = 6, align="center", rHandsontableOutput("hot_x2")))
           ),

  tabPanel("Left",

            fluidRow(
              column(width=12, align="center", "Alignment (left) without split")),
            fluidRow(
              column(width = 6, rHandsontableOutput("hot_x3")),
              column(width = 6, rHandsontableOutput("hot_x4")))
           )

  , fluid=TRUE)))

  shinyApp(ui=ui, server=server)

【问题讨论】:

    标签: r shiny centering rhandsontable


    【解决方案1】:

    如果页面上只有一个表格,并且表格有固定的,您可以居中:

    style = "margin-left: calc(50vw - 400px); margin-right: calc(50vw - 400px);"
    

    例如:

    df = ... some data frame ...
    
    ui <- fluidPage(
      br(),
    
      style = "margin-left: calc(50vw - 400px); margin-right: calc(50vw - 400px);",
      rHandsontableOutput('table')
    )
    
    server = function(input, output, session) 
    {
      output$table <- renderRHandsontable({
        rhandsontable(df, width = 800)
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      【解决方案2】:

      我不知道为什么你的表在使用align="center" 时会被拆分;也许这是您应该发布的内容here

      不过,这里有一个使用offset 的解决方法:

      library(shiny)
      library(rhandsontable)
      
      # Create initial data frames
      x1 <- data.frame(v1 = c(1, 2), v2 <- c(3, 4))
      
      # Server
      server <- shinyServer(function(input, output, session) {
      
        # Data frame 1 to hot
        previousX1 <- reactive({
          x1
        })
      
        changeX1 <- reactive({
          if (is.null(input$hot_x1)) {
            return(previousX1())
          }
          else{
            X1 <- as.data.frame(hot_to_r(input$hot_x1))
            # Perform some operations on X1
          }
        })
      
        output$hot_x6 <- output$hot_x5 <- output$hot_x4 <- output$hot_x3 <- output$hot_x2 <- output$hot_x1 <- renderRHandsontable({
            rhandsontable(changeX1())
          })
      
      })
      
      
      
      # User interface
      ui <- shinyUI(fluidPage(navbarPage(
        "MWE",
        tabPanel("Center",
                 fluidRow(
                   column(
                     width = 12,
                     align = "center",
                     "Alignment (center) with split"
                   )
                 ),
                 fluidRow(
                   column(
                     width = 6,
                     align = "center",
                     rHandsontableOutput("hot_x1")
                   ),
                   column(
                     width = 6,
                     align = "center",
                     rHandsontableOutput("hot_x2")
                   )
                 )),
      
        tabPanel("Left",
                 fluidRow(
                   column(
                     width = 12,
                     align = "center",
                     "Alignment (left) without split"
                   )
                 ),
                 fluidRow(
                   column(width = 6, rHandsontableOutput("hot_x3")),
                   column(width = 6, rHandsontableOutput("hot_x4"))
                 )),
      
        tabPanel("Offset",
                 fluidRow(
                   column(
                     width = 12,
                     align = "center",
                     "column offset without split"
                   )
                 ),
                 fluidRow(
                   column(
                     width = 4,
                     offset = 2,
                     rHandsontableOutput("hot_x5")
                   ),
                   column(
                     width = 4,
                     offset = 2,
                     rHandsontableOutput("hot_x6")
                   )
                 )),
        fluid = TRUE
      )))
      
      shinyApp(ui = ui, server = server)
      

      【讨论】:

        猜你喜欢
        • 2017-01-30
        • 2019-01-22
        • 1970-01-01
        • 2022-06-10
        • 2020-04-01
        • 2014-09-28
        • 2019-01-28
        • 2018-05-26
        • 2016-11-27
        相关资源
        最近更新 更多