2012-12-09 46 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高度。) –