[英]Plotly disappears when using jQuery in R Shiny
我正在使用jQuery(jQuery Core 2.1.4)来开发我的Shiny应用程序的一部分。 另外,我正在使用新的plotly
包来渲染图。 jQuery的完美的作品对自己和plotly
过; 然而,当同时使用,从图中plotly
dissapear。 调用jQuery似乎是原因。
是否有一个解决方案允许在同一个Shiny App中使用jQuery和
plotly
(特别是ggplotly
)?
这是一个例子。 我知道这个例子不需要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))
谢谢!
解决方案:您需要使用JQuery的noconflict() 。 在加载jquery的行之后添加tags$head(tags$script("$.noConflict(true);")),
。
说明:Shiny默认已经加载了JQuery。 你可以通过查看任何Shiny应用程序页面的来源来看到这一点,你会发现它加载了jquery。 当我运行你的应用程序并查看JavaScript控制台的错误时,我得到了一些奇怪的错误,这些错误表明JQuery的某些功能无法正常工作。 所以我认为“嗯jquery肯定是加载的。它实际上加载了两次。但是当我加载它时它不起作用。让我们Google吧!”。 因此,如果你谷歌如何在一个页面上加载jquery两次,你会看到许多答案说使用noconflict()。 在你的jquery行之后添加它似乎可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.