【发布时间】:2021-12-22 09:38:08
【问题描述】:
我正在使用拉丝直方图来查询闪亮应用程序中的样本。在我的完整应用程序中,我覆盖了一个新的直方图,该直方图突出显示了选定的区域,并更新了一个显示过滤样本属性的 DT 数据表。
我注意到每次移动时都会调用两次依赖于画笔的反应。例如,每次刷直方图时,下面的 table_data 反应式会被调用两次。
app.R
library(ggplot2)
library(shiny)
df <- data.frame(x = rnorm(1000))
base_histogram <- ggplot(df, aes(x)) +
geom_histogram(bins = 30)
# Define UI for application that draws a histogram
ui <- fluidPage(
column(
plotOutput("histogram", brush = brushOpts(direction = "x", id = "brush", delay=500, delayType = "debounce")),
width = 6
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$histogram <- renderPlot({
p <- base_histogram
current <- table_data()
if (nrow(current) > 0) {
p <- p + geom_histogram(data = current, fill = "red", bins = 30)
}
p
})
table_data <- reactive({
print("called")
brushedPoints(df, input$brush)
})
}
# Run the application
shinyApp(ui = ui, server = server)
在这个玩具示例中,它几乎不引人注意。但是在我的完整应用程序中,必须在 table_data 响应式中进行大量计算,而这种双重调用会不必要地减慢一切。
有没有什么方法可以构建应用程序,以便在画笔结束时只执行一次反应?
这是一个 GIF,显示每次画笔都会执行两次 table_data。
【问题讨论】:
标签: r ggplot2 shiny reactive brush