R selectizeInput根据从其他菜单中选择的内容过滤所有其他菜单(每次进行选择时)
我的数据看起来类似于数据集R selectizeInput根据从其他菜单中选择的内容过滤所有其他菜单(每次进行选择时),r,shiny,R,Shiny,我的数据看起来类似于数据集橙色,其中有可能包含重复值的列,但每一行都是唯一的。 我的代码: 库(闪亮) 图书馆(DT) 库(数据表) dserver我发现的唯一问题是,如果用户在单击绘图一次后重新选择输入,它将继续从筛选列表中筛选,而不是获得一个干净的副本并重新开始。“All”立即消失,因为它不在数据集中作为选项。有没有办法让它成为一个选项?它很接近,但现在当我看到你点击“绘图”时,它会立即删除用户选择的内容。很抱歉误解了。我以为你的意思是点击“绘图”应该会得到“一个干净的副本并重新开始” li
橙色
,其中有可能包含重复值的列,但每一行都是唯一的。我的代码:
库(闪亮)
图书馆(DT)
库(数据表)
dserver我发现的唯一问题是,如果用户在单击绘图一次后重新选择输入,它将继续从筛选列表中筛选,而不是获得一个干净的副本并重新开始。“All”立即消失,因为它不在数据集中作为选项。有没有办法让它成为一个选项?它很接近,但现在当我看到你点击“绘图”时,它会立即删除用户选择的内容。很抱歉误解了。我以为你的意思是点击“绘图”应该会得到“一个干净的副本并重新开始”
library(shiny)
library(DT)
library(data.table)
d <- copy(Orange)
col_names <- names(Orange)
user_friendly_names <- c('TreeNumber', 'TreeAge', 'Circumference')
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
h3("Filters:"),
uiOutput("filters"),
# Plot button
fluidRow(column(2, align = "right",
actionButton("plot_graph_button", "Plot")))
),
mainPanel(tableOutput("summary"))
)
)
server <- function(input, output) {
#### Create the filter lists for UI ####
output$filters <- renderUI({
if(is.null(col_names)) return(NULL)
lapply(1:length(col_names), function(i) {
col <- paste0(col_names[i])
alias <- user_friendly_names[i]
# Populate input with unique values from column
selectizeInput(inputId = alias, label = paste(alias,':'),
choices = c('All', unique(d[[col]])), selected = 'All', multiple = T)
})
})
output$summary <- renderTable({
# Do not show a plot when the page first loads
# Wait until the user clicks "Plot" button
if (input$plot_graph_button == 0){
return()
}
# Update code below everytime the "Plot" button is clicked
input$plot_graph_button
isolate({
# Fresh copy of the full data set every time "Plot" button is clicked
d <- copy(Orange)
# Filter data based on UI
for(f in 1:length(col_names)){
print(paste("This is loop # ", f))
if(eval(parse(text = paste0('is.null(input$',user_friendly_names[f],')')))){
# If the user deleted "All" but failed to pick anything else default to "All" - do not filter
break
}else{
if(eval(parse(text = paste0('input$',user_friendly_names[f]))) != "All"){
print("FALSE -- Input is not == ALL")
d <- d[d[[col_names[f]]] == unlist(eval(parse(text = paste0('input$',user_friendly_names[f])))), ]
}else{
print("TRUE -- Input is defaulted to ALL")
}
}
}
final_summary_table <<- d
})
})
}
shinyApp(ui = ui, server = server)
server <- function(input, output, session) {
output$filters <- renderUI({
# ...
})
lapply(seq_along(d), function(i) {
observeEvent(input[[user_friendly_names[i]]], {
for (j in seq_along(d)[-i]) {
choices <- if ("All" %in% input[[user_friendly_names[i]]])
unique(d[[j]]) else
unique(d[[j]][d[[i]] %in% input[[user_friendly_names[i]]]])
choices <- c("All", choices)
selected <- intersect(choices, input[[user_friendly_names[j]]])
updateSelectInput(session = session, inputId = user_friendly_names[j],
choices = choices, selected = selected)
}
})
})
observeEvent(input$plot_graph_button, {
for (j in seq_along(d)) {
updateSelectInput(session = session, inputId = user_friendly_names[j],
choices = c("All", unique(d[[j]])), selected = "All")
}
})
output$summary <- renderTable({
# ...
})
}