将以闪亮方式呈现的ggvis图表的默认大小链接到wellPanel的维度

时间:2014-09-13 00:34:15

标签: r svg shiny ggvis vega

我有一个非常酷的闪亮应用程序会查看我在工作中做的一些建模的结果。我使用的其中一个设备是位于wellPanel中的ggvis折线图。我想部署应用程序以便在线查看,但我知道通过在正常大小的屏幕(笔记本电脑)上查看应用程序,ggvis图溢出了wellPanel的边界。对我来说不是很大,因为我有一个巨大的屏幕,如果我在一个小屏幕上看它,我知道我可以交互式地调整ggvis图表的大小,但我不想告诉人们这样做。如果我重新调整ggvis图表的大小,那么wellPanel会响应,因此这两个对象的大小之间必然存在一些联系。

那么有没有办法将渲染的ggvis图表的默认大小设置为它所在的容器的默认大小,而不管用户的屏幕大小如何?我知道ggvis是用SVG渲染的,所以探索它可能会很有成效。我还没有直接与SVG或Vega合作。示例代码如下,这里是shinyapps上托管的应用程序的 link 。有什么想法吗?

注意:我发布的代码显然是供大家使用的。我想我只是想清楚一点,我不会问一个关于如何让这个闪亮的应用程序工作的问题 - 它更多的是如何设计它。当然,我欢迎其他建议。欢呼声。

ui.r

library(shiny)
library(ggvis)

shinyUI(navbarPage("Results Viewer", #theme="bootstrap.css",

  tabPanel("Summary Results",
   fluidRow(
      column(2,
       wellPanel(
        uiOutput("regionO"),
        br(),
        uiOutput("scenarioO"),
        br(),
        uiOutput("fuelO"),
        uiOutput("tmpO")
        )
      ),
     column(5,
       wellPanel(style="position: relative;", 
        h5("Employment",align="center"),
        ggvisOutput("plot1")
        )
      ),
     column(5,
      wellPanel(style="position: relative;", 
        h5("Gross Regional Product (Million Dollars)",align="center"),
        ggvisOutput("plot2")
        )
      )
     ) 
    )
   )
  )

server.r

librarylibrary(ggvis)
library(dplyr)

shinyServer(function(input, output, session) {

observe({

###### Define Controls ####### 

output$regionO <- renderUI({ checkboxGroupInput(inputId="regionI", label = h4("Regions"), 
        choices = list("Western", "Central", "Southern","Northern", "Capital", "Hudson"),
                   selected="Capital")
}) 

output$scenarioO <- renderUI({ selectInput(inputId="scenarioI", label = h4("Scenario"), 
        choices = list("Low Units, Low Tech"="Lo,Lo" ,"Low Units, High Tech"="Lo,Hi","High Units, Low Tech"="Hi,Lo","High Units, High Tech"="Hi,Hi"),
                   selected="Lo,Lo")
})

output$fuelO <- renderUI({ selectInput(inputId="fuelI", label = h4("Fuel Price"), 
        choices = list("High Price" = "Hi", "Low Price" = "Lo"),
                   selected="Lo")
})

output$tmpO <- renderUI({actionButton("resetI", label = "Reset")}) 

###### Define Data ########### 

if(!is.null(input$resetI)){

chooseRJ <- reactive({ 

     chooseD <- expJ %>% filter(Scenario == input$scenarioI, Region %in% input$regionI, Price == input$fuelI)
     return(chooseD)
 })

sPlot1 <- reactive({

     chooseRJ %>% ggvis(~factor(Period),~Total,stroke=~Region) %>% layer_lines(strokeWidth:=2.5) %>% 
     add_axis("x",title="Analysis Period") %>% add_axis("y",title="Jobs") %>% scale_numeric("y",zero=TRUE) 
}) 

sPlot1 %>% bind_shiny("plot1") 


chooseRV <- reactive({ 

     chooseD <- expV %>% filter(Scenario == input$scenarioI, Region %in% input$regionI, Price == input$fuelI)
     return(chooseD)
})

sPlot2 <- reactive({

        chooseRV %>% ggvis(~factor(Period),~Total,stroke=~Region) %>% layer_lines(strokeWidth:=2.5) %>% 
        add_axis("x",title="Analysis Period") %>% add_axis("y",title="Gross Regional Product $M") %>% scale_numeric("y",zero=TRUE) 
})  

sPlot2 %>% bind_shiny("plot2")  

 } # Test ui controls exist 

}) # End Observeer

}) # End Shiny Server

Global.r

##### Play data ####################

options(stringsAsFactors = FALSE)

expJ <- data.frame(Region=rep(c("Western","Central","Southern","Northern","Capital","Hudson"),times=4,each=8),
                Scenario=rep(c("Lo,Lo","Lo,Hi","Hi,Lo","Hi,Hi"),each=48),
                Price=rep(c("Hi","Lo"),times=24,each=4),
                Period=rep(c(1,2,3,4),times=48),
                Total=rnorm(192,5,1.5))

expV <- data.frame(Region=rep(c("Western","Central","Southern","Northern","Capital","Hudson"),times=4,each=8),
                Scenario=rep(c("Lo,Lo","Lo,Hi","Hi,Lo","Hi,Hi"),each=48),
                Price=rep(c("Hi","Lo"),times=24,each=4),
                Period=rep(c(1,2,3,4),times=48),
                Total=rgamma(192,10,1.78))

0 个答案:

没有答案