简体   繁体   English

在R Shiny中使用jQuery时,Plotly消失了

[英]Plotly disappears when using jQuery in R Shiny

I am using jQuery (jQuery Core 2.1.4) to develop part of my Shiny app. 我正在使用jQuery(jQuery Core 2.1.4)来开发我的Shiny应用程序的一部分。 In addition, I am using the new plotly package to render the plots. 另外,我正在使用新的plotly包来渲染图。 jQuery works perfectly on its own and plotly too; jQuery的完美的作品对自己和plotly过; however when using both, the plots from plotly dissapear. 然而,当同时使用,从图中plotly dissapear。 Calling jQuery seems to be the cause. 调用jQuery似乎是原因。

Is there a work-around that allows to use jQuery and plotly (specifically ggplotly ) in the same Shiny App? 是否有一个解决方案允许在同一个Shiny App中使用jQuery和plotly (特别是ggplotly )?

Here is an example. 这是一个例子。 I am aware this example does not need the jQuery but is only to illustrate how plotly plots dissapear when including jQuery (uncomment the line #tags$head(tags$script(src='http://code.jquery.com/jquery-2.1.4.js')), to include it): 我知道这个例子不需要jQuery,但只是为了说明在包含jQuery时如何plotly情节情节(取消注释行plotly #tags$head(tags$script(src='http://code.jquery.com/jquery-2.1.4.js')),包括它):

#Call the required libraries
library(shiny)
library(plotly)
library(ggplot2)

#Server to output plot
server <- shinyServer(function(input, output) {

  output$plotlyout <- renderPlotly({

    #Create random points
    x <- rnorm(100)
    y <- rnorm(100)

    #Set to data frame
    dats <- as.data.frame(cbind(x,y))

    #Plot them in plotly
    ggplotly(ggplot(dats) + geom_point(aes(x = x, y = y)))

  })

})

#Shiny plot
ui <- shinyUI(

  #One page with main Panel
  fluidPage(

      #Uncomment this to see how Jquery ruins everything
      #tags$head(tags$script(src='http://code.jquery.com/jquery-2.1.4.js')),

      #Panel for plot
      mainPanel(

          #Output plot
          plotlyOutput("plotlyout")
      )
  )
)

shinyApp(ui = ui, server = server, options = list(launch.browser = TRUE))

Thanks! 谢谢!

Solution: You need to use JQuery's noconflict() . 解决方案:您需要使用JQuery的noconflict() Add tags$head(tags$script("$.noConflict(true);")), after the line loading jquery. 在加载jquery的行之后添加tags$head(tags$script("$.noConflict(true);")),

Explanation: JQuery already gets loaded by default by Shiny. 说明:Shiny默认已经加载了JQuery。 You can see this by viewing the source of the page of any Shiny app, and you'll see that it loads jquery. 你可以通过查看任何Shiny应用程序页面的来源来看到这一点,你会发现它加载了jquery。 When I ran your app and looked at the JavaScript console for errors, I was getting strange errors that suggested that something with JQuery isn't working right. 当我运行你的应用程序并查看JavaScript控制台的错误时,我得到了一些奇怪的错误,这些错误表明JQuery的某些功能无法正常工作。 So I thought "hmm jquery is definitely loaded. It's actually loaded twice. But it's not working when I load it twice. Let's Google that!". 所以我认为“嗯jquery肯定是加载的。它实际上加载了两次。但是当我加载它时它不起作用。让我们Google吧!”。 So if you Google for how to load jquery twice on one page, you'll see lots of answers saying to use noconflict(). 因此,如果你谷歌如何在一个页面上加载jquery两次,你会看到许多答案说使用noconflict()。 Adding it after your line of jquery seems to do the trick. 在你的jquery行之后添加它似乎可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM