Я пытаюсь создать интерактивную панель, которая отображает данные на гистограмме, группируя достижения в сегментах.Эта гистограмма должна соответствовать выбранному году, кварталу или месяцу.Само приложение работает и отображает все правильно, однако при выборе нового месяца / квартала / года визуальные элементы не меняются.Любая помощь очень ценится!
У меня есть следующий набор данных:
date <- c("5/13/2016","1/11/2017","9/6/2016","4/17/2018","4/9/2017","5/23/2016",
"8/5/2017","4/10/2018","12/26/2018","1/11/2016")
employee_id <- c('738138','521743','566295','183475','614729','758291','523776',
'533564','634953','493395')
name <- c('Toby','Kelly','Pam','Jim','Michael','Angela','Oscar','Kevin','Dwight','Andy')
sales <- c('77632','85213','45839','5582','58587','64183','6133','117923','16372','111553')
participation <- c('NULL','Y','NULL','NULL','NULL','NULL','NULL','NULL','Y','NULL')
held_quota <- c('Y','Y','Y','Y','Y','Y','Y','Y','Y','Y')
attainment_bucket <- c('70-89%','100-200%','0-29%','70-89%','30-69%','0-29%','0-29%',
'200-300%','70-89%','0-29%')
sample_data <- data.frame(date,employee_id,name,sales,participation,held_quota,attainment_bucket)
Я немного подделал данные, чтобы я мог больше с ними работать, однако я чувствовал, что это важночтобы узнать изменения здесь для целей интерпретации.
#adding in month&year coulmns to help break down views
class(sample_data$date)
x <- as.Date(sample_data$date, format = "%m/%d/%Y")
sample_data$mo <- strftime(x, "%m")
sample_data$yr <- strftime(x, "%Y")
sample_data$qrt <- quarter(x, with_year = FALSE, fiscal_start = 01)
#changing column names for front end purposes.
colName1 <- c("January" = "01",
"February" = "02",
"March" = "03",
"April" = "04",
"May" = "05",
"June" = "06",
"July" = "07",
"August" = "08",
"September" = "09",
"October" = "10",
"November" = "11",
"December" = "12")
colName2 <- c("Quarter 1" = "1",
"Quarter 2" = "2",
"Quarter 3" = "3",
"Quarter 4" = "4")
col_alias <- function(x) {switch(x,
"01" = "January",
"02" = "February",
"03" = "March",
"04" = "April",
"05" = "May",
"06" = "June",
"07" = "July",
"08" = "August",
"09" = "September",
"10" = "October",
"11" = "November",
"12" = "December")}
col_alias2 <- function(x) {switch(x,
"1" = "Quarter 1",
"2" = "Quarter 2",
"3" = "Quarter 3",
"4" = "Quarter 4")}
#subsetting data to display sales reps that hold a quota
newdata <- sample_data[grepl("Y", sample_data$held_quota),]
#fixing participation column into categorical for donut chart
newdata$participation[is.na(newdata$participation)] <- 0
newdata$participation <- factor(newdata$participation, labels =
c("0-99%","100%"))
#grouping data
newdata2 <- newdata %>%
group_by(yr, mo, qrt)
buckets <- newdata2$attainment_bucket
Часть пользовательского интерфейса начинается здесь:
ui = dashboardPage( skin = "blue",
dashboardHeader( title = "Sales Breakdown "),
dashboardSidebar(
sidebarMenu(
radioButtons("yearOption", "Select Year:", choices =
c("2016", "2017", "2018")),
radioButtons("timeView", "Select View:", choices =
c("Monthly", "Quarterly", "YTD")),
conditionalPanel(condition = 'input.timeView == "Quarterly"',
selectInput("quarter1", "Quarter 1", choices =
colName2),
selectInput("quarter2", "Quarter 2:", choices =
colName2)),
conditionalPanel(condition = 'input.timeView == "Monthly"',
selectInput("month1", "Month 1:", choices = colName1),
selectInput("month2", "Month 2:", choices = colName1)),
conditionalPanel(condition = 'input.timeView == "YTD"'),
numericInput('n',
"Number of Obervations",
min = 1,
max = 20,
value = 5)
)
),
dashboardBody(
fluidRow(
box(width = 6, plotOutput("hist1")),
box(width = 6, plotlyOutput("donut1")),
box(width = 12,tableOutput("table1"))
)
))
Серверная часть начинается здесь:
server = function(input, output) {
output$hist1 <- renderPlot({
g1 <- ggplot(data = filter(newdata2, yr == input$yearOption & mo == input$month1
& qrt == input$quarter1)
, aes_string(x = 'buckets'))+
geom_histogram(fill = "red", color = "black", stat = "count")+
scale_x_discrete(limits=c("0-29%","30-69%","70-89%","90-99%",
"100%-200%","200-300%",">300%"))+
theme_bw()
if (input$timeView == 'Monthly') {
return(g1 + labs(x="Attainment Buckets",
title = paste(col_alias(input$month1),
input$yearOption)))
}
if (input$timeView == 'Quarterly') {
return (g1 + labs(x="Attainment Buckets",
title = paste(col_alias2(input$quarter1),
input$yearOption)))
}
else{
return(g1 + labs(x="Attainment Buckets",
title = paste("YTD",input$yearOption)))
}
})
output$donut1 <- renderPlotly ({
p <- newdata2 %>%
group_by(participation) %>%
summarize(count = n()) %>%
plot_ly(labels = ~participation, values = ~count) %>%
add_pie(hole = 0.6) %>%
layout(title = "Participation", showlegend = T,
xaxis = list(showgrid = FALSE, zeroline = FALSE,
showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE,
showticklabels = FALSE))
print(p)
})
output$table1 <- renderTable ({
head(newdata2[,2:7], input$n)
})
}
shinyApp(ui = ui, server = server)