2014-12-19 80 views
3

我正在处理一个Shiny应用程序。其中一部分涉及使用ggplot2生成一个图。有相当多的变量,所以我希望它能够非常广泛地滚动以便可读。最初我试图用plotOutput("plot1", width = X)(其中x是某个数字)来调整尺寸,如果我确切知道我的plot1将会是什么样子,我可以使其工作。事情是,最终会有过滤器会影响plot1有多大,所以我不能预先定义它。闪亮/ ggplot2中的变量大小

我想要做的是以某种方式告诉R说:“用ggplot2制作一个条形图,其中条形图全部为x个单位,并使图形尽可能大。”

这里是server.r产生积代码:

output$plot1 <- renderPlot({ 
ggplot(data = dat, aes(x = foo)) + geom_bar(stat="bin") 
+facet_grid(~bar, scales = "free_x", space = "free") 

,然后在ui.r我把它像我上面那样:

plotOutput("plot1", width = X) 

具体发生了什么事是, 'facet_grid'将剧情分成不同的组,用户可以选择决定哪些组/多少组。

我想到的一种可能的解决方法将涉及以某种方式尝试计算将出现的小节数量,然后提出函数来计算X有多大。在一个常规的R会话中,这将非常容易,但在Shiny中,我需要能够在server.r中计算一个变量并在ui.r中使用它,并且我对如何实现这一点感到困惑。

编辑:我可能已经找出了一个解决方法,而不是做一个情节,将剧情转换为图像并显示图像。现在,我只需要弄清楚如何使图像成为全尺寸,而不是将它缩放以适应窗口。任何意见,将不胜感激。

回答

4

我能够通过使用renderUI来做到这一点。像在server.r以下

output$newUI <- renderUI({ 
    output$plot1 <- renderPlot({ 
    ggplot(data = dat, aes(x = foo)) + geom_bar(stat="bin") 
    +facet_grid(~bar, scales = "free_x", space = "free") 
    }) 
plotOutput(plot1, width = X) 
}) 

(其中X是数据的一些功能),并在ui.r

mainPanel(
    uiOutput("testUI") 
) 

编辑:上面的代码是一个懒散的努力得到工作没有真正重现我的完整代码。下面是更完整的代码,相关代码:

在server.ui

output$ui <- renderUI({ 
    ... 
    ##here, I did a query based on parameters passed in from the 
    ##shiny ui, so the data was variable. 
    ##so I do some processing of the data, and then a function on the 
    ##resulting data so I know how much to scale my plot 
    ##(something like nn = nrow(data)) 
    ##call that scaling factor "nn" 
    .... 
    output$plot1 <- renderPlot({ 
     ggplot(...) 
     )} 
    plotOutput("plot1", width = 30*nn, height = 500) ## nn is my scaling factor 
)} 

而且在ui.R

ShinyUI(fluidPage(
    ..., 
    mainPanel(
     uiOutput("ui") 
    ) 
) 
+0

我得到的错误:错误标记(“div”,列表(...)):找不到对象'plot1'。另外,当你引用ui.R中的变量“testUI”时,你的代码至少有一个错误,并且你在server.R中使用newUI。你能确认你的代码是正确的吗?我也尝试访问plotOutput代码上的输出$ plot1,但后来我得到错误:不允许从shinyoutput对象的eading对象。在此先感谢 – Picarus 2015-05-01 04:13:30

+0

是的,我的代码工作,但它有点屠杀,因为我刚刚输入一些骨架代码,以节省时间,并没有打扰仔细检查的东西名称。我编辑了我的答案,所以你可以看到正确的代码(无论如何,相关的东西) – goodtimeslim 2015-05-01 05:37:49

+0

谢谢。我猜报价在哪里... – Picarus 2015-05-01 05:40:06

0

我用图表不GGPLOT2。

对于那些谁与googleVis缩放问题来到这里,我想和大家分享的是,而不是设置说width = 200以像素为单位,你可以用这个词“自动”它可以扩展很好,并没有在文档中明确。从我的其中一个功能片段:

# where plotdt has my data with columns px and py 

plot1 <- gvisBarChart(plotdt, 
         xvar = px, 
         yvar = c(py), 
         options = list(width = "automatic", 
             height = "automatic") 

希望这可以帮助别人。