在闪亮的RMD中工作时,是否有R-way将Plotly窗口高度设置为100%?

发布于 2025-02-03 08:25:13 字数 1598 浏览 2 评论 0 原文

我正在使用RMD文件开发交互式报告,该报告指定了闪亮的运行时。但是,我遇到了以下问题。

当绘制具有较大高度规格的GGPLOTS时,它们会开始覆盖HTML输出的其他方面(例如在Exaple中,此处为文本)。到目前为止,我的研究已经产生了Poltly容器中的固定设置 Height == 400px 对此负责。

---
runtime: shiny
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)

library(tidyverse)
library(shiny)
library(plotly) 
```

```{r}
fluidRow(
  column(3,
         selectInput("myselectorID",
                     "Select:",
                     selected = "setosa",
                     choices = c("setosa", "versicolor", "virginica")))
)

renderPlotly({
  data <- iris %>% filter(Species == input[["myselectorID"]])
  ggp <- data %>% ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
    geom_point()
  ggplotly(ggp, height = 487)
})
```
START
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT 
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
END

如果我提供以下 css (例如,通过yaml标头),则该问题已经解决:

.plotly {
  height: 100% !important;
}

我更喜欢将参数传递到r函数的解决方案。有什么想法吗?谢谢你!

I am in the process of developing interactive reports with Rmd files, which specify a shiny runtime. I have, however, run into the following issue.

When plotlyfying ggplots that have larger height specifications, they start overlaying other aspects of the html output (e.g. in the exaple here its the text). So far my research has yielded that a fixed setting of height == 400px in the poltly container is responsible for this.

---
runtime: shiny
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)

library(tidyverse)
library(shiny)
library(plotly) 
```

```{r}
fluidRow(
  column(3,
         selectInput("myselectorID",
                     "Select:",
                     selected = "setosa",
                     choices = c("setosa", "versicolor", "virginica")))
)

renderPlotly({
  data <- iris %>% filter(Species == input[["myselectorID"]])
  ggp <- data %>% ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
    geom_point()
  ggplotly(ggp, height = 487)
})
```
START
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT 
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
END

If I supply the following css (e.g. via the yaml header) then the problem is supposedly solved:

.plotly {
  height: 100% !important;
}

I would prefer a solution where I pass the parameter to R functions though. Any ideas? Thank you!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

有人回答了这一点,并删除了他们的答案……不确定为什么,但这是他们为记录发布的内容,因为它似乎可以解决问题:

---
runtime: shiny
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)

library(tidyverse)
library(shiny)
library(plotly) 
```

```{r}
fluidRow(
  column(3,
         selectInput("myselectorID",
                     "Select:",
                     selected = "setosa",
                     choices = c("setosa", "versicolor", "virginica")))
)

# create a specific plotly output with the parameter set as desired
plotlyOutput("testplot", height = "100%")

# address the plotly output
output$testplot <- renderPlotly({
  data <- iris %>% filter(Species == input[["myselectorID"]])
  ggp <- data %>% ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
    geom_point()
  ggplotly(ggp, height = 487)
})
```
START
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT 
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
END

Someone answered this and deleted their answer which was working... Not sure why but here's what they posted for the record since it seemed to do the trick:

---
runtime: shiny
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)

library(tidyverse)
library(shiny)
library(plotly) 
```

```{r}
fluidRow(
  column(3,
         selectInput("myselectorID",
                     "Select:",
                     selected = "setosa",
                     choices = c("setosa", "versicolor", "virginica")))
)

# create a specific plotly output with the parameter set as desired
plotlyOutput("testplot", height = "100%")

# address the plotly output
output$testplot <- renderPlotly({
  data <- iris %>% filter(Species == input[["myselectorID"]])
  ggp <- data %>% ggplot(aes(x = Sepal.Length, y = Sepal.Width)) +
    geom_point()
  ggplotly(ggp, height = 487)
})
```
START
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT 
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT
END
梦行七里 2025-02-10 08:25:13

”上展开扩展

  1. id 在该矿石上以 insertui 稍后放置绘图输出,稍后:
uiOutput('some_plots')
  1. 渲染输出并将其插入指定的输出ID;将绘图高度指定为 plotlyoutput()

示例(插入 id id> id “ some_plots”的三个 ggplotly 输出的三个 ggplotly 输出):

plot_heights = c(200, 150, 350)

for(plot_index in 1:3){
  plot_name = paste0('plot_', plot_index)
  my_plot <- qplot(data = iris,
                   x = Sepal.Length, y = Sepal.Width,
                   geom = 'point') %>% ggplotly()

  output[[plot_name]] <- renderPlotly(my_plot) ## add to output list
  
  insertUI(selector = "#some_plots",
           where = "afterEnd",
           ui = plotlyOutput(plot_name,
                             ## specify plot height:
                             height = paste0(plot_heights[plot_index],'px')
                             )
  )
}

To expand on this solution, you can programmatically insert ggplotly outputs of specified heights like this:

  1. set one ore more ids at which to place the plot output with insertUI later:
uiOutput('some_plots')
  1. render the output and insert it at the specified output id; specify plot height as an argument to plotlyOutput():

Example (inserting three ggplotly outputs of different heights at id "some_plots"):

plot_heights = c(200, 150, 350)

for(plot_index in 1:3){
  plot_name = paste0('plot_', plot_index)
  my_plot <- qplot(data = iris,
                   x = Sepal.Length, y = Sepal.Width,
                   geom = 'point') %>% ggplotly()

  output[[plot_name]] <- renderPlotly(my_plot) ## add to output list
  
  insertUI(selector = "#some_plots",
           where = "afterEnd",
           ui = plotlyOutput(plot_name,
                             ## specify plot height:
                             height = paste0(plot_heights[plot_index],'px')
                             )
  )
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文