R 数据表中的条件筛选
我在数据表中有一个人口和地区类型列,我希望用户在其中执行条件筛选(例如选择人口>15000&地区类型已计划)。我使用的是一个反应函数,区域类型在工作,而人口没有反应。我正在考虑使用观察功能,但我不知道。我的问题是如何使这两者都是被动的&包括依赖性。代码:R 数据表中的条件筛选,r,shiny,R,Shiny,我在数据表中有一个人口和地区类型列,我希望用户在其中执行条件筛选(例如选择人口>15000&地区类型已计划)。我使用的是一个反应函数,区域类型在工作,而人口没有反应。我正在考虑使用观察功能,但我不知道。我的问题是如何使这两者都是被动的&包括依赖性。代码: #UI ui<-fluidpage(checkboxGroupInput(inputId = "popdensity", label = " P
#UI
ui<-fluidpage(checkboxGroupInput(inputId = "popdensity",
label = " Population Per Km2:",
choices = list(
"< 15,000"=1,
"15,001 - 30,000"=2 ,
">30,000"=3
),
selected =list(1,2,3)
),
selectInput(inputId = "area",
label = " AreaType:",
choices = c(
"All",
unique(as.character(nakuru$AreaType))
)
))
#SERVER
server<-function(input,output)({
output$table<-DT::renderDataTable({
#filtering based on user selection
dt<-reactive({
df<-nakuru
if(input$area!="All"){
df<-df[df$AreaType==input$area,]
}
if(input$popdensity==1){
df[df$PopDensity<=15000,]
}
if(input$popdensity==2){
df[df$PopDensity>15000&df$PopDensity<=30000,]
}
if(input$popdensity==3){
df[df$PopDensity>30000,]
}
df
})
DT::datatable(dt(),options = list(scrollX=TRUE))
})
})
#用户界面
ui如果没有一个最小的可重复的例子,就很难理解这个问题。但也许可以试试这个。将反应部分置于renderDatatable
部分之外
dt<-reactive({
df<-nakuru
if(input$area!="All"){
df<-df[df$AreaType==input$area,]
}
if(input$popdensity==1){
df[df$PopDensity<=15000,]
}
if(input$popdensity==2){
df[df$PopDensity>15000&df$PopDensity<=30000,]
}
if(input$popdensity==3){
df[df$PopDensity>30000,]
}
df
})
output$table<-DT::renderDataTable({
DT::datatable(dt(),options = list(scrollX=TRUE))
})
dt请查看这是否解决了问题。您必须提供一个最小的可复制示例,包括使用dput的数据集
library(shiny)
library(dplyr)
ui<-fluidPage(checkboxGroupInput(inputId = "popdensity",
label = " Population Per Km2:",
choices = list(
"< 15,000"=1,
"15,001 - 30,000"=2 ,
">30,000"=3
),
selected =list(2,3)
),
selectInput(inputId = "area",
label = " AreaType:",
choices = c(
"All",unique(as.character(nakuru$AreaType)))
),
dataTableOutput("filtered_table")
)
#SERVER
server<-function(input,output){
dt<-reactive({
df<-nakuru
print(head(df))
if(input$area!="All"){
df<-df[df$AreaType==input$area,]
}
str(df)
df1<- df %>% filter(Populationperkm2 < 15000)
df2<- df %>% filter(Populationperkm2 > 15000 & Populationperkm2<=30000)
df3<- df %>% filter(Populationperkm2>30000)
temp<- data.frame()
if(1 %in% input$popdensity ){
temp<-rbind(temp,df1)
}
if("2" %in% input$popdensity){
temp<-rbind(temp,df2)
}
if("3" %in% input$popdensity){
temp<-rbind(temp,df3)
}
temp
})
output$filtered_table<-DT::renderDataTable({
DT::datatable(dt(),options = list(scrollX=TRUE))
})
}
shinyApp(ui, server)
库(闪亮)
图书馆(dplyr)
ui您希望得到什么样的输出?假设用户选择人口>15000&区域类型==计划,则会根据数据表上显示的用户选择和输出对列进行筛选。您好@brian,请参阅以获取有关如何为Shining创建MCVE的提示。这将使回答您的问题变得更容易。这假设nakuru不是一个反应数据集。如果你可以下载数据集,这就不起作用了。我只包含了代码中最重要的部分。我尝试了你的选项仍然不起作用。只有areatype响应用户输入,而population不起作用。你在哪里提供了数据集?单击nakuru数据我正在考虑使用observe函数,但不确定如何使用实施它,如果它将在总体列中包括反应性。