2016-05-30 20 views
0

私はユーザー入力に基づいてggplotの散布図のy変数を変更したいアプリケーションをShinyで作成しています。しかし、私はユーザー入力に基づいてプロットを変更する方法を決定するのに問題があります。私は、次のコードを持っている:Shinyにプロットする変数を選択する

UIを

library(shiny) 
shinyUI(fluidPage(
tags$head(
tags$style(HTML(" 
.multicol { 
    -webkit-column-count: 3; /* Chrome, Safari, Opera */ 
    -moz-column-count: 3; /* Firefox */ 
    column-count: 3; 
    } 
    ")) 
), 

    titlePanel(h1('NBA MVPs', align = "center")), 
    hr(), 
    mainPanel(
    tabsetPanel(type='tabs', 
      tabPanel("Plot", plotOutput("plot")), 
      tabPanel("Description", 
        p("Blah, blah")))), 
    hr(), 
    fluidRow(
    column(2, 
     selectInput('stat', "Statistics", choices=c('Field Goal %'=FG.Perc, '3 Pt. %'=ThreePointPerc,"Free Throw %" = "FTPerc", "Rebounds" = "TRB", "Assists" ="ASST", 'Steals' = "STL", "Points" = "PTS", "True Shooting %"= "TrueShootingPerc", "Eff. FG %"= "eFG", "Total Reb. %" = "TRBPerc", "Off. Rating" = "ORtg", "Def. Rating" = "DRtg"))) 
), 
wellPanel(
tags$div(class = "multicol", checkboxGroupInput("player", choices = c('one' = '1', 'two' = '2'), label = "Player/Year", selected = c('1', '2') 
    )) 
) 

サーバー

library(shiny); library(dplyr); library(mosaic); library(ggplot2) 
MVP = MVPData 
shinyServer(function(input, output){ 
plotData <- reactive({ 
var = input$stat 
df <- MVPData %>% 
    filter(player %in% input$player) 
df} 
) 
    output$plot <- renderPlot(ggplot(plotData(), aes(x=Rk, y=input$stat, color=player)) + geom_point()) 
}) 

は今のところ、散布図は、すべてのプレーヤーのために同じ文字列としてユーザー入力を読み込み、これだけストレートがありますデータライン。選択肢から引用を削除すると、オブジェクトが見つからないというエラーが表示されます。また、選択肢はデータセットの列なので、names()関数を使って選択肢を呼び出そうとしましたが、予期しない入力の終了についてエラーが発生しました。

dputのoutupt(ヘッド(MVPData)))である:

structure(list(V1 = c("1", "2", "3", "4", "5", "6"), Rk = 1:6, 
G = 1:6, Date = c("2004-11-03", "2004-11-05", "2004-11-06", 
"2004-11-09", "2004-11-10", "2004-11-13"), Age = c("30-270", 
"30-272", "30-273", "30-276", "30-277", "30-280"), Team = c("PHO", 
"PHO", "PHO", "PHO", "PHO", "PHO"), H.A = c("", "@", "@", 
"@", "@", ""), Opponent = c("ATL", "PHI", "NJN", "CHI", "CLE", 
"SAC"), Outcome = c("W (+30)", "W (+10)", "W (+32)", "W (+20)", 
"L (-5)", "L (-2)"), GS = c("1", "1", "1", "1", "1", "1"), 
MP = c("24:00:00", "32:00:00", "37:00:00", "33:00:00", "50:00:00", 
"36:00:00"), FG = c(6L, 3L, 6L, 3L, 5L, 11L), FGA = c(12L, 
5L, 8L, 9L, 13L, 16L), FG.Perc = c(0.5, 0.6, 0.75, 0.333, 
0.385, 0.688), ThreePtFG = c(0L, 1L, 1L, 0L, 1L, 5L), ThreePtFGA = c(2L, 
1L, 1L, 3L, 3L, 8L), ThreePointPerc = c(0, 1, 1, 0, 0.333, 
0.625), FT = c(0L, 2L, 4L, 2L, 4L, 2L), FTA = c(0L, 2L, 4L, 
2L, 6L, 2L), FTPerc = c(NA, 1, 1, 1, 0.667, 1), ORB = c(0L, 
0L, 1L, 0L, 1L, 2L), DRB = c(3L, 4L, 1L, 3L, 6L, 1L), TRB = c(3L, 
4L, 2L, 3L, 7L, 3L), ASST = c(4L, 10L, 8L, 12L, 17L, 7L), 
STL = c(2L, 0L, 3L, 0L, 0L, 0L), BLK = c(0L, 0L, 0L, 0L, 
0L, 0L), TOV = c(3L, 4L, 6L, 1L, 1L, 3L), PF = c(2L, 5L, 
0L, 3L, 5L, 3L), PTS = c(12L, 9L, 17L, 8L, 15L, 29L), GmSc = c(7.9, 
8.9, 17.4, 10, 18.5, 24.6), Plus.Minus = c(17L, 4L, 21L, 
21L, 1L, 4L), Regular = c(1L, 1L, 1L, 1L, 1L, 1L), TrueShootingPerc = c(0.5, 
0.765, 0.871, 0.405, 0.48, 0.859), eFG = c(0.5, 0.7, 0.813, 
0.333, 0.423, 0.844), ORBPerc = c(0, 0, 4.2, 0, 1.9, 7.4), 
DRBPerc = c(9.2, 14.3, 2.9, 6.9, 13, 3.3), TRBPerc = c(5.5, 
6.7, 3.4, 4.1, 7.1, 5.3), ASTPerc = c(24.2, 43.5, 33.2, 63.2, 
50.5, 35.4), STLPerc = c(3.9, 0, 4.5, 0, 0, 0), BLKPerc = c(0, 
0, 0, 0, 0, 0), TOVPerc = c(20, 40.5, 38.1, 9.2, 6, 15.1), 
USGPerc = c(24.8, 12.8, 20.7, 14.9, 13.8, 24.6), ORtg = c(98L, 
118L, 122L, 115L, 124L, 163L), DRtg = c(80L, 106L, 96L, 88L, 
109L, 135L), Plus.Minus2 = c(7.9, 8.9, 17.4, 10, 18.5, 24.6 
), postMVP = c(0L, 0L, 0L, 0L, 0L, 0L), player = c("2", 
"2", "1", "2", "1", "2" 
)), .Names = c("V1", "Rk", "G", "Date", "Age", "Team", "H.A", 
"Opponent", "Outcome", "GS", "MP", "FG", "FGA", "FG.Perc", "ThreePtFG", 
"ThreePtFGA", "ThreePointPerc", "FT", "FTA", "FTPerc", "ORB", 
"DRB", "TRB", "ASST", "STL", "BLK", "TOV", "PF", "PTS", "GmSc", 
"Plus.Minus", "Regular", "TrueShootingPerc", "eFG", "ORBPerc", 
"DRBPerc", "TRBPerc", "ASTPerc", "STLPerc", "BLKPerc", "TOVPerc", 
"USGPerc", "ORtg", "DRtg", "Plus.Minus2", "postMVP", "player" 
), .internal.selfref = <pointer: (nil)>, row.names = c(NA, 6L 
), class = c("data.table", "data.frame")) 
+0

あなたの質問に 'dput(head(MVPData))の出力を含めることができますか? – SymbolixAU

+0

@SymbolixAU、私はその情報で投稿を更新しました。 – Megan

答えて

0

ような何かをしようとドン私は次のようなことをします:

MVPData_melted <- melt(MVPData, id.vars = c("V1", "Rk","G","Date","Age","Team","H.A","Opponent","Outcome", "player")) 
## double check this melt does what you need 

そして、「可変」列

library(shiny); library(dplyr); library(mosaic); library(ggplot2) 

ui <- fluidPage(
    tags$head(
     tags$style(HTML(" 
         .multicol { 
         -webkit-column-count: 3; /* Chrome, Safari, Opera */ 
         -moz-column-count: 3; /* Firefox */ 
         column-count: 3; 
         } 
         ")) 
     ), 

    titlePanel(h1('NBA MVPs', align = "center")), 
    hr(), 
    mainPanel(
     tabsetPanel(type='tabs', 
         tabPanel("Plot", plotOutput("plot")), 
         tabPanel("Description", 
           p("Blah, blah")))), 
    hr(), 
    fluidRow(
     column(2, 
      selectInput('stat', "Statistics", choices=c('Field Goal %'="FG.Perc", '3 Pt. %'="ThreePointPerc","Free Throw %" = "FTPerc", "Rebounds" = "TRB", "Assists" ="ASST", 'Steals' = "STL", "Points" = "PTS", "True Shooting %"= "TrueShootingPerc", "Eff. FG %"= "eFG", "Total Reb. %" = "TRBPerc", "Off. Rating" = "ORtg", "Def. Rating" = "DRtg"))) 
    ), 
    wellPanel(
     tags$div(class = "multicol", checkboxGroupInput("player", choices = c('one' = '1', 'two' = '2'), label = "Player/Year", selected = c('1', '2') 
     )) 
    ) 
) 


server <- function(input, output){ 

    plotData <- reactive({ 
     var = input$stat 
     ## now filtering on var using the melted data 
     df <- MVPData_melted %>% 
      filter(player %in% input$player, 
         variable == var) 
     df 
     }) 

## i've moved the plot into its own function 
output$plot <- renderPlot({ 
    ggplot(plotData(), aes(x=Rk, y=value, color=player)) + 
     geom_point() 
    }) 
} 

shinyApp(ui = ui, server = server) 
0

はなぜ、それがggplot2が溶融したデータを "好む" というのが私の理解だあなたはこの

output$plot1 <- renderPlot({ 

    selectedaxis <- input$selectaxis 


    ifelse(test = selectedaxis == "input$selectedaxis", 
      yes = ggplot(plotData(), aes(x=Rk, y=input$stat, color=player)) + geom_point(), 
      no = ggplot(plotData(), aes(x=Rk, y=input$stat, color=player)) + geom_point() 
      ) 
    }) 
+0

私は条件文の中に何が入っているのか理解していません。具体的に教えてください。具体的に何がテストであり、条件が何をチェックしていますか? – Megan

関連する問題