我想将
plotlyOutput
的高度和宽度调整为当前窗口大小。我尝试过使用以下但没有用。
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)
您是否知道在服务器功能中可以使用任何其他选项来代替上述 UI 功能用法?
它没有回答您的问题,但根据我的评论,您可以使用
this链接中的 js 将绘图高度和宽度添加到
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 的帖子。
我只是想针对特殊用例添加该响应:通过用鼠标抓住窗口的边缘之一并调整其大小,来降低窗口缓慢调整大小时 Shiny 尝试重新渲染可视化的频率方式。对我来说,渲染时间需要一段时间,如果没有这些额外的代码,它只需从一个窗口调整大小就可以进行多次重新渲染。
在我的
ui.R
里面,我有一个tags$script
,其中包含以下JS字符串:
console.log("INFO: Loaded ui.R custom JS.");
// VARS
const REFRESH_RATE_MIN_SECONDS = 1.5;
var dimension = [0, 0];
var notRecentlyRendered = true;
var midRender = false;
var timeNow = Date.now();
var lastRendered;
var nowDiffMs;
// METHODS
$(document).on("shiny:connected", function(e) {
dimension[0] = window.innerWidth;
dimension[1] = window.innerHeight;
Shiny.onInputChange("dimension", dimension);
})
$(window).resize(function(e) {
timeNow = Date.now();
firstRender = (typeof(lastRendered) === "undefined");
if (firstRender === true) {
nowDiffMs = timeNow - lastRendered;
refreshRateMinMs = REFRESH_RATE_MIN_SECONDS * 1000;
notRecentlyRendered = nowDiffMs > refreshRateMinMs;
}
if ((midRender === false) && (notRecentlyRendered === true)) {
console.log("INFO: Adjusting figure height.");
dimension[0] = window.innerWidth;
dimension[1] = window.innerHeight;
midRender = true;
Shiny.onInputChange("dimension", dimension);
}
})
在我的
server.R
文件中,我有我的观察结果:
observeEvent(input$dimension,{
output$multi_indicator_facility <- renderPlotly({
plot.multi_indicator_facility(input)
})
在另一个文件中,我有
plot.multi_indicator_facility()
,大致如下:
gg = ggplot(...) # ... = a lot of code
ggp = ggplotly(gg, height=figure.height) # I took figure.height from input$dimension
# More JS within R:
ggp = onRender(
ggp, '
function(el) {
console.log("Rendered.");
lastRendered = Date.now();
midRender = false;
}
')
return(ggp)
另外不要忘记包含
htmlwidgets
库以使用 onRender。
library('htmlwidgets') # <-- include this somewhere (e.g. server.R)
我们可以简单地使用相对CSS单元:
library(shiny)
ui <- fluidPage(
plotlyOutput("plot", width = "90vw", height = "90vh")
)
server <- function(input, output, session) {
output$plot <- renderPlotly({plot_ly(x = 1:10, y = 1:10, type = "scatter", mode = "lines+markers")})
}
shinyApp(ui, server)