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

时间:2022-08-27 11:03:14

Just playing with Shiny and loving it already. But how do I get charts in the reactivePlot / plotOutput combination to be of different sizes depending on which chart is plotted?

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

In this first example, I have selected the "Yield curve" analysis and get the aspect ratio I want:

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

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

But when I choose another analysis, in this case a heatmap, it is now the same size as the "Yield Curve" chart which distorts it (cells should be square, not rectangular).

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

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

How can I change the chart size depending on which chart has been selected? I've tried putting the height parameter = NA, NULL or "" but it doesn't like any of those.

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

Separately, but in the same application, how can I get some whitespace between the top selectInput and the textInputs in the sidebarPanel? I have tried h4(" ") but doesn't work.

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

Here is my ui.R:

这是我的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"))
))

and here is my server.r

这是我的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 个解决方案

#1


9  

(sometimes it's a good idea to RTFM (talking to my self as well, cf my comment to OP)

(有时候对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.

however, so far I couldn't manage to make to work (with height="15in")...

然而,到目前为止,我无法设法工作(身高=“15英寸”)......

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

EDIT: it's now working, height has to be numeric, with optional units="px" and res certainly something to convert units to pixels.

编辑:它现在正在工作,高度必须是数字,可选单位=“px”和res当然可以将单位转换为像素。

EDIT 2: and don't forget to update Shiny [to the last version], it fixes some bugs I faced.

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

EDIT 3: here is an example where the height is dynamically changed:

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

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

You can relate the snippet to this screenshot, where getNumberOfPlots returns the number of graph to plot.

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

EDIT 4: if you want to display several images, you should change the height in 'ui.R' as well: this value is directly transmitted to CSS, and the default value is 400px. So if your images are bigger, they will overlap and only the to 400px will be visible ...

编辑4:如果要显示多个图像,您还应该更改'ui.R'中的高度:此值直接传输到CSS,默认值为400px。因此,如果您的图像更大,它们将重叠,只有400px可见...

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

#1


9  

(sometimes it's a good idea to RTFM (talking to my self as well, cf my comment to OP)

(有时候对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.

however, so far I couldn't manage to make to work (with height="15in")...

然而,到目前为止,我无法设法工作(身高=“15英寸”)......

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

EDIT: it's now working, height has to be numeric, with optional units="px" and res certainly something to convert units to pixels.

编辑:它现在正在工作,高度必须是数字,可选单位=“px”和res当然可以将单位转换为像素。

EDIT 2: and don't forget to update Shiny [to the last version], it fixes some bugs I faced.

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

EDIT 3: here is an example where the height is dynamically changed:

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

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

You can relate the snippet to this screenshot, where getNumberOfPlots returns the number of graph to plot.

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

EDIT 4: if you want to display several images, you should change the height in 'ui.R' as well: this value is directly transmitted to CSS, and the default value is 400px. So if your images are bigger, they will overlap and only the to 400px will be visible ...

编辑4:如果要显示多个图像,您还应该更改'ui.R'中的高度:此值直接传输到CSS,默认值为400px。因此,如果您的图像更大,它们将重叠,只有400px可见...

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