[英]Order of execution in ggvis and reactive slider input shiny
I create very simple shiny app with one reactive widgets sliderInput
. 我使用一个反应式小部件sliderInput
创建了一个非常简单的闪亮应用程序。 The solution allow me to manipulate a scale of y-axis. 该解决方案允许我操纵y轴的比例。 But when I set slider between 0 and 0.3 and then I change product eg P1->P3 the sliderInput
widget automatically comes back to the original range (ie 0 and 1). 但是,当我将滑块设置在0和0.3之间,然后更改乘积(例如P1-> P3)时, sliderInput
小部件会自动返回到原始范围(即0和1)。 App works fine, but I noticed that when I change a product with manipulated range, the first step is to draw a plot with manipulated y-axis and after that plot includes original range in y-axis. App运行正常,但是我注意到当我更改具有可控范围的产品时,第一步是绘制一个具有可控y轴的图,然后该图包括y轴中的原始范围。 It looks quite ugly especially in more complex app. 看起来非常难看,尤其是在更复杂的应用程序中。 How can I change the order of execution, ie the first would be changing range of y-axis and then generate a plot? 如何更改执行顺序,即首先要更改y轴范围,然后生成图?
global.R global.R
library(dplyr)
prod <- c('P1','P2','P3')
level <- c('L1','L2','L3')
part <- c('p1','p2','p3','p4','p5')
set.seed(123)
module1_df <- data.frame(prod = sample(prod,300, replace = T),
value = runif(300,0,0.3))
module1_df <- as.data.frame(module1_df %>%
group_by(prod) %>%
mutate(id = 1:n()) %>%
arrange(prod, id))
app.R app.R
library(shiny)
library(ggvis)
ui <- navbarPage(
title = '',
tabPanel("Module 1",
selectInput('prod', '', prod),
uiOutput('in_value'),
ggvisOutput('plot_show')
)
)
)
server <- (function(input, output) {
data_0 <- reactive({
df <- module1_df %>%
filter(prod == input$prod)
})
output$in_value <- renderUI({
df2 <- data_0()
var <- 'value'
min_value <- min(df2[,var])
sliderInput('value','',min = round(min_value,0),
max = 1, value = c(0,1), step = 0.1)
})
data <- reactive({
df3 <- data_0()
if (!is.null(input$value)) {
df3 <- df3 %>%
filter(df3[['value']] >= input$value[1] &
df3[['value']] <= input$value[2])
}
return(df3)
})
plot <- reactive({
withProgress('', value = 0, {
y_min <- input$value[1]
y_max <- input$value[2]
plot <- data() %>%
ggvis(~id, ~value) %>%
layer_points() %>%
scale_numeric('y',domain = c(y_min, y_max))
Sys.sleep(0.5)
})
return(plot)
})
plot %>% bind_shiny('plot_show')
})
shinyApp(ui = ui, server = server)
Placing an invalidateLater
for 1 second in your reactive plot command will make the renderer wait just enough for the slider to reinitialize. 在反应式绘图命令中将invalidateLater
放置1秒钟,将使渲染器等待刚好足以使滑块重新初始化。 I also changed the Sys.sleep
to a much shorter time, so the re-plot delay is almost imperceptible 我也将Sys.sleep
更改为更短的时间,因此重新绘制延迟几乎不可察觉
# note the added 'session' argument
server <- function(input, output, session) {
data_0 <- reactive({
df <- module1_df %>%
filter(prod == input$prod)
})
output$in_value <- renderUI({
df2 <- data_0()
var <- 'value'
min_value <- min(df2[,var])
sliderInput('value','', min = round(min_value ,0),
max = 1, value = c(0, 1), step = 0.1)
})
data <- reactive({
df3 <- data_0()
if (!is.null(input$value)) {
df3 <- df3 %>%
filter(df3[['value']] >= input$value[1] &
df3[['value']] <= input$value[2])
}
return(df3)
})
plot <- reactive({
invalidateLater(1000, session)
withProgress('', value = 0, {
y_min <- input$value[1]
y_max <- input$value[2]
plot <- isolate(data()) %>%
ggvis(~id, ~value) %>%
layer_points() %>%
scale_numeric('y', domain = c(y_min, y_max))
Sys.sleep(0.01)
})
return(plot)
})
plot %>% bind_shiny('plot_show')
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.