DS4PS / cpp-526-sum-2020

Course shell for CPP 526 Foundations of Data Science I for Summer 2020.
http://ds4ps.org/cpp-526-sum-2020/
MIT License
2 stars 1 forks source link

Lab 04 - Errors when trying to create dynamic legend #15

Open pharri14 opened 4 years ago

pharri14 commented 4 years ago

Every time I try to run the document it keeps giving me the following error:

ERROR: Operation not allowed without an active reactive context

I went back to my code and saw that it was saying that Error: object input not found when I ran the following code:

# Reactive Object / Legend
one_team <- filter( .data = Teams, 
                    name == input$my_team )
points( x = one_team$yearID, 
        y = one_team$ave.so, 
        type = "b", 
        pch = 19, 
        col = "darkorange4" )

I am confused as to what to assign for input. In the demo examples, it states: When using Shiny, a global object called "input" will be created, and it stores the input values the user defines in slots created through the inputId arguments.

I did call out this by doing the following

selectInput( inputId = "my_team",
             label = "Choose a Team",
             choices = c("Cincinnati Reds", 
                         "Pitsburgh Pirates", 
                         "Philidelphia Phillies", 
                         "St. Louis Cardinals", 
                         "Chicago White Sox", 
                         "Detroit Tigers", 
                         "Chicago Cubs", 
                         "Boston Red Sox", 
                         "New York Yaknees", 
                         "Cleveland Indians", 
                         "Baltimore Orioles", 
                         "Washington Senators", 
                         "New York Giants", 
                         "Philidelphia Athletics", 
                         "St. Louis Browns", 
                         "Los Angeles Dodgers", 
                         "San Fransisco Giants", 
                         "Minnesota Twins", 
                         "New York Mets", 
                         "Houston Astros", 
                         "Atlanta Braves", 
                         "Milwaukee Brewers", 
                         "Oakland Athletics", 
                         "Kansas City Royals", 
                         "San Diego Padres"),
             selected = "Los Angeles Dodgers" )

Does anyone know how to work around this?

jamisoncrawford commented 4 years ago

Hi @pharri14.

You're call to selectInput() looks fine to me, so long as it's in Column {.sidebar} using flexdashboard per the examples.

You're also using input$my_team correctly, so none of that needs adjustment. The error:

ERROR: Operation not allowed without an active reactive context

...typically indicates that the location where you use input$my_team is not within a reactive function. It can't simply be within a code chunk. Try putting it inside renderPlot({}), within both parentheses and curly brackets. Your call to filter() should precede any plotting functions. For example:

renderPlot({

one_team <- filter(.data = Teams, 
                   name == input$my_team)

plot.new()

# Your plot code with reactive value "one_team"

})
pharri14 commented 4 years ago

@jamisoncrawford thank you for that tip. I was able to get the error from popping up and to at least show the chart. However, it still says that Error: object input not found inside my Console. Do I need to create a new Shiny Web Application, like was illustrated in the Week 4: How does "Shiny" work? video?

jamisoncrawford commented 4 years ago

@pharri14 the template should work fine unless you've made any alterations. This is a difficult error to diagnose without seeing the code.

Would you email me your .rmd file?

jamisoncrawford commented 4 years ago

@pharri14 thanks for sending the .rmd file - posting here for others.

Any operation with the reactive variable input$my_team should occur within renderPlot({}) first, so you'll want to assign the output of filter() to one_team in order to prepare that for later use in the code.

Importantly, plot.new() should be the next step, then any code for the main plot and, lastly, you'll want to include the dynamic part of your plotting code (the code that uses the filtered data one_team).

After switching the order of these around, it functioned as intended - so I do hope that works for you. I should note that I did not get any errors when I ran it initially, so hopefully you're not experiencing that, either. Let me know if this works (and, if so, congrats on your first dashboard)!

pharri14 commented 4 years ago

@jamisoncrawford I made sure to put one_team inside of renderPlot ({}). When I run the one_team input line (as shown below) it produces the following error:

one_team <- filter( .data = Teams, name == input$my_team )

Error in input$my_team : $ operator is invalid for atomic vectors

one_team still shows up in my Data Environment. I went back and watched the Lab 04 Part II video and saw that when you ran that line of code, no error occurred in your Console. For reference, here is a snippet of my code to show the order:

