0
votes

I've been making a Shiny app that creates both plots and tables that depend on some slider-based settings. Everything works ok, except that when the app is run, the plot doesn't appear until you move one of the sliders. This is the case on multiple browsers and computers.

Here is an example:

#ui.R
shinyUI(fluidPage(

  titlePanel("mtcars example"),

  sidebarLayout(
    sidebarPanel(
      sliderInput("mVal",
                  "Goal HP:",
                  min = 50,
                  max = 350,
                  value = 100)
    ),

    mainPanel(
      plotOutput("distPlot",width='1000px',height='600px'),
      uiOutput("view")
    )
  )
))

#server.R
library(ggplot2)
shinyServer(function(input, output,session) {

  output$distPlot <- renderPlot({
    MTtemp <- mtcars
    MTtemp$distToTarget <- (MTtemp$hp - input$mVal)^2

    p1<-ggplot(MTtemp,aes(x=hp,y=mpg,col=log(distToTarget)))+geom_point()+scale_colour_gradientn(colours=c('red','lightblue'))
    print(p1)

  })

  output$view <- renderUI({
    MTtemp <- mtcars
    MTtemp$distToTarget <- (MTtemp$hp - input$mVal)^2

    tM<-head(MTtemp[order(MTtemp$distToTarget),],n=10)

    M <- print(xtable(tM), floating=FALSE, tabular.environment="array", comment=FALSE, print.results=FALSE)
    html <- paste0("$$", M, "$$")
    list(
      withMathJax(HTML(html))
    )
  })      
})

However, if rather than using renderUI() and mathjax/html I just go with a renderTable() the plot appears straight away as I would expect. I.e., replacing the output$view above with:

  output$view <- renderTable({
    MTtemp <- mtcars
    MTtemp$distToTarget <- (MTtemp$hp - input$mVal)^2
    head(MTtemp[order(MTtemp$distToTarget),],n=10)
  })

If all else fails this is a workable solution, but I'd prefer to go with the prettier renderUI/mathjax table. So, I was hoping that someone could provide insight into the interaction between these elements that is causing the plot not to appear initially, or whatever other error I'm making to produce these behaviours.

Edit: It appears to be the mathjax that causes the issue. Ideally I'd like some way to keep the mathjax while seeing the plot appear.

1

1 Answers

0
votes

Returning html object worked for me, and it seems to be the same output:

  output$view <- renderUI({
    MTtemp <- mtcars
    MTtemp$distToTarget <- (MTtemp$hp - input$mVal)^2

    tM<-head(MTtemp[order(MTtemp$distToTarget),],n=10)

    M <- print(xtable(tM), floating=FALSE, tabular.environment="array", comment=FALSE, print.results=FALSE)
    html <- paste0("$$", M, "$$")
#     list(
#       withMathJax(HTML(html))
#     )
    return(html)
  })

EXAMPLE with renderTable

library(shiny)
library(ggplot2)

server <- shinyServer(function(input, output) {

  output$distPlot <- renderPlot({
    MTtemp <- mtcars
    MTtemp$distToTarget <- (MTtemp$hp - input$mVal)^2

    p1<-ggplot(MTtemp,aes(x=hp,y=mpg,col=log(distToTarget)))+geom_point()+scale_colour_gradientn(colours=c('red','lightblue'))
    print(p1)

  })

  output$view <- renderTable({
    MTtemp <- mtcars
    MTtemp$distToTarget <- (MTtemp$hp - input$mVal)^2

    tM<-head(MTtemp[order(MTtemp$distToTarget),],n=10)

    return(tM)

  }) 

})


ui <- fluidPage(
  titlePanel("mtcars example"),

  sidebarLayout(
    sidebarPanel(
      sliderInput("mVal",
                  "Goal HP:",
                  min = 50,
                  max = 350,
                  value = 100)
    ),

    mainPanel(
      plotOutput("distPlot",width='1000px',height='600px'),
      tableOutput("view")
    )
  )
)

shinyApp(ui = ui, server = server)