基于数据表长度的flexDashboard动态调整行的动态调整

发布于 2025-02-02 21:52:04 字数 2465 浏览 4 评论 0 原文

我正在在R中开发一个flexdashboard。 我了解我们设置了诸如行高的起始维度,并且Shiny可以自动调整的大小,同时保持起始比例的比例。

我可以动态地修改比例比例吗? < br> 假设基于显示的长度数据?

在此可再现示例中,下面有一个数据表和一个图表。

DataTable具有2个长度选项。

我希望调整数据容器的大小以使其与桌子齐平,并留出更多的图表空间。相反,如果我们选择最大的长度,我希望完全显示该数据表(没有滚动),从而减少显示的图表。

例子:

---
title: "Dynamic Resizing?"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: fill
    runtime: shiny
    
---

```{r setup, include=FALSE}
library(flexdashboard)
library(DT)
library(ggplot2)
```

Row {data-height=500}
-----------------------------------------------------------------------

### The Table

```{r}

output$my_table <-  DT::renderDataTable({
  
    DT::datatable(mtcars %>% head(20), 
                  options = list( dom = 'Blfrt'
                                  , searching = FALSE
                                  , lengthMenu = c(5, 10)
                                  , pageLength = 5
                                  , scrollY = 300
                                  , scrollCollapse = FALSE
                                  ))
  
  })

dataTableOutput('my_table')

```

Row {data-height=500}
-----------------------------------------------------------------------

### The Chart

```{r}

output$my_chart <- renderPlot({
  
ggplot(mtcars, aes(x=hp, y=mpg, color=cyl)) +
  geom_point(size=3)


})

plotOutput("my_chart") 

```

我已经测试了很多事情,没有任何成功。

显然,行高度是硬编码:行{data-height = 500} 。 也许有一种简单的方法可以动态?

我可以写:

 Row {data-height= `r variable` }  

但是它没有反应性。

我可以创建一个反应价值,可以将其输入标题部分,但对于行高度不起作用。我猜 RenderText 不适合“值”。

例如:

```{r}
h <- reactiveValues(val = 200)
```

Row {data-height=`r renderText(h$val)`}
-----------------------------------------------------------------------

### The Table `r renderText(h$val)`


否则,此问题与我的相似,但是诀窍是将两个图表组合成一个图表,这在我的情况下不适用,因为它是带有图表的表。

由于我的问题涉及数据。 flexdashboard中的表高度?。它涉及CSS,其中包含此代码:

<style>
.dataTables_scrollBody {
    max-height: 100% !important;
}
</style>

但是它似乎不起作用。我感觉到答案可能是通过CSS来的,但无法弄清楚。...

任何专家都可以加入吗?我缺少这一点很明显。

提前致谢!

I’m developing a Flexdashboard in R.
I understand that we set the starting dimensions such as Row height, and that Shiny will automatically resize things while keeping the starting proportion ratio.

Can I dynamically modify the proportion ratio?
Let’s say based on the length datatable being displayed?

In this reproducible example, there's a datatable and a chart below.

Datatable has 2 length option.

I would like the datatable container to be resized to fit flush with the table, leaving more room for the graph. Inversely, if we select the largest length, I want the datatable to be shown entirely (no scroll), therefore reducing the chart displayed.

Example:

---
title: "Dynamic Resizing?"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: fill
    runtime: shiny
    
---

```{r setup, include=FALSE}
library(flexdashboard)
library(DT)
library(ggplot2)
```

Row {data-height=500}
-----------------------------------------------------------------------

### The Table

```{r}

output$my_table <-  DT::renderDataTable({
  
    DT::datatable(mtcars %>% head(20), 
                  options = list( dom = 'Blfrt'
                                  , searching = FALSE
                                  , lengthMenu = c(5, 10)
                                  , pageLength = 5
                                  , scrollY = 300
                                  , scrollCollapse = FALSE
                                  ))
  
  })

dataTableOutput('my_table')

```

Row {data-height=500}
-----------------------------------------------------------------------

### The Chart

```{r}

output$my_chart <- renderPlot({
  
ggplot(mtcars, aes(x=hp, y=mpg, color=cyl)) +
  geom_point(size=3)


})

plotOutput("my_chart") 

```

I've tested many things without any success.

Obviously, the row height is hardcoded: Row {data-height=500}.
Perhaps there is an easy way to have it dynamic?

I can write:

 Row {data-height= `r variable` }  

However it is not reactive.

I can create a reactiveValue which I can feed to the title section, but it does not work for the Row height. I guess renderText is inappropriate for a "value".

Ex:

```{r}
h <- reactiveValues(val = 200)
```

Row {data-height=`r renderText(h$val)`}
-----------------------------------------------------------------------

### The Table `r renderText(h$val)`

Otherwise, this question Dynamic Re-sizing Flexdashboard/Plotly Image to a Different Graphic is similar to mine, but the trick was to combine the two charts into one, which doesn't apply in my case because it's a table with a chart.

Since my question involves Data.Table, I had hopes with this one: How to adjust table height in flexdashboard? . It involves CSS, with this code:

<style>
.dataTables_scrollBody {
    max-height: 100% !important;
}
</style>

But it didn't seem to work. I have a got feeling the answer might be through CSS but could not figure it out....

Any expert can chime in on this? It's probably something super obvious that I'm missing.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文