如何在不缩小 r shiny 矩阵大小的情况下将列添加到输入矩阵?
How to add columns to input matrix without shrinking matrix size in r shiny?
每当我向矩阵添加列时,列的宽度都会缩小,直到它们变得太小。有没有办法增加页面的宽度,使矩阵列不缩小?谢谢
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))
})
这可以使用 CSS。一旦我们覆盖了 HTML 元素 table
的 table-layout: fixed;
,header 单元格就会响应 min-width
属性。您可以根据需要调整最小宽度。
确保您使用正确的 inputID
到 select HTML table。在 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 中测试了这个示例。
每当我向矩阵添加列时,列的宽度都会缩小,直到它们变得太小。有没有办法增加页面的宽度,使矩阵列不缩小?谢谢
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))
})
这可以使用 CSS。一旦我们覆盖了 HTML 元素 table
的 table-layout: fixed;
,header 单元格就会响应 min-width
属性。您可以根据需要调整最小宽度。
确保您使用正确的 inputID
到 select HTML table。在 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 中测试了这个示例。