闪亮的过滤器更新并不显示所有变量的形式
问题描述:
我想你哟建立一个动态的应用程序可以过滤数据表,我想要的是当我选择我的第一个变量的形式我的下一个过滤器将更新,目的只有模式对应于我的第一个过滤器和第三个过滤器相同 我开始了一个反应的应用程序,但它似乎不工作,因为我必须始终保持“全部”的选择,以显示其他模态,然后删除它...有可能这样做吗? 所以我决定添加一个动作按钮,但似乎不能很好地与我的更新输入闪亮的过滤器更新并不显示所有变量的形式
我该如何解决它?由于
我的应用程序的一个例子:
library(shiny)
library(dplyr)
library(DT)
ui <- fluidPage(
titlePanel("Title"),
sidebarLayout(
sidebarPanel(width=3,
selectInput("filter1", "Filter 1", multiple = T, choices = c("All", LETTERS)),
selectInput("filter2", "Filter 2", multiple = T, choices = c("All", as.character(seq.int(1, length(letters), 1)))),
selectInput("filter3", "Filter 3", multiple = T, choices = c("All", letters)),
actionButton("goButton", "Go!"),
p(class = 'text-center', downloadButton('dl', 'Download Data'))
),
mainPanel(
DT::dataTableOutput("tableprint")
)
)
)
server <- function(input, output, session) {
output$tableprint <- DT::renderDataTable({
# Data
df <- tibble(LETTERS = rep(LETTERS, 2), Numbers = as.character(1:52),
letters = paste(LETTERS, Numbers, sep = ""))
df1 <- df
if("All" %in% input$filter1){
df1
} else if (length(input$filter1)){
df1 <- df1[which(df1$LETTERS %in% input$filter1),]
}
if("All" %in% input$filter2){
df1
} else if (length(input$filter2)){
df1 <- df1[which(df1$Numbers %in% input$filter2),]
}
if("All" %in% input$filter3){
df1
} else if (length(input$filter3)){
df1 <- df1[which(df1$letters %in% input$filter3),]
}
input$goButton
# Update selectInput choices based on the filtered data. Update 'selected' to reflect the user input.
updateSelectInput(session, "filter1", choices = c("All", df$LETTERS), selected = input$filter1)
updateSelectInput(session, "filter2", choices = c("All", df1$Numbers), selected = input$filter2)
updateSelectInput(session, "filter3", choices = c("All", df1$letters), selected = input$filter3)
datatable(df1)
})
output$dl <- downloadHandler('mydata.csv', content = function(file) {
# Data
df <- tibble(LETTERS = rep(LETTERS, 2), Numbers = as.character(1:52),
letters = paste(LETTERS, Numbers, sep = ""))
df1 <- df
if("All" %in% input$filter1){
df1
} else if (length(input$filter1)){
df1 <- df1[which(df1$LETTERS %in% input$filter1),]
}
if("All" %in% input$filter2){
df1
} else if (length(input$filter2)){
df1 <- df1[which(df1$Numbers %in% input$filter2),]
}
if("All" %in% input$filter3){
df1
} else if (length(input$filter3)){
df1 <- df1[which(df1$letters %in% input$filter3),]
}
# Update selectInput choices based on the filtered data. Update 'selected' to reflect the user input.
updateSelectInput(session, "filter1", choices = c("All", df$LETTERS), selected = input$filter1)
updateSelectInput(session, "filter2", choices = c("All", df1$Numbers), selected = input$filter2)
updateSelectInput(session, "filter3", choices = c("All", df1$letters), selected = input$filter3)
datatable(df1)
write.csv(df1, file)
})
}
# Run the application
shinyApp(ui = ui, server = server)
答
当我Filter1
选择A和B,该数据集是LETTERS
与A
和B
子集。 Filter2
中的选项是1,2,27,28
和Filter3
是A1, B2, A27, B28
。当我在Filter2
中选择1
时,Filter3
中的选项为A1
,当Filter2
中也选择2
时,更新为A1
和A27
作为选项。您没有All
选件Filter2
和Filter3
。这是你所期望的吗?
library(shiny)
library(dplyr)
library(DT)
ui <- fluidPage(
titlePanel("Title"),
sidebarLayout(
sidebarPanel(width=3,
selectInput("filter1", "Filter 1", multiple = TRUE, choices = c("All", LETTERS)),
selectInput("filter2", "Filter 2", multiple = TRUE, choices = c("All", as.character(seq.int(1, length(letters), 1)))),
selectInput("filter3", "Filter 3", multiple = TRUE, choices = c("All", letters)) ),
mainPanel(
DT::dataTableOutput("tableprint")
)
)
)
server <- function(input, output, session) {
output$tableprint <- DT::renderDataTable({
# Data
df <- tibble(LETTERS = rep(LETTERS, 2), Numbers = as.character(1:52),
letters = paste(LETTERS, Numbers, sep = ""))
df1 <- df
if("All" %in% input$filter1){
df1
} else if (length(input$filter1)){
df1 <- df1[which(df1$LETTERS %in% input$filter1),]
}
# Update selectInput choices based on the filtered data. Update 'selected' to reflect the user input.
updateSelectInput(session, "filter1", choices = c("All", df$LETTERS), selected = input$filter1)
updateSelectInput(session, "filter2", choices = c("All", df1$Numbers), selected = input$filter2)
if("All" %in% input$filter2){
df1
} else if (length(input$filter2)){
df1 <- df1[which(df1$Numbers %in% input$filter2),]
}
updateSelectInput(session, "filter3", choices = c("All", df1$letters), selected = input$filter3)
if("All" %in% input$filter3){
df1
} else if (length(input$filter3)){
df1 <- df1[which(df1$letters %in% input$filter3),]
}
datatable(df1)
})
}
# Run the application
shinyApp(ui = ui, server = server)
此代码不包括操作按钮。
不知道你想要什么:你的过滤器如预期更新 – HubertL
在第一个过滤器中选择A,然后尝试在第二个过滤器中选择2个模式,我无法修复它,我需要添加“全部”方式。当我选择1时,我不能选择27 ... –
因此,只需在第一个过滤器之后和另外两个过滤器之前移动'updateSelectInput',以便df1仍然包含所有模态。但添加过滤器是一个AND – HubertL