2012-12-09 39 views
11

只要玩Shiny并且已经爱上它了。但是,如何根据绘制的图表,将reactivePlot/plotOutput组合中的图表设为不同的大小?如何有条件地改变R的Shiny包装中图表的纵横比?

在该第一示例中,我已选择“产率曲线”分析,并获得纵横比我想:

enter image description here

但是,当我选择其他的分析,在这种情况下为热图,它是现在与扭曲它的“收益率曲线”图表相同(单元格应该是正方形,而不是矩形)。

enter image description here

如何变更图表的大小取决于该图已被选中?我试着把高度参数= NA,NULL或“”,但它不喜欢任何这些。

另外,但在同一个应用程序中,如何在sidebarPanel的顶部selectInput和textInputs之间获得一些空格?我试过h4(“”),但不起作用。

这里是我的ui.R:

library(shiny) 

shinyUI(pageWithSidebar(
    headerPanel(h1("SAGB Relative Value Tool")), 
    sidebarPanel(
     h4("Choose analysis:"), 
     selectInput("analysis1", "", 
      choices = c("Yield curve", "Optical asset swap spreads", 
         "Cheap dear box", "Cheap dear charts", "Switch signaliser", 
         "Barbells")), 
     h4(" "), 
     h4("Yield override:"), 
     lapply(bondNames, function(x) 
      textInput(paste(x, "bond"), x, last(sagb$sagb)[x])) 
    ), 
    mainPanel(
     h3(textOutput("AnalysisHeader")), 
     plotOutput("AnalysisOutput", height = "10in")) 
)) 

,这里是我的server.r

library(shiny) 

shinyServer(function(input, output) { 

    output$AnalysisHeader <- reactiveText(function() { 
     input$analysis1 
    }) 


    output$AnalysisOutput <- reactivePlot(function() { 
     switch(input$analysis1, 
      "Yield curve" = wo(whichOut = 1), 
      "Optical asset swap spreads" = wo(whichOut = 2), 
      "Cheap dear box" = wo(whichOut = 3), 
      "Cheap dear charts" = wo(whichOut = 4), 
      "Switch signaliser" = wo(whichOut = 5), 
      "Barbells" = wo(whichOut = 6) 
     ) 

    }) 


}) 
+0

什么是bondNames? – agstudy

+0

bondNames是调用runApp()的程序中的全局列表变量,其中包含南非政府债券(“SAGB”)市场中12个活跃债券的名称。在这里它用于创建边栏产量覆盖输入(虽然有点不稳定 - 我只是刚刚发现了numericInput,我应该使用for循环而不是lapply,而我的粘贴忽略了sep =“”参数)。无论如何不相关的问题。例如,wo()和sagb $ sagb数据结构也是包含代码的一部分。 –

+0

我第二个问题(如果Rstudio的人想知道有多少人对此功能感兴趣!),[我的情节目前无法阅读](http://i.stack.imgur.com/liY3O.png),但我不能静态设置高度,因为根据动态UI,绘图数量会有所不同... – Kevin

回答

9

(有时这是一个好主意,RTFM(说我的自我为好,比照我对OP的评论)

reactivePlot(func, width = "auto", height = "auto", ...) 

width The width of the rendered plot, in pixels; or ’auto’ to use the offsetWidth of 
     the HTML element that is bound to this plot. You can also pass in a function 
     that returns the width in pixels or ’auto’; in the body of the function you may 
     reference reactive values and functions. 
*height* The height of the rendered plot, in pixels; or ’auto’ to use the offsetHeight 
     of the HTML element that is bound to this plot. You can also pass in a function 
     that returns the width in pixels or ’auto’; in the body of the function you may 
     reference reactive values and functions. 
...  Arguments to be passed through to png. These can be used to set the width, 
     height, background color, etc. 

但是,到目前为止我无法设法使工作(与height="15in")...

Error in switch(units, `in` = res, cm = res/2.54, mm = res/25.4, px = 1) * : 
    non-numeric argument to binary operator 

编辑:它现在的工作,height必须是数字,可选units="px"res肯定是要units转换为像素。

编辑2:别忘了更新Shiny [到最新版本],它修复了我面对的一些错误。

EDIT 3:这里是一个例子,其中的高度被动态地改变:

getVarHeight <- function() { 
    return(getNumberOfPlots() * 400) 
} 
output$out <- reactivePlot(doPlots, height=getVarHeight) 

可以涉及代码段此screenshot,其中getNumberOfPlots返回图形的数量曲线图。

编辑4:如果你想显示几个图像,你应该改变height“ui.R”以及:这个值被直接传送到CSS,默认值是400px。所以如果你的图片比较大,它们会重叠,只有400px可见...

plotOutput(outputId = "plot_rain", height="100%")) 
+0

感谢Kevin - 我确实在RTFM中看到了这个;)但是我在plotOutput函数中放置了什么,它有一个默认值400px的高度。 –

+0

@Thomas Browne:我已经用示例更新了答案 – Kevin

+1

我相信你也可以将'height =“auto”'传递给'plotOutput'函数,它应该使plotOutput使用其内容的“自然”高度。 (请确保不要同时使用'height =“auto”'同时使用'reactivePlot'和'plotOutput',尽管:每个都将依赖于另一个来确定高度,我的猜测是最终会以0px高度。) –