如何有条件地改变R闪亮包装中图表的纵横比?

时间:2012-12-09 21:47:08

标签: r shiny

只是玩Shiny并且已经爱好了。但是,如何根据绘制的图表,将reactivePlot / plotOutput组合中的图表设置为不同的大小?

在第一个例子中,我选择了“收益率曲线”分析并获得了我想要的宽高比:

enter image description here

但是当我选择另一个分析时,在这种情况下是一个热图,它现在与“屈服曲线”图表的大小相同,它会扭曲它(单元格应该是正方形,而不是矩形)。

enter image description here

如何根据选择的图表更改图表大小?我已经尝试将高度参数= NA,NULL或“”但它不喜欢任何这些。

另外,但在同一个应用程序中,如何在sidebarPanel中的顶部selectInput和textInput之间获得一些空格?我试过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)
        )

    })


})

1 个答案:

答案 0 :(得分:10)

(有时对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肯定要转换{{1}到像素。

编辑2 :不要忘记更新Shiny [到最新版本],它修复了我遇到的一些错误。

编辑3 :以下是高度动态更改的示例:

units

您可以将代码段与此screenshot相关联,其中getVarHeight <- function() { return(getNumberOfPlots() * 400) } output$out <- reactivePlot(doPlots, height=getVarHeight) 返回要绘制的图表数量。

编辑4 :如果您想要显示多个图片,则应更改&{39> ui.R&#39;中的getNumberOfPlots。同样:此值直接传输到CSS,默认值为height。因此,如果您的图像更大,它们将重叠,只有400px可见...

400px