如何使用R筛选数据
我有一个数据库,包括如何使用R筛选数据,r,shiny,R,Shiny,我有一个数据库,包括 中学代码(代码) 学生性别;1:男性,2:女性(性别) 关于学生的决定;A:通过,R:不通过(决定) 学生在一年中的行为;0:不好,1:好(条件) 该数据分为三年:20082011年和2014年。 我使用R Shining,我创建了性别、决策和行为的图表,只显示了中学和学生的数量。现在我想让用户能够根据某一年过滤这些数据。 代码如下: 用户界面 ui <- fluidPage( textOutput("inst_nbr"),
- 中学代码(代码)
- 学生性别;1:男性,2:女性(性别)
- 关于学生的决定;A:通过,R:不通过(决定)
- 学生在一年中的行为;0:不好,1:好(条件)
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)))
)
如果要使绘图具有反应性,则需要使用反应性数据集
TestFilter
而不是静态数据.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)
库(闪亮)
图书馆(dplyr)
Test2008如果要使绘图成为反应式,则需要使用反应式数据集TestFilter
而不是静态数据。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)
库(闪亮)
图书馆(dplyr)
Test2008针对2个警告,我得到了一个解决方案。在过滤器中,而不是写“过滤器(Test2008$Year==input$YearSelect)”,我需要写“过滤器(Test2008$Year%在%input$YearSelect中)”,但是,我仍然不知道如何筛选我的图形请提供一些虚拟数据,使您的示例具有可复制性。@ismirsehregal Donene下次请使用dput(Test2008)
或head(dput(Test2008),7)
的输出,并复制到此处。这使处理数据变得更加容易。对于这两个警告,我有一个解决方案。在过滤器中,而不是写“过滤器(Test2008$Year==input$YearSelect)”,我需要写“过滤器(Test2008$Year%在%input$YearSelect中)”,但是,我仍然不知道如何筛选我的图形请提供一些虚拟数据,使您的示例具有可复制性。@ismirsehregal Donene下次请使用dput(Test2008)
或head(dput(Test2008),7)
的输出,并复制到此处。这使得处理数据变得更加容易。
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)