首页 > 解决方案 > 在 rstudio 查看器中控制数据表输出的高度

问题描述

考虑以下降价文档:

---
title: "R Notebook"
output: html_notebook
---

```{r}
library(shiny)

ui <- fluidPage(
    DT::dataTableOutput("tbl")
)
server <- function(input, output, session){
    output$tbl = DT::renderDataTable(
        mtcars,
        server = FALSE,
        selection = list(mode = "multiple", target = "column", selected = c(1)),
        options = list(pageLength = 10, autoWidth = TRUE)
    )
}

runApp(
    appDir = shinyApp(ui, server), 
    launch.browser = rstudioapi::viewer
)
```

如果我从 rmarkdown 运行闪亮的应用程序,则表格未完全显示。如果我点击“在浏览器中打开”,一切都会很好。

问题:

如果我从 rmarkdown 调用应用程序,如何在查看器的整个页面上显示表格?

在此处输入图像描述

标签: rshinyrstudior-markdown

解决方案


我认为最简单的方法是添加heightdataTableOutput. 你可以玩弄数字,也可以试试“rem”、“px”等——“em”最适合我。您可能还想尝试添加width参数,以便在调整窗口大小时表格缩放,并可能尝试autoWidth从中删除options,具体取决于最终产品的外观。

---
title: "R Notebook"
output: html_notebook
---

```{r}
library(shiny)

ui <- fluidPage(
    DT::dataTableOutput("tbl", height = "40em")
)
server <- function(input, output, session){
    output$tbl = DT::renderDataTable(
        mtcars,
        server = FALSE,
        selection = list(mode = "multiple", target = "column", selected = c(1)),
        options = list(pageLength = 10, autoWidth = TRUE)
    )
}

runApp(
    appDir = shinyApp(ui, server), 
    launch.browser = rstudioapi::viewer
)
```

数据表截图


推荐阅读