首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >闪亮:在应用程序初始化时,renderPlot()没有与withMathJax()一起出现?

闪亮:在应用程序初始化时,renderPlot()没有与withMathJax()一起出现?
EN

Stack Overflow用户
提问于 2015-08-05 12:31:58
回答 1查看 289关注 0票数 0

我一直在做一个闪亮的应用程序,它可以创建依赖于一些滑块设置的图表和表格。一切正常,除了当应用程序运行时,只有当你移动其中一个滑块时,绘图才会出现。这是在多个浏览器和计算机上的情况。

下面是一个示例:

代码语言:javascript
运行
复制
#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(),那么绘图就会像我预期的那样立即出现。即,将上面的输出$view替换为:

代码语言:javascript
运行
复制
  output$view <- renderTable({
    MTtemp <- mtcars
    MTtemp$distToTarget <- (MTtemp$hp - input$mVal)^2
    head(MTtemp[order(MTtemp$distToTarget),],n=10)
  })

如果所有其他方法都失败了,这是一个可行的解决方案,但我更喜欢使用更漂亮的renderUI/mathjax表。因此,我希望有人能提供关于这些元素之间的相互作用的洞察力,这些元素是导致情节最初不出现的原因,或者是我为了产生这些行为而犯的其他错误。

编辑:似乎是mathjax导致了这个问题。理想情况下,我想要一些方法来保持mathjax,同时看到情节出现。

EN

回答 1

Stack Overflow用户

发布于 2015-08-05 16:34:15

返回html对象对我来说是有效的,而且它似乎是相同的输出:

代码语言:javascript
运行
复制
  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的示例

代码语言:javascript
运行
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31823501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档