0
votes

Consider the following small piece of R code:

A = tibble(animal = 'cat',color = 'black',weight = 1000)

A[1,] <-   gsub("a","e",A[1,])

What I want is a one-row tibble with entries 'cet', 'bleck' and 1000, but trying to run this with tibble 3.0.3 loaded gives an error:

Error: Assigned data `gsub("a", "e", A[1, ])` must be compatible with row subscript `1`.

x 1 row must be assigned.

x Assigned data has 3 rows.

ℹ Row updates require a list value. Do you need `list()` or `as.list()`?*

...which I guess makes sense, as the gsub results in a length 3 character vector.

Here's my question: This piece of code is a small part of a R Shiny app I have running on a company server. Yesterday, after an admin installed the 'photobiology' package on the server, our app started to crash; digging into the code, the lines above turned out to be the cause of the problem. So, before installing some new packages, the code above was executed as we intended it, after installing the new package (with dependencies), we got errors. Rewriting the code is not a big issue, but I'd like to understand what went wrong, and I'm not too familiar with tibbles. Can it be that this type of row replacement was possible in an earlier version of the tibble package, but not in the most recent version?

1

1 Answers

0
votes

When you are updating a row of a tibble it expects you to pass

  • a list or a dataframe/tibble
  • they should have same column names as the original tibble (A)
  • they should have the same class as the original tibble (A)

This is to ensure data uniformity is maintained and wrong values are not updated.

Combine all this and you can do :

tmp <- as.data.frame(t(gsub("a","e",A[1,])))
tmp <- type.convert(tmp, as.is = TRUE)
names(tmp) <- names(A)
A[1,]  <- tmp
A

# A tibble: 1 x 3
#  animal color weight
#  <chr>  <chr>  <dbl>
#1 cet    bleck   1000