Skip to content

Instantly share code, notes, and snippets.

@celiomsj
Created May 22, 2020 13:08
Show Gist options
  • Save celiomsj/2c339e8f8dfa81172d9147470f1aebed to your computer and use it in GitHub Desktop.
Save celiomsj/2c339e8f8dfa81172d9147470f1aebed to your computer and use it in GitHub Desktop.
Exemplo de atualização de gráfico do Plotly no shiny.
library(shiny)
library(dplyr)
library(plotly)
ui <- fluidPage(
mainPanel(
tags$style(".recalculating {opacity: 1.0}"), # Comente esta linha para ver o comportamento normal do shiny
plotly::plotlyOutput("grafico")
)
)
server <- function(input, output, session) {
output$grafico <- plotly::renderPlotly({
data() %>%
plot_ly(x = ~Sepal.Length, y = ~Sepal.Width, type = "scatter", mode = "markers") %>%
layout(
images = list(
list(source = "https://raw.githubusercontent.com/rstudio/shiny/master/man/figures/logo.png",
xref = "x", yref = "y", x = 5, y = 4, sizex = 1, sizey = 1, opacity = 0.8)
)
)
})
data <- reactive({
invalidateLater(3000, session)
Sys.sleep(1)
iris %>%
sample_n(30)
})
}
shinyApp(ui, server)
library(shiny)
library(dplyr)
library(plotly)
ui <- fluidPage(
mainPanel(
# tags$style(".recalculating {opacity: 1.0;}"),
plotly::plotlyOutput("grafico")
)
)
server <- function(input, output, session) {
output$grafico <- plotly::renderPlotly({
iris %>% sample_n(30) %>% # dados iniciais
plot_ly(x = ~Sepal.Length, y = ~Sepal.Width, type = "scatter", mode = "markers") %>%
layout(
images = list(
list(source = "https://raw.githubusercontent.com/rstudio/shiny/master/man/figures/logo.png",
xref = "x", yref = "y", x = 5, y = 4, sizex = 1, sizey = 1, opacity = 0.8)
)
)
})
observe({
x = data() %>% pull(Sepal.Length)
y = data() %>% pull(Sepal.Width)
plotlyProxy("grafico", session) %>%
plotlyProxyInvoke("deleteTraces", list(as.integer(0))) %>%
plotlyProxyInvoke("addTraces", list(
x = x, y = y, type = "scatter", mode = "markers"
))
})
data <- reactive({
invalidateLater(3000, session)
Sys.sleep(1)
iris %>%
sample_n(30)
})
}
shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment