2017-11-21 15 views
0

창 크기에 따라 동적 너비/높이를 가진 플롯을 생성하는 여러 가지 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) 

모든 팁 모듈화 된 플롯 객체 내의 창 크기? 감사!

답변

1

모듈에서 액세스 한 입력 값은 네임 스페이스이며, Shiny.onInputChange으로 설정된 입력 값은 네임 스페이스가 아닙니다.

따라서 myPlot 모듈에서 input$dimensionmyPlot-dimension이되지만 실제로 입력은 dimension입니다.

하나의 솔루션은 스크립트에 네임 스페이스 ID를 사용할 수 있도록하는 것입니다 :

library(shiny) 
library(plotly) 

myPlotUI <- function(id, label = "My Plot") { 
    ns <- NS(id) 
    dimensionId <- ns("dimension") 

    tagList(
    tags$head(tags$script(sprintf(" 
     var dimensionId = '%s'; 
     var dimension = [0, 0]; 

     $(document).on('shiny:connected', function(e) { 
     dimension[0] = window.innerWidth; 
     dimension[1] = window.innerHeight; 
     Shiny.onInputChange(dimensionId, dimension); 
     }); 

     $(window).resize(function(e) { 
     dimension[0] = window.innerWidth; 
     dimension[1] = window.innerHeight; 
     Shiny.onInputChange(dimensionId, dimension); 
     }); 
    ", dimensionId))), 

    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]))) 
    }) 

} 

server <- function(input, output, session){ 
    callModule(myPlot, "myPlot") 
} 

ui <- fluidPage(
    navlistPanel(
    tabPanel("Dynamic Dimensions", 
      myPlotUI("myPlot")) 
) 
) 

shinyApp(ui = ui, server = server) 

고지되어 또 다른 해결책 : 위험, 문서화, 학대가 발생하기 쉬운 기능을! 실제로 모듈에서 session$rootScope()을 통해 루트 세션을 가져올 수 있습니다. 당신이 정말로해야하지 않는 한 추천하지 않겠지 만, 단지 FYI.

library(shiny) 
library(plotly) 

myPlotUI <- function(id, label = "My Plot") { 
    ns <- NS(id) 

    tagList(
    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(ns("myPlot")) 
) 
} 

myPlot <- function(input, output, session) { 
    ns <- session$ns 
    rootInput <- session$rootScope()$input 

    output$myPlot <- renderPlotly({ 
    plot_ly(midwest, x = ~percollege, color = ~state, type = "scatter", 
      width = (0.6 * as.numeric(rootInput$dimension[1])), 
      height = (0.75 * as.numeric(rootInput$dimension[2]))) 
    }) 

} 

server <- function(input, output, session){ 
    callModule(myPlot, "myPlot") 
} 

ui <- fluidPage(
    navlistPanel(
    tabPanel("Dynamic Dimensions", 
      myPlotUI("myPlot")) 
) 
) 

shinyApp(ui = ui, server = server) 
+0

감사합니다. –