Note that there are some explanatory texts on larger screens.

plurals
  1. POR Shiny rCharts Morris - rChart is not displaying (under particular circumstances)
    primarykey
    data
    text
    <p>I came across a problem while using <code>rCharts</code> in my <code>Shiny</code> application. I generate dynamically 1-3 <code>tabPanels</code> (depending on the user selection) and in each of them one plot is rendered. Plot may be of two types: simple (with <em>graphics</em> libary) or rCharts (with <strong><em>morris</em> library</strong>). The whole <code>tabsetPanel</code> is rendered within the <code>uiOutput</code> component, each time an user changes his input. The type of the <code>ui output</code> (<code>plotOutput</code> for simple plot or <code>showOutput</code> for rCharts) is defined when a <code>tabPanel</code> is rendered, so the plot <strong>does</strong> have a <em>proper</em> environment. </p> <p>And the <strong>problem</strong>: simple plots (with graphics, ggplot2 etc.) do work OK - they are displayed correctly in a <code>tabPanel</code>. However, when I work with application and have 2 or 3 rCharts to be displayed, it happens that <strong>one chart is not displaying</strong> - almost at all (see the images below). Of course, such a problem does not appear with simple plots. </p> <p>I tried to have the output size fixed, size flexible, but the problem still exists. I have varsions of R and the libraries as follows: </p> <pre><code>&gt; R.Version()$version.string [1] "R version 3.0.1 (2013-05-16)" &gt; packageVersion("Shiny") [1] ‘0.7.0’ &gt; packageVersion("rCharts") [1] ‘0.3.51’ </code></pre> <p>Thank you a lot for any suggestions. </p> <p><strong>rCharts working OK:</strong></p> <p><img src="https://i.stack.imgur.com/B0bEm.png" alt="enter image description here"></p> <p><strong>rCharts FAIL to be dispkayed ok:</strong></p> <p><img src="https://i.stack.imgur.com/BRvFX.png" alt="enter image description here"></p> <p><strong>EDIT:</strong> my code below:</p> <p><strong>UI</strong></p> <pre><code>library(shiny) library(Epi) shinyUI(pageWithSidebar( headerPanel("Header"), sidebarPanel( checkboxInput(inputId = "checkboxInputx", label = "function: x", value = TRUE), checkboxInput(inputId = "checkboxInputxpower2", label = "function: x^2", value = FALSE), checkboxInput(inputId = "checkboxInput2x", label = "function: 2x", value = FALSE), actionButton("gobutton","GO!") ), mainPanel( radioButtons("plottypechoice", "Choose plot type", c("simple", "rCharts")), uiOutput("plotpanelcontent") ) )) </code></pre> <p><strong>SERVER</strong></p> <pre><code>library(shiny) library(rCharts) library(Epi) library(reshape2) # build data frame x &lt;- 1:100 df &lt;- data.frame(x, x^2, 2*x) names(df) &lt;- c("x", "xpower2", "2productx") shinyServer(function(input, output) { # generate tabsetPanel with tabPlots with plot of selected type output$plotpanelcontent &lt;- renderUI({ if(input$gobutton != 0){ # collect tab names tab.names &lt;- vector() if(input$checkboxInputx) tab.names &lt;- c(tab.names, "x") if(input$checkboxInputxpower2) tab.names &lt;- c(tab.names, "xpower2") if(input$checkboxInput2x) tab.names &lt;- c(tab.names, "2productx") print(tab.names) # render tabs tabs &lt;- lapply(tab.names, function(tab.name){ # define tabPanel content depending on plot type selection if(input$plottypechoice == "simple") tab &lt;- tabPanel(tab.name, plotOutput(paste0("simpleplot", tab.name))) else tab &lt;- tabPanel(tab.name, showOutput(paste0("rchartplot", tab.name), "morris")) return(tab) }) return(do.call(tabsetPanel, tabs)) } }) # Render simple plots output$simpleplotx &lt;- renderPlot({ print(plot(df[,1], df[,1])) plot(df[,1], df[,1]) }) output$simpleplotxpower2 &lt;- renderPlot({ print(plot(df[,1], df[,2])) plot(df[,1], df[,2]) }) output$simpleplot2productx &lt;- renderPlot({ print(plot(df[,1], df[,3])) plot(df[,1], df[,3]) }) # Render rCharts output$rchartplotx &lt;- renderChart({ plot &lt;- mPlot(x="x", y="x", type = "Line", data = df) plot$set(dom = "rchartplotx") return(plot) }) output$rchartplotxpower2 &lt;- renderChart({ plot &lt;- mPlot(x="x", y="xpower2", type = "Line", data = df) plot$set(dom = "rchartplotxpower2") return(plot) }) output$rchartplot2productx &lt;- renderChart({ plot &lt;- mPlot(x="x", y="2productx", type = "Line", data = df) plot$set(dom = "rchartplot2productx") return(plot) }) }) </code></pre> <p><strong>UPDATE:</strong></p> <p>I have asked <a href="http://www.oesmith.co.uk/" rel="nofollow noreferrer">Olly Smith</a>, an author of morris.js library, to suggest a solution and I received a following response:</p> <p><em>Morris can't correctly draw charts when they're not displayed on screen. When using Morris in tabbed applications, you need to redraw the active chart whenever the tab selection changes. There's a redraw() method on the chart objects that each of the Morris.Line/Bar/Donut constructors return that you can use to do this. Unfortunately, it's currently undocumented, but it's a stable part of the API now so it's safe to use.</em></p> <p><strong>UPDATE 2. :</strong></p> <p>I followed the <strong>Ramnath</strong> suggestions and update both <code>Shiny</code> and <code>rCharts</code> libraries version:</p> <pre><code>&gt; packageVersion("Shiny") [1] ‘0.8.0’ &gt; packageVersion("rCharts") [1] ‘0.3.53’ </code></pre> <p>and run the code just after restarting the R session. Unfortunately, plots seem to behave in even stranger way now. After doing actions in the following order: </p> <ol> <li><p>set: "function: x", "rCharts", GO [OK], </p></li> <li><p>add: "function: x^2" [see "not_ok_1" image attached below in the post],</p></li> <li><p>add: "function: 2x" [see "not_ok_2" image attached below in the post].</p></li> </ol> <p>I received the plot visualizations as follows:</p> <ol> <li><p><strong>OK</strong> <img src="https://i.stack.imgur.com/cIpjZ.png" alt="enter image description here"></p></li> <li><p><strong>"not_ok_1" image</strong> <img src="https://i.stack.imgur.com/FgScM.png" alt="enter image description here"></p></li> <li><p><strong>"not_ok_2" image</strong> <img src="https://i.stack.imgur.com/MtTEh.png" alt="enter image description here"></p></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload