Search code examples
rplotlyverboselearnr

R: plot_ly() works only once each session before: "verbose must be TRUE or FALSE"


What I do

I'm translating "OpenIntro::Introduction to Modern Statistics Tutorials" into German. I use the downloaded .zip tutorials to do so: https://openintrostat.github.io/ims-tutorials/. The tutorials are learnr::tutorials written as .Rmd files. In tutorial 3 lesson 8 I'm facing an Error I'm unable to solve.

Problem

While doing the tutorial you face the plot_ly() function four times. But it only works one time as expected (in one session). Using the function a second, time an error message says: "verbose must be TRUE or FALSE". In the RStudio "Render"-pane: "Quitting from lines 11-44 [ex5-babies-plotly] (exercise.Rmd)". This points to the {r setup} but also makes me wonder because verbose = FALSE.

knitr::opts_chunk$set(fig.align = "center", 
                      fig.height = 3, 
                      fig.width = 5,
                      echo = FALSE, 
                      message = FALSE, 
                      warning = FALSE,
                      verbose = FALSE)

Things I tried an checked

Running plot_ly several times in RStudio is no problem.

Running plot_ly in the original on shinyapps several times works as well.

I tried knitr::opts_chunk$set(verbose = FALSE) as the first line in the chunk where the plot_ly()function is used. Didn't work!

Everything is up to date.

Questions

I don't get why it only works the first time. Do you have an explanation? Do you know a way to solve this problem?


Solution

  • I apologize for passing you bad information. I must have left a cache somewhere making me think that the solution was the package unloading when clearly it is not.

    However, it did tell me that something I did did actually fix it... since you messaged me I've been trying to figure out what I did that did work.

    The BLUF or more trendy TL;DR:

    Call library(data.table) when you initially call the necessary libraries in your setup chunk.

    Yes, that's it. (For real this time...)

    How did I get there?

    While Plotly apparently imports data.table. When Plotly imports it, neither the environments of RMD nor Shiny prerendered pull the default R options for data.table into your script.

    If you recall, I mentioned that I found a verbosity error having to do with data.table and I dismissed it.... in my original answer. .

    Before fixing the situation (without calling library(data.table)) & after allowing messages & warnings)

    In the viewer/browser, I rendered any two of the Plotly plots (how you directed me to recreate the error about verbosity).

    For example, chunk ex5-babies-plotly - 3D visualization, then chunk ex10-babies-plotly-two - Drawing parallel planes in 3D.

    enter image description here

    After attempting to render the second plot, so that the error occurs, I deleted plot_ly(...) and replaced it with rlang::last_trace() (If you don't delete plotly it errors again before giving you the output of last_trace)

    This allows us to see the rest of the error message...reading beyond that isn't necessary. (What last_trace returns starts with ..alloccol... -- what was discussed in that link I included in my original answer)

    enter image description here

    Once the error is present, run options() in your R console, then in any interactive exercise, you'll see several datatable options in the console and none in your RMD.

    For me, R returns 15.

    enter image description here

    (The code for that without the picture: length(options()[startsWith(names(options()), "datatable")]))

    The exercise returns zero.

    enter image description here

    After you add library(data.table) to your setup chunk and run the document, you can check the options again. You'll see it now matches the R console at 15.

    enter image description here



    Originally I wrote....(doesn't work)

    This does not address the actual problem, it's a workaround.

    The only corresponding information regarding a fix placed the blame on the package data.table. (You can read about that here.) Obviously, data.table isn't the issue. Unfortunately, I am not entirely sure what the problem is.

    The fix I offer is to unload the Plotly library between each call to plot. You can leave these chunks invisible, so the reader won't know they are there. I would encourage you to leave comments for yourself, so that you remember why these are here, though.

    Here is an excerpt from the English version, along with the fix I offer.

    ```{r unsetter, include=F}
    unloadNamespace("plotly")
    ```
    
    ```{r ex3-mariokart, exercise=TRUE}
    # load plotly package
    library(plotly) 
    
    # draw the 3D scatterplot
    p <- plot_ly(data = mariokart, z = ~ ____,x = ~____, y = ~____, opacity = 0.6) |>
      add_markers() 
      
    # draw the plane
    p |>
      add_surface(x = ~x, y = ~y, z = ~_____, showscale = FALSE)
    ```
    

    Notice that I've set include=FALSE so that it isn't visible to the user. Additionally, I didn't call the library after unloading it again since it's called in the code visible to the user.