r - 如何有条件地更改 R Shiny 包中图表的纵横比?

标签 r shiny

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

在第一个示例中,我选择了“产量曲线”分析并获得了我想要的纵横比:

enter image description here

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

enter image description here

如何根据所选图表更改图表大小?我尝试将高度参数设置为 NA、NULL 或“”,但它不喜欢其中任何一个。

单独地,但在同一个应用程序中,如何在侧边栏面板中的顶部 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"))
))

这是我的服务器

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)
        )

    })


})

最佳答案

(有时,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 当然可以将单位转换为像素。

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

编辑3:这是一个动态更改高度的示例:

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

您可以将代码片段与此 screenshot 相关联,其中 getNumberOfPlots 返回要绘制的图形数量。

编辑 4:如果您想显示多个图像,您还应该更改“ui.R”中的高度:这value直接传给CSS,默认值为400px。因此,如果您的图像较大,它们将重叠,并且只有 400 像素可见......

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

关于r - 如何有条件地更改 R Shiny 包中图表的纵横比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13792195/

相关文章:

html - 是否可以在 Shiny downloadHandler 生成的报告中使用 HTML

python - 为什么来自字典的数据帧在 R 中的行为与通过网状结构的独立数据帧不同?

R矩阵求和列向量

javascript - 在不更改标记大小的情况下,使用 plotly proxy 在 plotly 中更改跟踪标记的颜色

r - 使用 Dplyr 使用另一列中的相应值填充空单元格

r - 热图ggplot中瓷砖之间的空白

在 Shiny 应用程序中,rhandsontable 下拉菜单缩短了

format - 更改 Shiny 中文本输出的格式

r - 表或数据框上的复选框

r - 您能否建议如何管理 Shiny 服务器上托管的应用程序的包依赖关系?