r - 有没有办法修复 tableOutPut 或 dataTableOutput 的高度?
问题描述
下面粘贴的应用程序有问题;当您将鼠标悬停在某些数据点上时,表格输出的大小会垂直增加,将绘图向下推并取消选择这些点。
library(tidyverse)
library(shiny)
#example text with short and long entries
example_text <- c("short",
"This text is fairly long and it changes the size of the table: maybe there is a scrolling option?",
"also short",
"Here we have another example of long text ruining everything; I would maybe accept it expanding downwards or sideways",
"not long",
"I'm at a loss for what to do so I'm asking stackoverflow, a website that has helped millions of stuck people")
example_data <- tibble(x = (1:6) ^2, y = sqrt(1:6), hover_text = example_text)
ui <- fluidPage(
fluidRow(
column(10,h1( "hover over the points and the changing size of the table moves the plot and deselects points")),
column(2,tableOutput("dynamic_table"))
),
fluidRow(
plotOutput("plot_out",hover = "plot_hover"),
)
)
server <- function(input, output,session) {
output$plot_out <- renderPlot(ggplot(example_data,aes(x = x, y = y)) + geom_point(size = 5) + labs())
table_out <- reactive(nearPoints(df = example_data,
coordinfo = input$plot_hover,
maxpoints = 1,
threshold = 100))
output$dynamic_table <- renderTable(table_out())
}
shinyApp(ui = ui, server = server)
我在这里通过给列宽 2 来强制表格垂直调整大小来强制这种行为,但在我的实际用例中,表格本身非常宽并且即使列宽为 12 也会垂直调整大小。
所以我正在寻找一些东西来永久设置数据表的垂直大小,而不管显示什么文本。它可能是来自不同包的表格输出,该包使用滚动条而不是调整大小。它可能是fluidRow
不动态改变大小的替代方案。一个 hacky 的替代方法是添加一个具有更高元素的列,这样它通常会比表格高,但我没有尝试过/不知道它是否会起作用。
解决方案
最简单的解决方案是添加固定高度,fluidRow
并为要呈现的表格提供足够的空间,您可以通过向style
流体行添加参数并提供足够的空间来做到这一点
fluidRow(id="tablerow",style="height:400px;",
column(10,h1( "hover over the points and the changing size of the table moves the plot and deselects points")),
column(2,tableOutput("dynamic_table"))
)
如果您希望保持流体行的高度较窄,另一种解决方案是向tableOutput
css 添加溢出并修复您的高度,在此示例中我使用 200pxfluidRow
示例代码:
library(tidyverse)
library(shiny)
#example text with short and long entries
example_text <- c("short",
"This text is fairly long and it changes the size of the table: maybe there is a scrolling option?",
"also short",
"Here we have another example of long text ruining everything; I would maybe accept it expanding downwards or sideways",
"not long",
"I'm at a loss for what to do so I'm asking stackoverflow, a website that has helped millions of stuck people")
example_data <- tibble(x = (1:6) ^2, y = sqrt(1:6), hover_text = example_text)
ui <- fluidPage(
tags$head(
# Note the wrapping of the string in HTML()
tags$style(HTML("
#dynamic_table{
overflow-y:auto;
height:200px;
}
"))
),
fluidRow(id="tablerow",style="height:200px;",
column(10,h1( "hover over the points and the changing size of the table moves the plot and deselects points")),
column(2,tableOutput("dynamic_table"))
),
fluidRow(
plotOutput("plot_out",hover = "plot_hover"),
)
)
server <- function(input, output,session) {
output$plot_out <- renderPlot(ggplot(example_data,aes(x = x, y = y)) + geom_point(size = 5) + labs())
table_out <- reactive(nearPoints(df = example_data,
coordinfo = input$plot_hover,
maxpoints = 1,
threshold = 100))
output$dynamic_table <- renderTable(table_out())
}
shinyApp(ui = ui, server = server)
推荐阅读
- c# - Docker 从 github 操作构建,包含 api 密钥等敏感信息
- vue.js - 在 Vue 2 中突出显示时更改输入中的文本
- python - 如何将 3d 数组输入神经网络
- firebase - 检查用户名是否已经存在会给出错误 kotlin
- reactjs - 无法调用可能是“未定义”的对象。ReactTypescript 通过将状态设置器作为道具传递
- python - 当玩家在 Pygame 中按下一个键时图像重复
- ios - SwiftUI - 如何以编程方式滚动到顶部并显示大导航标题?
- linux - Linux - 如何计算单词(不是文件)中字符“i”的出现次数
- transactions - 解释禁止脏写的原因
- reactjs - 使用钩子更新依赖于另一个状态的 React 状态