我在数据表中有一个“人口”和“区域类型”列,我希望用户进行条件过滤(例如,选择“人口”> 15000,并且计划了“区域类型”)。我正在使用反应性函数,而总体不反应时,areatype工作。我正在考虑使用观察功能,但我不知道。我的问题是如何使反应性和包含依赖性。代码:
#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))
})
})
答案 0 :(得分:0)
没有最少的可复制示例,很难理解问题。但是也许尝试一下。将电抗部分移至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))
})
答案 1 :(得分:0)
请查看是否可以解决问题。您确实必须提供一个最小的可重现示例,包括使用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)