renderPlot({
  one_team <- filter( .data = Teams, 
                      name == input$my_team )
  plot.new()
  plot.window( xlim = c(1900, 2012),
               ylim = c(0, 10),
               yaxs = "i" )
  par( mar = c(2, 2, 2, 4),
      mgp = c(2, 0.2, 0.2) )...................
jamisoncrawford commented 4 years ago

@pharri14 are you knitting the file or running the code chunks individually?

Are you able to open the dashboard?

pharri14 commented 4 years ago

@jamisoncrawford the entire program will run and the dashboard opens. But, when I try to run that code chunk itself, it gives me that error.

jamisoncrawford commented 4 years ago

@pharri14 that makes sense. The individual code chunks should throw an error if they contain input$my_team.

This is because input is not made into an object in your working environment using assignment (<-). It's only made into a reactive object when it can work in concert with the code chunk where inputId = specifies it - and for that to happen, the document must be knit in its entirety.

So in order to check the result of changes in code, you should knit the entire document, but don't worry about individual chunks. If you shuffle around the code you have in your renderPlot() call and knit the whole dashboard each time, you will see the real changes you're making, and all the code you need is already in your script.

Sorry for prematurely celebrating - does this help?

pharri14 commented 4 years ago

@jamisoncrawford that makes sense. However, no matter where I move the following code chunk around and run the document, no lines show up on the graph when a team is selected: points( x = one_team$yearID, y = one_team$ave.so, type = "b", pch = 16, col = "darkorange4" )

lines( x = one_team$yearID, y = one_team$ave.so, col = "darkorange4", lwd = 2 )

Am I messing something up with the order of other chunks of the code, maybe? Right now I have it ordered as:

I have also tried to place the dynamic plotting function right before the section where I establish the X and Y-axis without success.

jamisoncrawford commented 4 years ago

@pharri14 perhaps par() should come before plot.new() since it establishes graphical parameters. This is the damnedest thing - would you mind resending your .rmd script?

We had an issue that neither Dr. Lecy nor I could figure out for the life of us on this particular assignment last semester. It turned out that argument inputId = had a lowercase "i" and since R is case sensitive, it didn't run. Took us a hot minute to find that. It might be something small like that.

jamisoncrawford commented 4 years ago

@pharri14 I have one more suggestion. Try creating a new script (Ctrl + N), then copy and paste your Lahman data and any other manipulation, then the code to create your plot, including the part that is supposed to be dynamic (the orange line for ave.so for a single team).

Only this time, don't actually use input$my_team. Instead, simply replace input$my_team with the name of any of the teams. Does the orange line appear for my_team/one_team?

This way, you can test the plot itself and determine if the error lies within your plot code, or if it is an issue with the reactive variable code.

jamisoncrawford commented 4 years ago

@pharri14 any news?

pharri14 commented 4 years ago

@jamisoncrawford I figured out what I was doing wrong. For one, instead of listing 10-25 teams in the {.sidebar} chunk, I had made my code include every single team. This was causing issues because I had my timeframe set to start at 1900 and some of the teams were outside of those bounds. So, I switched it to having 25 teams with large datasets within that time frame (see below).


selectInput( inputId = "my_team", # creates slot in input to hold values: input$your_inputId
             label = "Choose a Team", # title of the input widget
             choices = c("Cincinnati Reds", 
                         "Pitsburgh Pirates", 
                         "Philidelphia Phillies", 
                         "St. Louis Cardinals", 
                         "Chicago White Sox", 
                         "Detroit Tigers", 
                         "Chicago Cubs", 
                         "Boston Red Sox", 
                         "New York Yaknees", 
                         "Cleveland Indians", 
                         "Baltimore Orioles", 
                         "Washington Senators", 
                         "New York Giants", 
                         "Philidelphia Athletics", 
                         "St. Louis Browns", 
                         "Los Angeles Dodgers", 
                         "San Fransisco Giants", 
                         "Minnesota Twins", 
                         "New York Mets", 
                         "Houston Astros", 
                         "Atlanta Braves", 
                         "Milwaukee Brewers", 
                         "Oakland Athletics", 
                         "Kansas City Royals", 
                         "San Diego Padres"), # options for the user, all team names
             selected = "Los Angeles Dodgers" ) # sets the default option

Additionally, I organized my main chunk as follows:

I am not sure if it helped or if I'm tricking myself, but when my code was not reacting properly when run, I took your advice and copied it into a new script, ran that, and then re-ran my original script, and it worked.

Thanks for helping me figure out the issue

jamisoncrawford commented 4 years ago

@pharri14 fantastic work troubleshooting this whole thing! Thanks for your patience with my instructions. Looking forward to seeing your first interactive data product! 🔥 🔥 🔥