我有一个像这样的 UI 元素
fluidRow(column(12, rbokehOutput("linePlots")) )
Run Code Online (Sandbox Code Playgroud)
server.R 返回
grid_plot(p1,ncol=3,byrow=TRUE, same_axes=FALSE,width=1500)
Run Code Online (Sandbox Code Playgroud)
这是一个 rbokeh 图。当窗口大小调整时,我想返回一个具有不同 ncol 值的替代 grid_plot 。这在闪亮中可能吗?
是的,这是可能的,您可以在页面 Javascript 中创建 Shiny 绑定,并在窗口调整大小事件上调用 R 函数。结合此 renderUI,您可以将 DOM 事件绑定到更新 UI 的 R 函数。
require(shiny)
shinyApp(
ui = shinyUI(fluidPage(
tags$head(
tags$script(HTML('
$(window).resize(function(event){
var w = $(this).width();
var h = $(this).height();
var obj = {width: w, height: h};
Shiny.onInputChange("pltChange", obj);
});
'))
),
fluidRow( column(12, uiOutput('ui')) ),
fluidRow( column(12, textOutput('txt')) )
)),
server = shinyServer( function(input,output,session){
data <- list(x=runif(10),y=runif(10),main="no resize")
observeEvent(input$pltChange,{
output$ui <- renderUI({
plotOutput("linePlots")
})
str <- sprintf('Window height: %d, Window width %d', input$pltChange$height, input$pltChange$width)
output$txt <- renderText({
print(str)
})
data$main <- str
output$linePlots <- renderPlot({ plot(data,main=data$main) })
})
})
)
Run Code Online (Sandbox Code Playgroud)
此示例创建一个plotOutput并在窗口大小调整后填充它。这只是基本原理的一个例子。由于您没有提供可重现的示例,也许这可以帮助您了解如何做到这一点。