我一直在做一个闪亮的应用程序,它可以创建依赖于一些滑块设置的图表和表格。一切正常,除了当应用程序运行时,只有当你移动其中一个滑块时,绘图才会出现。这是在多个浏览器和计算机上的情况。
下面是一个示例:
#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替换为:
  output$view <- renderTable({
    MTtemp <- mtcars
    MTtemp$distToTarget <- (MTtemp$hp - input$mVal)^2
    head(MTtemp[order(MTtemp$distToTarget),],n=10)
  })如果所有其他方法都失败了,这是一个可行的解决方案,但我更喜欢使用更漂亮的renderUI/mathjax表。因此,我希望有人能提供关于这些元素之间的相互作用的洞察力,这些元素是导致情节最初不出现的原因,或者是我为了产生这些行为而犯的其他错误。
编辑:似乎是mathjax导致了这个问题。理想情况下,我想要一些方法来保持mathjax,同时看到情节出现。
发布于 2015-08-05 16:34:15
返回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)https://stackoverflow.com/questions/31823501
复制相似问题