tidyverse / dplyr

dplyr: A grammar of data manipulation
https://dplyr.tidyverse.org/
Other
4.75k stars 2.12k forks source link

This R function is purely indicative and should never be called. (dplyr::bind_rows) #6154

Closed Andryas closed 2 years ago

Andryas commented 2 years ago

Just to understand why this is happening. The problem is when I used dplyr::bind_rows I got an error but if I use rbind works well.

To see the error just uncomment the # aux$custo_dinamico_tb <- dplyr::bind_rows(aux$custo_dinamico_tb, tb) and comment aux$custo_dinamico_tb <- rbind(aux$custo_dinamico_tb, tb).

The error is as follows:

Warning: Error in : `vec_ptype2.integer.integer()` is implemented at C level.
This R function is purely indicative and should never be called.
  [No stack trace available]
library(shiny)
library(shinyWidgets)

ui <- shiny::shinyUI({
    shiny::fluidPage({
        shiny::fluidRow(
            shiny::column(
                width = 4,
                shiny::splitLayout(
                    cellArgs = list(style = "padding: 17px; white-space: normal;", align = "center"),
                    shiny::sliderInput("custo_taxa_administrativa_pct", "(%) da taxa administrativa", value = 0.06, min = 0, max = 1),
                    shiny::actionButton("custo_dinamico_bttn", "Adicionar custo", icon = shiny::icon("plus"), style = "margin-top: 36px;")
                ),

                htmltools::div(id = "custo_dinamico")
            )
        )
    })
})

server <- shiny::shinyServer(function(input, output, session) {
    # custo_dinamico_tb  <-  tibble::tibble()

    aux <- shiny::reactiveValues(custo_dinamico_tb = tibble::tibble())

    shiny::observeEvent(input$custo_dinamico_bttn, {
        tb_btn <- input$custo_dinamico_bttn
        tb_id <- paste0('custo_dinamico_', tb_btn)
        tb_txt <- paste0(tb_id, "_txt")
        tb_vl <- paste0(tb_id, "_vl")
        tb_rmv <- paste0(tb_id, "_rmv")
        tb <- tibble::tibble(
            btn = tb_btn,
            id = tb_id,
            txt = tb_txt,
            vl = tb_vl,
            rmv = tb_rmv
        )

        shiny::insertUI(
            selector = '#custo_dinamico',
            ui = tags$div(
                shiny::splitLayout(
                    cellArgs = list(style = "padding: 17px; white-space: normal;"),
                    cellWidths = c("60%", "20%", "20%"),
                    shiny::textInput(tb$txt, "", "", placeholder = "Nome do custo"),
                    shiny::numericInput(tb$vl, "", 0),
                    htmltools::div(style = "margin-top: 20px;", shinyWidgets::actionBttn(tb$rmv, "", shiny::icon("times"), color = "danger", style = "simple", block = TRUE))
                ),
                id = tb$id
            )
        )

        print(tb)
        aux$custo_dinamico_tb <- rbind(aux$custo_dinamico_tb, tb)
        # aux$custo_dinamico_tb <- dplyr::bind_rows(aux$custo_dinamico_tb, tb)
        print(aux$custo_dinamico_tb)

        # shiny::observeEvent(input[[tb_rmv]], {
        #     shiny::removeUI(selector = paste0("#", tb_id))
        #     aux$custo_dinamico_tb <- aux$custo_dinamico_tb[-which(aux$custo_dinamico_tb$btn == tb_btn), ]
        #     message("Remove: ", tb_btn, "\n")
        # })

    })
})

app <- shiny::shinyApp(ui, server)

shiny::runApp(app)
romainfrancois commented 2 years ago

@Andryas could you make the example simpler, e.g. outside of shiny ?

Andryas commented 2 years ago

No, I think that outsided the shiny everything will work as expect.

DavisVaughan commented 2 years ago

I see this if I print out the tb$btn

# [1] 1
# attr(,"class")
# [1] "integer"                "shinyActionButtonValue"

so I think the shiny team accidentally appended a class rather than prepended one.

I can reproduce with this minimal example:

library(vctrs)

x <- structure(1L, class = c("integer", "foo"))

vec_c(x, x)
#> Error: `vec_ptype2.integer.integer()` is implemented at C level.
#> This R function is purely indicative and should never be called.

Created on 2022-01-10 by the reprex package (v2.0.1)

romainfrancois commented 2 years ago

Probably here https://github.com/rstudio/shiny/blob/a8c14dab9623c984a66fcd4824d8d448afb151e7/R/server-input-handlers.R#L184 cc @wch

romainfrancois commented 2 years ago

fixed on the shiny side: https://github.com/rstudio/shiny/pull/3570