我有一个数据库,由
该数据分为3年:2008、2011和2014。我使用R Shiny,创建了性别,决策和行为图表,并仅显示中学和学生的数量。现在,我希望使用户能够根据特定年份过滤此数据。这是代码:
用户界面
ui <- fluidPage(
textOutput("inst_nbr"),
textOutput("stunbr"),
plotOutput("plot_decision"),
plotOutput("genderdonut"),
plotOutput("Conduite"),
checkboxGroupInput(inputId = "YearSelect", "Select the corresponding year",
choices = levels(factor(Test2008$Year)), selected = levels(factor(Test2008$Year)))
)
inst_nbr:中学编号/学生人数:学生人数/ plot_decision:决策的直方图(A / R)/ genderdonut:donut性别分布图/ Conduite:donut行为图表/ YearSelect:从数据库创建的过滤器
服务器
# CREATE YEAR FILTER
TestFilter <- reactive({ # <-- Reactive function here
Test2008 %>%
filter(Test2008$Year == input$YearSelect)
})
# NBR OF INSTITUTIONS
output$inst_nbr= renderText({
Test=TestFilter()
length(unique(x = Test$Code))
})
# PLOT DECISION DIAGRAM
# % of each decison
Per_A= 100*length(which(Test2008$Decision=='A'))/length(Test2008$Decision)
Per_R= 100*length(which(Test2008$Decision=='R'))/length(Test2008$Decision)
DecisionName=c('Accepté','Refusé')
DecisionFraction=c(Per_A,Per_R)
# Plot of decisions
output$plot_decision=renderPlot({
Test=TestFilter()
barplot(height= DecisionFraction, names = DecisionName )
})
我将过滤器应用于中学的数量,仅当我每年检查一次时,它就会起作用,但是当我选中所有复选框时,它不会返回总数。另一方面,我不知道如何将其应用于图表。另外,当我运行应用程序时,我得到这些错误:
警告:错误:
filter()
输入出现问题..1
。x输入..1
的大小必须为111613或1,而不是0。i输入..1
为Test2008$Year == input$YearSelect
。170 :(当未选中任何框时发生)
和
Test2008 $ Year == input $ YearSelect中的警告:较长的对象长度不是较短的对象长度的倍数
虚拟数据
Code Cod_sexe conduite decision year
1002 1 1 A 2008
2065 1 0 R 2008
1002 2 1 A 2008
4225 2 1 R 2011
2005 1 1 R 2011
1003 2 0 R 2014
2005 2 0 A 2014
如果要使绘图成为反应性,则需要使用反应性数据集TestFilter
而不是静态data.frameTest2008
来创建绘图。
我不确定这是否是您想要的逻辑,但这应该可以帮助您入门。
请检查以下内容:
library(shiny)
library(dplyr)
Test2008 <- data.frame(
stringsAsFactors = FALSE,
Code = c(1002L, 2065L, 1002L, 4225L, 2005L, 1003L, 2005L),
Cod_sexe = c(1L, 1L, 2L, 2L, 1L, 2L, 2L),
conduite = c(1L, 0L, 1L, 1L, 1L, 0L, 0L),
Decision = c("A", "R", "A", "R", "R", "R", "A"),
Year = c(2008L, 2008L, 2018L, 2011L, 2011L, 2014L, 2014L)
)
ui <- fluidPage(
checkboxGroupInput(
inputId = "YearSelect",
"Select the corresponding year",
choices = unique(Test2008$Year),
selected = unique(Test2008$Year)
),
textOutput("inst_nbr"),
textOutput("stunbr"),
plotOutput("plot_decision"),
plotOutput("genderdonut"),
plotOutput("Conduite")
)
server <- function(input, output, session) {
# CREATE YEAR FILTER
TestFilter <- reactive({
Test2008 %>% filter(Year %in% input$YearSelect)
})
# NBR OF INSTITUTIONS
output$inst_nbr = renderText({
length(unique(TestFilter()$Code))
})
# PLOT DECISION DIAGRAM
output$plot_decision = renderPlot({
req(TestFilter())
# % of each decison
Per_A = 100 * length(which(TestFilter()$Decision == 'A')) / length(TestFilter()$Decision)
Per_R = 100 * length(which(TestFilter()$Decision == 'R')) / length(TestFilter()$Decision)
DecisionName = c('Accepté', 'Refusé')
DecisionFraction = c(Per_A, Per_R)
barplot(height = DecisionFraction, names = DecisionName)
})
}
shinyApp(ui, server)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句