[英]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.