【问题标题】:Control height of datatableoutput in rstudio viewer在 rstudio 查看器中控制数据表输出的高度
【发布时间】:2020-06-03 21:08:51
【问题描述】:

考虑以下降价文档:

---
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 调用应用程序,如何在查看器中的整个页面上显示表格?

【问题讨论】:

    标签: r shiny rstudio r-markdown


    【解决方案1】:

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

    ---
    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
    )
    ```
    

    【讨论】:

    • 哈哈,我试过 height = 100%,但没用。我实际上以为我尝试了替代方案。谢谢!它超出了我的问题范围,但是您知道是否有可能将高度缩放到查看器窗格的高度?
    • @Tlatwork 恐怕不会。您需要某种方法来按数量或行细分表格高度,即随着表格的扩大,行会扩大,或者随着包含表格的扩大,应该会出现更多的行。无论哪种方式,它都比将表格高度设置为某个绝对或相对尺寸要复杂一些。我确信有一种方法可以设置行高,尽管它可能不会立即显而易见。也许通过一些自定义 CSS?
    • 我刚刚发现rstudioapi::viewer(..., height = 300) 接受了一个高度参数。因此,根据查看器窗格可能至少有一个近似的解决方案来缩放表格。有趣的是,对我来说,只有在 Parameter 增加窗格的当前高度时才使用 height 参数。如果它更低,则不会...
    • 嗯,但不确定是否可以将它与runApp(,...launch.browser = rstudioapi::viewer,...) 一起使用,...
    猜你喜欢
    • 2021-03-27
    • 2010-11-09
    • 2016-03-21
    • 1970-01-01
    • 2022-01-24
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多