2016-12-13 3 views
1

私はユーザから動的な入力を受け取り、棒グラフと折れ線グラフを生成するアプリケーションを作成しています。バーの適切なパーセンテージを得るために、私は自分のデータを要約する必要があると感じます。私のデータは膨大なので、私はdata.tableを使用しています。Shinyは反応的なdata.tableベースの選択された変数を動的に作成します

私が探しているのは、行内で選択された変数がx軸で、y軸がパーセント(分母)の計算に使用されるべきです。スケールを100%にする。私は状況を描写するためのサンプルデータを作成しました。

library("shiny") 
library("ggplot2") 
library("scales") 
library("data.table") 
library("plotly") 

Location <- sample(1:5,100,replace = T) 
Brand <- sample(1:3,100,replace = T) 
Year <- rep(c("Year 2014","Year 2015"),50) 
Q1 <- sample(1:5,100,replace = T) 
Q2 <- sample(1:5,100,replace = T) 

mydata <- as.data.table(cbind(Location,Brand,Year,Q1,Q2)) 

ui <- shinyUI(fluidPage(
    sidebarPanel(
    fluidRow(
     column(10, 
      div(style = "font-size: 13px;", selectInput("rowvar", label = "Select Row Variable", '')) 
    ), 
     tags$br(), 
     tags$br(), 
     column(10, 
      div(style = "font-size: 13px;", selectInput("columnvar", "Select Column Variable", '')) 
    )) 

), 
    tabPanel("First Page"), 
    mainPanel(tabsetPanel(id='charts', 
         tabPanel("charts",tags$b(tags$br("Graphical Output")),tags$br(),plotlyOutput("plot1")) 
) 
) 
)) 

server <- shinyServer(function(input, output,session){ 
    updateTabsetPanel(session = session 
        ,inputId = 'myTabs') 

    observe({ 
    updateSelectInput(session, "rowvar", choices = (as.character(colnames(mydata))),selected = "mpg") 
    }) 

    observe({ 
    updateSelectInput(session, "columnvar", choices = (as.character(colnames(mydata))),selected = "cyl") 
    }) 

    output$plot1 <- renderPlotly({ 
    validate(need(input$rowvar,''), 
      need(input$columnvar,'')) 
countdd <- mydata[,.N,by=.(get(input$rowvar),get(input$columnvar))] 
sumdd <- countdd[,sum(N),get(input$columnvar)] 
propdd <- countdd$N/sumdd$V1[match(paste0("countdd$",get(input$columnvar)),paste0("sumdd$",get(input$columnvar)))] 
countdd$prop <- round(propdd*100,2) 

ggplot(countdd,aes(x=get(input$rowvar),y=prop)) + geom_bar(stat = 'sum') 

    }) 

}) 

shinyApp(ui = ui, server = server) 

しかし、私はこのコードを実行すると、それは私にエラーを与える - - 以下

は、私が使用しています光沢のあるコードです エラー: を「で」または「keyby」リスト内の項目の長さがあります(100)。それぞれは、xの行またはi(6)が返す行の数と同じ長さでなければなりません。

どのように私はバーとラインのプロットを生成するために同じを使用することができるように反応的なdata.tableを作成することができます。 i、j、およびdata.tableの一部で動的変数を渡すことはできませんか?

ご提案ください!

+0

get()、 'countdd < - mydata [、.N、by = c(input $ rowvar、input $ columnvar)]'を使用しないことで、主なエラーを取り除くことができます。私はあなたがpropddでやろうとしていることにまだ困惑しています。つまり、なぜget()を使用していますか? – Dan

+0

@Dan、はい、プロットを取得せずにデータが表示されません。これは私にデータが作成されていないと私はget()のstackoverflowでいくつかの検索を行う投稿を使用し始めている感じを与えた。どのようにして、Shinyのdata.tableを使って、そのエラーが発生し始めたのかは分かりませんでした。基本的には、プロットを探しています。これは、%1を計算するために、この表が与えるものである** Brand1/Year1 * 100 **、prop.table(mydata $ Brand、mydata $ Year)、2 ) '。データを要約することなくこれを得る方法はありますか?はい、プロットは、入力変数が選択した基準を更新する必要があります。 – user1412

答えて

0

data.tableの表現では、eval()の文字ベクトル(input$rowvarなど)を使用できます。あなたはすべての3つのブランドの合計は、毎年1を合計するようprop.table(table(mydata$Brand,mydata$Year),2)などのデータをまとめてプロットしたい場合:

このプロットを生成
output$plot1 <- renderPlotly({ 
    validate(need(input$rowvar,''), 
      need(input$columnvar,'')) 
    countdd <- mydata[, .(n = .N), by = eval(c(input$rowvar, input$columnvar))] 
    sumdd <- countdd[, .(total = sum(n, na.rm = TRUE)), by = eval(c(input$rowvar))] 
    setkeyv(countdd, input$rowvar) 
    setkeyv(sumdd, input$rowvar) 
    propdd <- sumdd[countdd] 
    propdd[, proportion := round(100 * n/total, 2)] 
    ggplot(propdd, aes_string(x = input$rowvar, y = "proportion", 
     fill = input$columnvar)) + geom_bar(stat = 'sum') 
    }) 

Year 2014 Year 2015 
    1  0.48  0.38 
    2  0.24  0.32 
    3  0.28  0.30 

を、その後、これを(合計は100である)してみてくださいyearは、行変数であり、brandshinyアプリの列変数である場合:

summarized data

関連する問題