簡體   English   中英

閃亮:應用程序初始化后,renderPlot()不與withMathJax()一起出現嗎?

[英]Shiny: renderPlot() not appearing with withMathJax() upon app initialization?

我一直在制作一個Shiny應用程序,該應用程序創建基於某些基於滑塊的設置的繪圖和表格。 一切正常,除了運行該應用程序外,直到您移動其中一個滑塊,該圖才會出現。 在多個瀏覽器和計算機上就是這種情況。

這是一個例子:

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

但是,如果我不使用renderUI()和mathjax / html,而只是使用renderTable(),那么繪圖就會像我期望的那樣立刻出現。 即,將上面的output $ view替換為:

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

如果所有其他方法都失敗了,那么這是一個可行的解決方案,但是我更喜歡使用更漂亮的renderUI / mathjax表。 因此,我希望有人能夠洞悉這些元素之間的相互作用,從而導致該圖最初不出現,或者我為產生這些行為而犯任何其他錯誤。

編輯:這似乎是導致問題的mathjax。 理想情況下,我希望通過某種方式在看到情節出現時保持數學狀態。

返回html對象為我工作,它似乎是相同的輸出:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM