Skip to content

Extra ggplot2 lines trip up grade_this_code() #231

Open

Description

@garrettgman

Adding an extra call to a ggplot() call results in an obscure code suggestion: I expected ggplot() where you called +. For example, this learnr exercise

### 

__Edit your code from the previous exercise so that `marital` appears in order of increasing frequency.__ Use a forcats function to change the order.

```{r fct_rev, exercise =  TRUE}
gss_cat %>%
  ggplot(aes(marital)) + 
  geom_bar()
```

```{r fct_rev-solution}
gss_cat %>%
  ggplot(aes(fct_rev(fct_infreq(marital)))) + 
  geom_bar()
```

```{r fct_rev-check}
grade_this_code()
```

Returns the message below when a student supplies the code

gss_cat %>%
  ggplot(aes(fct_infreq(marital))) + 
  scale_x_discrete(limits = rev) +
  geom_bar()

Screen Shot 2021-03-05 at 10 38 57 AM

Since adding an extra line to a ggplot() call is a common failure mode for ggplot2, we should find a way to provide a better code suggestion.

One idea: perhaps the ggcheck package can come with a ggplot2 specific version of grade_this_code()that can begin by assuming it is working with ggplot2 code, e.g. grade_this_ggplot_code()

Activity

  1. gadenbuie commented on Mar 5, 2021

    @gadenbuie
    Member

    I think we should be able to fix this in gradethis. The underlying problem is that we don't do a good job of handling the case when there are more operations than expected:

    # Extra addition
    code_feedback("a + b + c", "a + c")
    #> I didn't expect `+` where you wrote `a + b + c`.
    
    # Extra addition, plus pipe (smaller reprex of the ggplot example)
    code_feedback("x %>% a() + b + c", "a(x) + c")
    #> In `x %>% a() + b + c`, I expected you to call `a()` where you called `+`.
    
    # Extra pipes
    code_feedback("x %>% a() %>% b() %>% c()", "x %>% a() %>% c()")
    #> In `x %>% a() %>% b() %>% c()`, I expected you to call `a()` where you called `b()`.

    We have an open issue for the extra pipe case: #185

  2. added
    grade code
    on Mar 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    grade code

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions