R 根据窗口大小动态调整打印输出的高度和/或宽度

R 根据窗口大小动态调整打印输出的高度和/或宽度,r,ggplot2,shiny,plotly,ropensci,R,Ggplot2,Shiny,Plotly,Ropensci,我希望将闪亮的plotly输出高度和宽度调整为当前窗口大小。我试过使用下面的,但没有用 ShinyUi <- fluidPage( # Application title titlePanel("title"), sidebarLayout( sidebarPanel( ... inputs ... ), mainPanel( plotlyOutput("distPlot", height = 'auto', wid

我希望将闪亮的plotly输出高度和宽度调整为当前窗口大小。我试过使用下面的,但没有用

ShinyUi <- fluidPage(

  # Application title
  titlePanel("title"),

  sidebarLayout(
    sidebarPanel(
      ... inputs ...
    ),

    mainPanel(
          plotlyOutput("distPlot", height = 'auto', width = 'auto')
      )
  ))

ShinyServer <- function(input, output, session) {

   output$distPlot <- renderPlotly({

    p <- ggplot(dataShow, aes(x=dataShow$X, y=dataShow$Y))  + 
geom_point(shape=1, alpha = 0.5, color = "grey50")

    ggplotly(p)

  })

}


# Run the application 
shinyApp(ui = ShinyUi, server = ShinyServer)

ShinyUi它没有回答您的问题,但是根据我的评论,您可以使用js-from链接将绘图高度和宽度添加到
ggplotly
函数中

我已经准备了一个你想要的最简单的例子

library(shiny)
library(plotly)

ShinyUi <- fluidPage(
  tags$head(tags$script('
                        var dimension = [0, 0];
                        $(document).on("shiny:connected", function(e) {
                        dimension[0] = window.innerWidth;
                        dimension[1] = window.innerHeight;
                        Shiny.onInputChange("dimension", dimension);
                        });
                        $(window).resize(function(e) {
                        dimension[0] = window.innerWidth;
                        dimension[1] = window.innerHeight;
                        Shiny.onInputChange("dimension", dimension);
                        });
                        ')),

      plotlyOutput("distPlot", width = "auto")

  )

ShinyServer <- function(input, output, session) {


  #To make the responsive to the change in UI size
  observeEvent(input$dimension,{

    output$distPlot <- renderPlotly({

      p <- ggplot(iris, aes(x = Sepal.Length, y=Sepal.Width))  +
        geom_point(shape=1, alpha = 0.5, color = "grey50")
      ggplotly(p, width = (0.95*as.numeric(input$dimension[1])), height = as.numeric(input$dimension[2]))

    })

  })

}


# Run the application 
shinyApp(ui = ShinyUi, server = ShinyServer)
库(闪亮)
图书馆(绘本)
新余前传
请先看@SBista的帖子

我只想添加一个特殊用例的响应:通过用鼠标抓住窗口的一个边缘并以这种方式调整大小,降低重新渲染可视化的次数。对我来说,渲染时间需要一段时间,如果没有这段额外的代码,它只需要从一个窗口背靠背地重新渲染几次

答复 在我的
ui.R
中,我有一个带有以下JS字符串的
标记$script

console.log(“INFO:Loaded ui.R custom JS”);
//瓦尔斯
常数刷新率最小秒=1.5;
变量维度=[0,0];
var notRecentlyRendered=true;
var midRender=false;
var timeNow=Date.now();
无功功率;
var-nowdifms;
//方法
$(文档).on(“闪亮:连接”,函数(e){
维度[0]=window.innerWidth;
尺寸[1]=窗内高度;
有光泽。onInputChange(“维度”,维度);
})
$(窗口)。调整大小(函数(e){
timeNow=Date.now();
firstRender=(typeof(LastRender)==“未定义”);
如果(firstRender==true){
nowDiffMs=timeNow-lastRendered;
refreshRateMinMs=刷新率\分钟\秒*1000;
notRecentlyRendered=nowDiffMs>refreshRateMinMs;
}
if((midRender==false)&&(notRecentlyRendered==true)){
console.log(“信息:调整图形高度”);
维度[0]=window.innerWidth;
尺寸[1]=窗内高度;
middrender=true;
有光泽。onInputChange(“维度”,维度);
}
})
在我的
server.R
文件中,我有我的可观察到的:

observeEvent(输入$dimension{

输出$multi_indicator_设施您是否已经在使用
fluidPage()
?@BigDataScientist请查看更新帖子中包含的代码结构。你说的“闪亮打印输出高度和宽度调整为当前窗口大小”是什么意思?@SBista它应该根据可用窗口属性调整大小。或者在其他情况下换句话说,它应该一直占据窗口面积的75%。为了获得更清晰的图片,我在增加浏览器窗口大小之前和之后都添加了数字。一种繁琐且需要
js
的方法是获取窗口大小并将其传递给
ggplotly
函数。请参阅链接以获取窗口大小。是的,这是虽然它没有回答我的问题,但它提供了实现解决方案的有用方法。(如果在未来几天内没有另一个简单的答案,我将接受这一(变通)答案。)我觉得他在很大程度上回答了你的问题。看来R的plotly库并没有一个简单的方法让你实现这一点。他的解决方案比我自己的更优雅,但却找到了同样好的东西。