Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example app for freezeReactiveValues can emit error messages #2463

Open
wch opened this issue May 30, 2019 · 3 comments
Open

Example app for freezeReactiveValues can emit error messages #2463

wch opened this issue May 30, 2019 · 3 comments

Comments

@wch
Copy link
Collaborator

wch commented May 30, 2019

The purpose of the example app for freezeReactiveValues is to demonstrate how to suppress error messages. However, it can still emit error messages in some cases.

This is a slightly modified version of the example app (I added some message() calls):

ui <- fluidPage(
  selectInput("data", "Data Set", c("mtcars", "pressure")),
  checkboxGroupInput("cols", "Columns (select 2)", character(0)),
  plotOutput("plot")
)

server <- function(input, output, session) {
  observe({
    message("observe: ", input$data)
    data <- get(input$data)
    # Sets a flag on input$cols to essentially do req(FALSE) if input$cols
    # is accessed. Without this, an error will momentarily show whenever a
    # new data set is selected.
    freezeReactiveValue(input, "cols")
    updateCheckboxGroupInput(session, "cols", choices = names(data))
  })

  output$plot <- renderPlot({
    message("renderPlot: ", input$data)
    # When a new data set is selected, input$cols will have been invalidated
    # above, and this will essentially do the same as req(FALSE), causing
    # this observer to stop and raise a silent exception.
    cols <- input$cols
    data <- get(input$data)

    if (length(cols) == 2) {
      plot(data[[ cols[1] ]], data[[ cols[2] ]])
    }
  })
}

shinyApp(ui, server)

If you check two of the columns so that a plot shows up, and then select the other data set, sometimes a red error message shows up briefly, and this appears in the console:

renderPlot: mtcars
renderPlot: mtcars
renderPlot: mtcars
renderPlot: pressure
Warning in min(x) : no non-missing arguments to min; returning Inf
Warning in max(x) : no non-missing arguments to max; returning -Inf
Warning in min(x) : no non-missing arguments to min; returning Inf
Warning in max(x) : no non-missing arguments to max; returning -Inf
Warning: Error in plot.window: need finite 'xlim' values
  [No stack trace available]
observe: pressure

The error occurs because the renderPlot is executing before the observer when the data set is changed. I can't get this to happen consistently, though. It happens the first time when I run the app in a new R session, but not after that. This is with Shiny 1.3.2.

However, with the fastmap PR (#2429), I can get the error message every time, not just the first time.

@wch
Copy link
Collaborator Author

wch commented May 30, 2019

This appears to be due an issue with the order that reactives fire in Shiny. See #2429 (comment)

@wch
Copy link
Collaborator Author

wch commented May 31, 2019

Closed in favor of #2466.

@wch wch closed this as completed May 31, 2019
@jcheng5
Copy link
Member

jcheng5 commented Sep 18, 2020

This is real, and addressed by #3055. Specifically, calling freezeReactiveValue later in the same flush cycle as an output that already used that input, is not ideal, but we also don't want the error to make it to the client. We should encourage people to set the freezeReactiveValue's parent observer to a higher priority, but suppress the error from the UI in either case.

@jcheng5 jcheng5 reopened this Sep 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants