Custom reactive() function doesn't work in Shiny above 0.13.0

Hello.
In our shiny applications (shiny version 0.12.2), we use the redefinition of the reactive() function in order not to prescribe the withProgress() function in each reactive. Here's the function:

reactive <- function(x) {
   func <- shiny::exprToFunction(x)
   shiny::reactive({
      withProgress(message = 'Загрузка данных', value = 1, detail = '', {
         func();
      })
   })
}

However, due to changes made in version 0.13.1, this technique does not work on newer versions of shiny. As I understand it, a reactive expression passed to another reactive expression is perceived as a function. Next, this function is not in the environment and we get an error that this function does not exist.

Is it possible in new versions of shiny to do something similar, so as not to write withProgress() in every reactive()?

Here is an example of an application that runs on version 0.13.0 and does not run on version 1.4.0.

library(shiny)
library(dplyr)
library(rlang)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot")
        )
    )
)
reactive <- function(x) {
    func <- shiny::exprToFunction(x)
    shiny::reactive({
        withProgress(message = 'Загрузка данных', value = 1, detail = '', {
            func();
        })
    })
}

# Define server logic required to draw a histogram
server <- function(input, output) {
    
    react1 <- reactive({
        data <- mtcars
        
        data <- data %>%
            group_by(cyl, gear) %>%
            transmute(wt = mean(wt)) %>%
            ungroup() %>%
            unique()
    })
    
    react2 <- reactive({
        Sys.sleep(3)
        data <- react1()
        
        data <- data %>%
            group_by(cyl) %>%
            transmute(wt = mean(wt)) %>%
            ungroup() %>%
            unique()
        
        data_output <- faithful[, 2]
    })

    output$distPlot <- renderPlot({
        x <- react2()
        # # generate bins based on input$bins from ui.R
        # x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)
        
        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

You can install version 0.13.0 of shiny by running the following code:

packageurl <- "https://cran.r-project.org/src/contrib/Archive/shiny/shiny_0.13.0.tar.gz"
install.packages(packageurl, repos=NULL, type="source")

Thanks.

in your preferred approach, by what mechanism would progess be set or incremented ? setProgress() /incProgress()

Hi @Gers1972. You can remove the line exprToFunction(x), then the function can work.

library(shiny)
library(dplyr)
library(rlang)

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("Old Faithful Geyser Data"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
)
reactive <- function(x) {
  shiny::reactive({
    withProgress(x, message = 'Загрузка данных', value = 1, detail = '')
  })
}

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  react1 <- reactive({
    data <- mtcars
    
    data <- data %>%
      group_by(cyl, gear) %>%
      transmute(wt = mean(wt)) %>%
      ungroup() %>%
      unique()
  })
  
  react2 <- reactive({
    Sys.sleep(3)
    data <- react1()
    
    data <- data %>%
      group_by(cyl) %>%
      transmute(wt = mean(wt)) %>%
      ungroup() %>%
      unique()
    
    data_output <- faithful[, 2]
  })
  
  output$distPlot <- renderPlot({
    x <- react2()
    # # generate bins based on input$bins from ui.R
    # x    <- faithful[, 2]
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    
    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'darkgray', border = 'white')
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

good shout raytong.
Seems to me that without incrementing or setting of the progress bar, this requirement is equivalent to a withSpinner of shinybusy type approach

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.