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

Tho*_*wne 11 r shiny

只是玩Shiny并且已经爱上了它.但是,如何将reactivePlot/plotOutput组合中的图表设置为不同的大小,具体取决于绘制的图表?

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

在此输入图像描述

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

在此输入图像描述

如何根据选择的图表更改图表大小?我已经尝试将高度参数= 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"))
))
Run Code Online (Sandbox Code Playgroud)

这是我的服务器.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)
        )

    })


})
Run Code Online (Sandbox Code Playgroud)

Kev*_*vin 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.
Run Code Online (Sandbox Code Playgroud)

然而,到目前为止,我无法工作(与height="15in"...)...

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

编辑:它现在正在工作,height必须是数字,可选units="px",res当然可以转换units为像素.

编辑2:并且不要忘记更新Shiny [到最后版本],它修复了我面临的一些错误.

编辑3:这是一个动态改变高度的例子:

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

您可以将片段与此屏幕截图相关联,其中getNumberOfPlots返回要绘制的图形数量.

编辑4:如果你想显示几个图像,你也应该更改height'ui.R':这个值直接传输到CSS,默认值是400px.因此,如果您的图像更大,它们将重叠,只有400px可见...

plotOutput(outputId = "plot_rain", height="100%"))
Run Code Online (Sandbox Code Playgroud)