【问题标题】:How to add columns to input matrix without shrinking matrix size in r shiny?如何在不缩小r闪亮中的矩阵大小的情况下向输入矩阵添加列?
【发布时间】:2021-04-25 23:19:52
【问题描述】:

每当我向矩阵添加列时,列的宽度都会缩小,直到它们变得太小。有没有办法增加页面的宽度,使矩阵列不缩小?谢谢

  output$mat <- renderUI({
        rw<-list(1:input$numoc)
        for (i in (1:input$numoc)) {
            rw[[1]][[i]] = paste("Outcome", i, sep=" ")
        }
 

        clm<-list(1:input$inp1)
        for (i in (1:input$inp1)) {
            clm[[1]][[i]] = paste("Treatment", i, sep=" ")
        }

        matrix1 <- matrix(seq(from=1, to=((input$numoc)*(input$inp1)), by=1), input$inp1, input$numoc, dimnames = list(clm[[1]],rw[[1]]))
        
        matrixInput("mat", "Probabilities", matrix1, rows=list(names=TRUE), cols=list(names=TRUE))
    })

【问题讨论】:

    标签: r user-interface matrix shiny user-input


    【解决方案1】:

    使用 CSS 可以做到这一点。一旦我们覆盖了 HTML 元素 tabletable-layout: fixed;,标题单元格就会响应 min-width 属性。您可以根据需要调整最小宽度。

    确保使用正确的inputID 来选择 HTML 表格。在 CSS 中,ID 以# 开头。

    缺点(当然):有时会出现水平滚动条。

    library(shiny)
    library(shinyMatrix)
    
    # Define UI for application that draws a histogram
    ui <- fluidPage(
    
      tags$head(
        tags$style(HTML(
            "div#mat table {
              table-layout: auto;
            }
            div#mat .matrix-input-col-header-cell div {
              min-width: 100px;
            }"
          ))),
    
        # Application title
        titlePanel("Min Col Width"),
    
        # Sidebar with a slider input for number of bins 
        sidebarLayout(
            sidebarPanel(
                sliderInput("numoc", "Number of bins:",
                            min = 1, max = 50, value = 5),
                numericInput("inp1", "Inp1", 1)
            ),
    
            mainPanel(
               uiOutput("mat") # show the matrix
            )
        )
    )
    
    # Server as provided by the asker
    server <- function(input, output) {
    
      output$mat <- renderUI({
        rw <- list(1:input$numoc)
        for (i in (1:input$numoc)) {
          rw[[1]][[i]] <- paste("Outcome", i, sep = " ")
        }
        
        
        clm <- list(1:input$inp1)
        for (i in (1:input$inp1)) {
          clm[[1]][[i]] <- paste("Treatment", i, sep = " ")
        }
        
        matrix1 <- matrix(seq(from=1, to=((input$numoc)*(input$inp1)), by=1), input$inp1, input$numoc, dimnames = list(clm[[1]],rw[[1]]))
        
        matrixInput("mat", "Probabilities", matrix1, rows=list(names=TRUE), cols=list(names=TRUE))
      })
      
    }
    
    shinyApp(ui = ui, server = server)
    

    我在 Firefox 和 R-Studio App Viewer 中测试了这个示例。

    【讨论】:

    • 效果很好!非常感谢!我认为可能有一种方法可以用 css 覆盖页面宽度,但我不确定如何。谢谢!
    猜你喜欢
    • 2022-11-01
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    相关资源
    最近更新 更多