Misleading warning in RStudio - {your_variable_name} is defined after it is used

I have a column name (dataset) that happens to have the same name as a new variable I define later on. Should I just use different variable names from column names or can this warning be improved?

image

Warning below (or whatever the exclamation point is supposed to be called :slight_smile: )

image

I tried making a simple replication of this and didn't get the warning. Can you make a reproducible example to demonstrate?

Sure, try this in an Rmarkdown document:

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
library(tidyverse)
```

```{r}
df <- mtcars %>%
  rename(dataset = mpg) %>%
  select(dataset)
```

```{r}
dataset <- "test"
```

Each block is of course a separate Rmarkdown chunk.

Ok, I see. First, the option that turns this warning on or off is under Tools > Global Options as shown here (and indeed described a bit different from the actual warning):

warn_opt

Second, to get the mtcars example to exhibit this behavior, you need to remove the rename line:

df <- mtcars %>%
  select(dataset)

Of course this means that dataset would now not actually be in your data.frame, but that does trigger the warning you describe. There may be something going wrong with the bit of code in lines ~31-33 in your original example?

Thanks for pointing me to the settings to turn off these warnings. I think I'm either going to do that or just simply ignore the warnings.

Perhaps it has something to do with me using a newer build of RStudio, but I still get the same behavior regardless if I use select only. Here is another example:

I also hypothesized that it might have to do the variable not being in the same chunk, but still the same warning:

image

Anyways, not a big deal now that I know how to turn these off.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.