我正在尝试用你可以过滤一个数据表来构建一个动态应用程序,我想要的是当我为我的第一个变量选择一个模态我的下一个过滤器将会更新并且只针对我的第一个过滤器和相同的模态对于第三个过滤器 我从一个被动的应用程序开始,但它似乎不起作用因为我必须始终保持“全部”选择以显示其他模态并在此之后将其删除...是否可以这样做? 所以我决定添加一个动作按钮,但似乎不适合我的更新输入
我该如何解决?谢谢
我的应用程序示例:
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)
答案 0 :(得分:3)
当我在Filter1
中选择A和B时,数据集是LETTERS
的{{1}}和A
的子集。 B
中的选项为Filter2
,1,2,27,28
为Filter3
。当我在A1, B2, A27, B28
中选择1
时,Filter2
中的选项为Filter3
,并且在A1
中同时选择了2
,{{1} }已更新为Filter2
和Filter3
作为选项。您没有A1
和A27
的{{1}}选项。这是你期待的吗?
All
此代码不包含操作按钮。