Search code examples
rshinycrud

crudtable crashes when starting from rstudio, Error in if: the condition has length > 1


I am trying to run this package https://github.com/beerda/crudtable. The package works via live demo link https://beerda.shinyapps.io/crudtable/ on that site.

enter image description here Here is the code:

library(shiny)
library(crudtable)

# Data Access Object from the CO2 data frame
dao <- dataFrameDao(CO2)

# User Interface
ui <- fluidPage(
  crudTableUI('crud')
)

# Server-side
server <- function(input, output, session) {
  crudTableServer('crud', dao)
}

# Run the shiny app
shinyApp(ui = ui, server = server)

But when I try to start it from my RStudio R Version 4.2 The program starts but crashes after clicking the + New Record button with the Error: Warning: Error in if: the condition has length > 1 1: runApp enter image description here

What does this error stand for and how can I solve it?


Solution

  • After some debugging I could track down the bug. The simpleFormUIFactory is faulty. There we test for the type of an attribute like so:

    if (a$type == "factor") {
       # ...
    }
    

    The problem is that with the example dataset the class of column Plant is

    class(CO2$Plant)
    # [1] "ordered" "factor" 
    

    i.e. it contains 2 elements. With R 4.2.0 the behaviour of using if with conditions of length greater then 1 changed to throw an error instead of a warning (cf. to the corrsponding section in the NEWS for R 4.2.0.), i.e. something like

    if (1 == 1:2) 1
    

    throws an error in R >= 4.2.0 and was a warning before. (This explains why it works for the demo but not for you).

    Having said that, until this bug is patched, you have basically 2 options:

    1. Provide your own edit form dialog to avoid the call of simpleFormUIFactory in the first place. (cf. to the help of crudTableServer for an example)
    2. Make sure all column types in your data frame have exactly one class. Notably transform ordered to normal factor:
    CO2$Plant <- factor(CO2$Plant, ordered = FALSE)
    

    P.S.: The package author should replace == in the if clause by a %in%. I will post a comment in your issue post.