Rob Creel
Rob Creel

Reputation: 343

R/Shiny error: non-numeric argument to binary operator

I'm learning Shiny and I am trying to write an app to explore Simpson's paradox graphically. I built the plot in R first, with customizable options set a the beginning. The plot shows data that are positively correlated but that can be factored into groups which are negatively correlated. The options at the beginning include how many groups to have, how they're spaced, whether to show the grouped/ungrouped regression lines, etc. See the working simpson.R script here.

The intent of the app is to get the user to play with these options to make the groupings more or less obvious and to highlight (or not) the negative correlations.

The complete code for my app files is on github. I don't have sufficient reputation to post more than two links, so please find ui.R and server.R there in the repo.

The error I can't seem to get past is non-numeric argument to binary operator apparently somewhere in this line:

anchors_x <- rep(seq(2, 2 + (input$number_of_groups - 1) * input$separation, 
                             by = input$separation)

and also

p <- ggplot(data = dfserver(), aes(x, y))

Specific question:

Why isn't R treating these variables as numeric?

More generally:

Was it smart of me to build the plot in a plain R script first? Or would a more typical workflow be just to build the thing in Shiny from the beginning? If so, how does one debug/build R inside of Shiny if one can't get into the environment to poke around at the objects? Or is there a way I don't know to access objects that live inside ui.R and server.R?

Upvotes: 1

Views: 1539

Answers (1)

Dieter Menne
Dieter Menne

Reputation: 10215

input$number_of_groups and friends is a string.

Try

    ng = as.integer(input$number_of_groups)
    sep = as.integer(input$separation)
    ppg = as.integer(input$points_per_group)
    anchors_x <- rep(seq(2, 2 + (ng - 1) * sep, by = sep), ppg)

and similar further below

Upvotes: 2

Related Questions