cssearcy / AYS-R-Coding-SPR-2020

Coding in R for Policy Analytics
https://cssearcy.github.io/AYS-R-Coding-SPR-2020/
3 stars 3 forks source link

Lab 4 - "object 'input' not found" #16

Open SalvadorW2 opened 4 years ago

SalvadorW2 commented 4 years ago

Hello everyone,

I'm trying to get the following chunk of code to work:

dat.one.team <- filter( .data = Teams, name == input$my_team )

points( x=dat.one.team$yearID, y=dat.one.team$ave.so, pch=20, cex=1.2, col="goldenrod1", type="b" )

team.name <- input$my_team

However, I keep getting the following error message:

Error: Problem with filter() input ..1. x object 'input' not found i Input ..1 is name == input$my_team.

I've installed Shiny and believe I have configured selectInput correctly:

selectInput(

inputId = "my_team", label = "Select a team:", choices = c("New York Yankees", "Boston Red Sox", "Oakland Athletics", "Atlanta Braves", "Toronto Blue Jays", "Los Angeles Dodgers", "Houston Astros", "Chicago Cubs", "New York Mets", "Arizona Diamonbacks"), selected = "Atlanta Braves"

Does anyone know what the problem could be?

jamisoncrawford commented 4 years ago

Hm... if object input isn't found, there could be a few reasons for that. Are you using input inside a reactive expression, i.e. render.*({ ... }) ? Have your preceded your code with both library(shiny) and library(dplyr)?

SalvadorW2 commented 4 years ago

Yes, I've done all of those things.

I checked with the library() function and shiny and dplyr are both installed.

I made sure to call them from the library before attempting to run the code.

jamisoncrawford commented 4 years ago

And they're explicitly in the beginning of your shiny script? The calls to library()?

BriannaSmithR commented 4 years ago

I'm having the same problem. My code is

team.name <- input$my_team  

dat.one.team <- filter(Teams, 
                       name == input$my_team)

When I run either line, it says "Error: object 'input' not found."

I've also tried

dat.one.team <- filter(Teams, 
                       name == input$my_team)

because I thought that after running my inital code, team.name and input$my_team would be the same, but that code returns "object team.name not found"

I have all the packages loaded, and these are above my renderPlot.

jamisoncrawford commented 4 years ago

@BriannaSmithR that would likely be the culprit:

I have all the packages loaded, and these are above my renderPlot.

input$ is used in reactive expressions and therefore needs to be in a reactive function like renderPlot(). Try putting it at the very beginning, before your plotting functions, while plotting functions with dat.one.team should be at the very end of renderPlot().

The { } signals to R that something dynamic and responsive to user inputs is occurring within the curly brackets.

BriannaSmithR commented 4 years ago

I moved

team.name <- input$my_team  

into the renderPlot(), but I'm still getting the "Error: object input no found." I also moved my other code and it isn't working either :(

jamisoncrawford commented 4 years ago

@BriannaSmithR would you kindly email me your .Rmd file and I can have a look?

SalvadorW2 commented 4 years ago

I've tried what you've said and am having the same problem as Brianna.

Could I do the same?

jamisoncrawford commented 4 years ago

@SalvadorW2 absolutely!

jamisoncrawford commented 4 years ago

@SalvadorW2 so the .Rmd script runs perfectly fine and I'm not able to reproduce this error. Couple of notes though:

  1. Only call data(Teams) once, at the beginning - by calling it again after several preprocessing tasks, you restore the Teams dataset to its original state
  2. Your reactive plotting function should go at the very end of the renderPlot() call:
points( x=dat.one.team$yearID, 
        y=dat.one.team$ave.so, 
        pch=20,
        cex=1.2,
        col="goldenrod1",
        type="b" )

It should be the final layer of paint on your visualization, since it's the reactive portion!

Last note is a biggie and so I'm putting it in bold so others will see it:

Everyone:

Running the individual code chunks will throw an error - you need to knit the document in its entirety, since the chunks must communicate with each other simultaneously - running an individual chunk is like seeing if a car will budge when you've taken off half the wheels!

Let me know if this helps!

SalvadorW2 commented 4 years ago

Okay, thank you!

This is helpful.

jamisoncrawford commented 4 years ago

Sure thing, @SalvadorW2!

SalvadorW2 commented 4 years ago

Thank you,

I have the reactive element working now for the interactive graph.

Sean Waggoner

jamisoncrawford commented 4 years ago

Great! :fire: