0

I have a selectInput to select different dataframes using a switch statement.

My "players" dataset looks like this:

  Player Pos Age Team  G GS  MPG  FG FGA   FG%  3P 3PA   3P%  2P 2PA   2P%  eFG%  FT FTA   FT% ORB DRB TRB AST STL BLK
    1  Álex Abrines  SG  24  OKC 75  8 15.1 1.5 3.9 0.395 1.1 2.9 0.380 0.4 0.9 0.443 0.540 0.5 0.6 0.848 0.3 1.2 1.5 0.4 0.5 0.1
    2    Quincy Acy  PF  27  BRK 70  8 19.4 1.9 5.2 0.356 1.5 4.2 0.349 0.4 1.0 0.384 0.496 0.7 0.9 0.817 0.6 3.1 3.7 0.8 0.5 0.4
    3  Steven Adams   C  24  OKC 76 76 32.7 5.9 9.4 0.629 0.0 0.0 0.000 5.9 9.3 0.631 0.629 2.1 3.8 0.559 5.1 4.0 9.0 1.2 1.2 1.0
    4   Bam Adebayo   C  20  MIA 69 19 19.8 2.5 4.9 0.512 0.0 0.1 0.000 2.5 4.8 0.523 0.512 1.9 2.6 0.721 1.7 3.8 5.5 1.5 0.5 0.6
    5 Arron Afflalo  SG  32  ORL 53  3 12.9 1.2 3.1 0.401 0.5 1.3 0.386 0.7 1.7 0.413 0.485 0.4 0.5 0.846 0.1 1.2 1.2 0.6 0.1 0.2
    6  Cole Aldrich   C  29  MIN 21  0  2.3 0.2 0.7 0.333 0.0 0.0    NA 0.2 0.7 0.333 0.333 0.1 0.3 0.333 0.1 0.6 0.7 0.1 0.1 0.0

and my "teams" dataset looks like this:

                  Team  W  L  W/L%   GB  PS/G  PA/G  SRS Conference    Season
1     Toronto Raptors* (1) 59 23 0.720    — 111.7 103.9 7.29    Eastern 2017-2018
2      Boston Celtics* (2) 55 27 0.671  4.0 104.0 100.4 3.23    Eastern 2017-2018
3  Philadelphia 76ers* (3) 52 30 0.634  7.0 109.8 105.3 4.30    Eastern 2017-2018
4 Cleveland Cavaliers* (4) 50 32 0.610  9.0 110.9 109.9 0.59    Eastern 2017-2018
5      Indiana Pacers* (5) 48 34 0.585 11.0 105.6 104.2 1.18    Eastern 2017-2018
6          Miami Heat* (6) 44 38 0.537 15.0 103.4 102.9 0.15    Eastern 2017-2018

and my switch function which select the dataset based on my selectInput looks like this:

leaderboardDatasetInput <- reactive({
 switch(
  input$leaderboard_dataset_input,
  "players" = d_season_combined,
  "teams" = d_team_regular_raw,
  "drafts" = d_draft
)})

Because my dataset are different, I want to assign different column as the "base" to plot my data. My "players" dataset has 'Player' as its "base column" and my "teams" dataset has 'Team' as its base column:

leaderboardCharColumn <- reactive({
switch(
  leaderboardDatasetInput(),
  "players" = Player,
  "teams" = Team,
  "draft" = Player
)})

I tried to access the value from the selectInput as follow:

 leaderboard <- reactive({
      datasetInput() %>%
      select(leaderboardCharColumn ())
  })

However, the returned value doesn't get recognize as a variable from the dataset. The error I get says:

EXPR must be a length 1 vector

I have also tried to use get():

 leaderboard <- reactive({
      datasetInput() %>%
      select(get(leaderboardCharColumn ()))
  })

as.symbol():

leaderboard <- reactive({
      datasetInput() %>%
      select(as.symbol(leaderboardCharColumn ()))
  })

!!!:

leaderboard <- reactive({
      datasetInput() %>%
      select(!!!leaderboardCharColumn ())
  })

And also surrendered the switch statements with '':

 leaderboardCharColumn <- reactive({
  switch(
   datasetInput(),
   "players" = 'Player',
   "teams" = 'Team',
   "draft" = 'Player'
)


 })

But I stil get the same error. Your help would be greatly appreciated!

yukikongju
  • 11
  • 4
  • 3
    What exactly is `datasetInput()`? Is that a data.frame? Are you passing the entire data.frame to your switch statement? What exactly is the desired behavior there? I'm guessing that you get different errors for these. If they all give the same error, maybe the error is somewhere else. Be sure to include the exact error message with each attempt. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Mar 26 '20 at 21:04
  • 'datasetInput()' is the function that select the right column to plot according to the dataset chosen. When I select the "players" dataset, I want to select the player column; when I select the "teams" dataset, I'm selecting the Team column to plot my data. I will provide a better explanation next time. Thank you for your feedback – yukikongju Mar 28 '20 at 01:26

0 Answers0