ユーザーから動的な入力を受け取り、棒グラフと折れ線グラフを生成するアプリを作成しています。バーの適切なパーセンテージを取得するには、データを要約する必要があると感じています。私のデータは巨大なので、data.table を使用しています。
私が探しているのは、行で選択された変数が x 軸であり、y 軸がパーセンテージ (分母) の計算に使用される必要があることです。スケールを 100% にします。状況を説明するサンプル データを作成しました。
以下は、私が使用しているShinyコードです-
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)
しかし、このコードを実行するとエラーが発生します -エラー: 'by' または 'keyby' リストの項目は長さ (100) です。それぞれは、x の行または i (6) によって返される行数と同じ長さでなければなりません。
同じものを使用して棒グラフと折れ線グラフを生成できるように、リアクティブな data.table を作成するにはどうすればよいですか。i,j および data.table の一部で動的変数を渡すことはできませんか?
提案してください !!