Error in vapply ... values must be length 1, but FUN(X[[11]]) result is length 0
Solution 1
I don't have the reputation to add a comment, so will provide my 2 cents as an answer though it doesn't actually answer the question.
I got a similar error, and I suspect it is related to list columns in your data. Here is a reproducible example:
```{r}
library (dplyr)
library (purrr)
# create data
mydata <-
tibble(col_a = rep(c("a", "b"), 5)) %>%
mutate(col_b = map(col_a, function (x) { list(a = x, b = x, c = x) }))
# filter
mydata %>% filter (col_a == "a")
```
The last line of code performs as expected when I copy/paste it into the console. However, when I runt it from the R-markdown document (using the 'Ctrl+Enter'-shortcut in R-studio) it produces the error:
Error in vapply(x, obj_sum, character(1L)) : values must be length 1,
but FUN(X[[1]]) result is length 3
The final "length"-part of the error message depends on the length of the list in col_b
.
Don't know what to make of this. A work around may be to reformat any list columns?
Solution 2
Your rmarkdown script may contain in-line code that cannot be coerced into a string, before or after your code chunks.
For example, `make_a_graph()` embedded in the text body of the rmarkdown like this:
Next, we want to
make_a_graph()
with our data.
I solved a similar problem in my script by converting in-line code to chunks one-by-one and the problem went away. Found the tip here.
Solution 3
I do not have sufficient reputation to comment, but I also found that an inline
r list(*data_list*)
snippet in my R notebook was the culprit for this error. Inserting this segment into a chunk was the solution.
Related videos on Youtube
Nevo
Updated on September 14, 2022Comments
-
Nevo over 1 year
I've never been very skilled with R and am coming back after an absence so I'm re-learning a lot. I've got a dataset (named data) that has fields latitude and longitude. Some of the observations have '0' in these fields, which is invalid data. I'm writing an R notebook to document my findings.
I have:
Let's start by finding out how many records have 0 for latitude and longitude. If it's a great deal of records, we might not be able to rely on these fields: ```{r} nrow(filter(data, latitude == 0.0)) nrow(filter(data, longitude == 0.0)) ``` Okay, there are 12 rows that have 0 for latitude and 12 rows that have 0 for longitude. I'm willing to bet these are the same rows. Let's find out. ```{r} filter(data, latitude == 0.0) ```
The first two lines that start with nrow() both display the output I expect:
[1] 12 [1] 12
However, the same filter statement, which I expect to output the 12 rows that match the filter criteria, gives me an error when I run the chunk:
Error in vapply(x, obj_sum, character(1L)) : values must be length 1, but FUN(X[[11]]) result is length 0
I don't understand why I'm getting this error. The 'data' variable is a tibble, if that makes a difference.
I'd sure appreciate an explanation of what's happening here.
-
Matt Tyers about 7 yearsWhen you knit/etc, think of it as a fresh R environment being created - you'll need to create/load (in a code chunk) whatever isn't there - and
data
might be part of that.
-
-
Rahul over 6 yearsI'm facing the exact issue today. Works in the console, but not in rmarkdown. What gives?
-
phili_b about 5 yearsSame problem but you point the cause : Jupyter. Everything working with Jupyter works on RStudio....except some cases. It works on RStudio but it doesn't work on Jupyter.
-
Matt about 4 yearsI wanted to add that I had in-line code that was printing the output for a shapefile that couldn't be coerced into a string. I didn't receive the error once I fixed those sections of in-line code.