DS4PS / cpp-526-spr-2020

Course shell for CPP 526 Foundations of Data Science I for Spring 2020.
http://ds4ps.org/cpp-526-spr-2020/
3 stars 0 forks source link

Lab 04 - Dynamic Selection #18

Open MCaldarulo opened 4 years ago

MCaldarulo commented 4 years ago

Hi! I have some doubts regarding lab 4. Specifically, I was wondering how I can make the trend line dynamic depending on the team chosen by the user. Indeed, right now the trend line appears only for the "default team". Right now I am using the functions below reported for creating the trend line and the selection option

selectInput( inputId = "Team Selection",
             label = "Select a Team",
             choices = c(sort(unique(Teams$name))),
             selected = "Chicago White Sox"
           )
Teams$ave.so <- Teams$SO / Teams$G
Dynamic <- filter(Teams, name == "Chicago White Sox")
points(
  x = Dynamic$yearID,
  y = Dynamic$ave.so,
  type = "b",
  pch = 19,
  col = "darkorange4"
)

Thanks a lot in advance!!

lecy commented 4 years ago

@MCaldarulo This is the somewhat tricky part of Shiny. It is very similar to when you are first learning math and you are feeling pretty good about understanding addition, subtraction, multiplication and division, and then you start algebra where you replace a concrete number with the placeholder X and it turns your world upside down.

The primary insight with Shiny is that you need widgets to allow users to specify their input, and then you need to collect the user input from the widget and use it to alter your graphics.

Using the example from the demo: [SHINY DEMO[(https://cdn.rawgit.com/DS4PS/Data-Science-Class/53c986f1/TEMPLATES/ShinyWidgetsDemo.Rmd)

Always start by hard-coding one case so that you can make sure your code for the graphic is working (can you select a specific team then add the line for that team). For the stock case we have stocks A though E, so a hard-coded example would look like this:

# highlight one organization
this.one <- "E"

# select the data:
these.points <- dat[ , this.one ]

points( these.points, type="l", col="darkred" )
text( 1000, these.points[1000], label=this.one, pos=4, col="darkred" )

image

Now that you have a specific case working, you need to integrate user choice. Recall that the user selections will always live in the Shiny object input$widget.name where you define the widget name.

You can name your widget anything, so input$widget.name will depend entirely on what name you use for the widget object when you define it:

selectInput( inputId='org2', 
            label='Select Organization', 
            choices=names(dat),
            selected="A"
          )

Then just replace the specific case with the more general user-selected option.

# highlight one organization
# this.one <- "E"           # specific case
this.one <- input$org2      # general case

# select the data:
these.points <- dat[ , this.one ]   # data step is a little different on the lab

points( these.points, type="l", col="darkred" )
text( 1000, these.points[1000], label=this.one, pos=4, col="darkred" )

Does that make sense?

For the lab the widget allows the user to select the team name, so the data step would be something like:

one.team <- filter( dat, TeamName == "Arizona Cactus" )   # specific
one.team <- filter( dat, TeamName == input$team.name.widget )  # general

Note, I'm making up variable names and team names here.

MCaldarulo commented 4 years ago

Got it, Thanks!