창 크기에 따라 동적 너비/높이를 가진 플롯을 생성하는 여러 가지 Shiny 응용 프로그램이 있습니다.반짝이는 모듈 내 창 크기 활용
내 의도는 항상 navbar
, navlist
, tabPanel
및 모듈 as specified here의 조합을 사용하여 하나의 응용 프로그램에있는 모든 응용 프로그램을 결합하는 것이 었습니다. 때문에 앱 구성 요소가 나는 내 코드의 대부분을 모듈화 한, 결합하고있어 많은 수의에
library(shiny)
library(plotly)
# ui.R file below
ui <- 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);
});
')),
navlistPanel(
tabPanel("Dynamic Dimensions",
plotlyOutput("myPlot")
)
)
)
)
# server.R file below
server <- function(input, output) {
output$myPlot <- renderPlotly({
plot_ly(midwest, x = ~percollege, color = ~state, type = "scatter",
width = (0.6 * as.numeric(input$dimension[1])),
height = (0.75 * as.numeric(input$dimension[2])))
})
}
# Typically I replace below with run.R file and launch the app in browser
shinyApp(ui = ui, server = server)
: 예를 들어 작업
는 아래 없이 사용하는 모듈을 제공한다. 여기서 치수 변수를 호출하는 데 어려움이 있습니다 (치수가 무시되는 것 같습니다) ns
함수로 포장해도이 변수가 호출되지 않습니다. 아래 코드의 전체입니다 위의 작업 애플 리케이션에서 변환 실패. 이것은 실제로 작업을 수행하지만, 폭은 제대로 업데이트되지 않습니다
myPlot 모듈 :
myPlotUI <- function(id, label = "My Plot"){
ns <- NS(id)
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);
});
"))
tagList(
plotlyOutput(ns("myPlot"))
)
}
myPlot <- function(input, output, session){
ns <- session$ns
output$myPlot <- renderPlotly({
plot_ly(midwest, x = ~percollege, color = ~state, type = "scatter",
width = (0.6 * as.numeric(input$dimension[1])),
height = (0.75 * as.numeric(input$dimension[2])))
})
}
서버, UI 및 shinyApp을 : 나는 액세스 할 수있는 방법에 대한
server <- function(input, output, session){
callModule(myPlot, "myPlot")
}
# ui.R file below
ui <- shinyUI(fluidPage(
# I've tried putting the js code in this section of the UI. Didn't work...
navlistPanel(
tabPanel("Dynamic Dimensions",
myPlotUI("myPlot")
)
)
)
)
shinyApp(ui = ui, server = server)
모든 팁 모듈화 된 플롯 객체 내의 창 크기? 감사!
감사합니다. –