我有一个很好的工作,工作得很好,但我想改进它并添加一些功能。
我的数据如下:
NITG <- c("L447", "G24M", "G112", "F144", "B33D", "M4A1", "G432", "K54N", "K312", "K24K")
RC <- c("7E", "8E", "7D", "2G", "2B", "1D", "2J", "7M", "4H", "1D")
Pheno <- c("ZZ", "SS", "ZE", "ZZ", "ZZ", "ZLMN", "BB", "ZA", "GG", "ZZ")
GRADES <- c("D", "C", "D", "D", "C", "A", "D", "D", "D", "D")
data <- data.frame(NITG, RC, Pheno, GRADES)
我想要的是:
当我为我的变量选择一个模态时,例如让我们为NITG选择“L447”,我想在RC变量滤波器中仅显示RC的“7E”,而不是我在数据中得到的所有RC,“ZZ “对于Pheno变量等等 我想要类似于Excel过滤器的东西。也许我需要一些反应功能,我现在卡住了。
感谢您的帮助
ui <- fluidPage(
titlePanel("Referentiel Renault Nissan"),
sidebarLayout(
sidebarPanel(selectInput("nitg",
"NITG:",
c("All", unique(filtered_df$NITG)),
selected = "All",
multiple = TRUE),
selectInput("pheno",
"Phenomenon:",
c("All", unique(filtered_df$Pheno)),
selected = "All",
multiple = TRUE),
selectInput("rc",
"RC:",
c("All", unique(filtered_df$RC)),
selected = "All",
multiple = TRUE),
selectInput("grade",
"Grades:",
c("All", unique(filtered_df$GRADES)),
selected = "All",
multiple = TRUE)
),
mainPanel(
DT::dataTableOutput("tableprint")
)
)
)
server <- function(input, output, session) {
output$tableprint <- DT::renderDataTable({
# Data
NITG <- c("L447", "G24M", "G112", "F144", "B33D", "M4A1", "G432", "K54N", "K312", "K24K")
RC <- c("7E", "8E", "7D", "2G", "2B", "1D", "2J", "7M", "4H", "1D")
Pheno <- c("ZZ", "SS", "ZE", "ZZ", "ZZ", "ZLMN", "BB", "ZA", "GG", "ZZ")
GRADES <- c("D", "C", "D", "D", "C", "A", "D", "D", "D", "D")
df <- data.frame(NITG, RC, Pheno, GRADES)
# Create filters based on inputs
f1 <- if("All" %in% input$nitg){
df$NITG
} else {
input$nitg
}
f2 <- if("All" %in% input$pheno){
df$Pheno
} else {
input$pheno
}
f3 <- if("All" %in% input$rc){
df$RC
} else {
input$rc
}
f4 <- if("All" %in% input$grade){
df$GRADES
} else {
input$grade
}
# Filter data
filtered_df <- filter(df,
NITG %in% f1,
Pheno %in% f2,
RC %in% f3,
GRADES %in% f4)
# Update selectInput choices based on the filtered data. Update 'selected' to reflect the user input.
updateSelectInput(session, "nitg", choices = c("All", filtered_df$NITG), selected = input$nitg)
updateSelectInput(session, "pheno", choices = c("All", filtered_df$Pheno), selected = input$pheno)
updateSelectInput(session, "rc", choices = c("All", filtered_df$RC), selected = input$rc)
updateSelectInput(session, "grade", choices = c("All", filtered_df$GRADES), selected = input$grade)
datatable(filtered_df)
})
}
# Run the application
shinyApp(ui = ui, server = server)
答案 0 :(得分:1)
您需要updateSelectInput()
的实施。演示是here。这是一个小例子,代码与你的相似。
library(shiny)
library(dplyr)
library(DT)
ui <- fluidPage(
titlePanel("Title"),
sidebarLayout(
sidebarPanel(width=3,
selectInput("filter1", "Filter 1", multiple = T, choices = c("All", LETTERS), selected = "All"),
selectInput("filter2", "Filter 2", multiple = T, choices = c("All", as.character(seq.int(1, length(letters), 1))),
selected = "All")
),
mainPanel(
DT::dataTableOutput("tableprint")
)
)
)
server <- function(input, output, session) {
output$tableprint <- DT::renderDataTable({
# Data
df <- tibble(Letters = LETTERS, Numbers = as.character(seq.int(1, length(letters), 1)))
# Create filters based on inputs
f1 <- if("All" %in% input$filter1) LETTERS else input$filter1
f2 <- if("All" %in% input$filter2) as.character(seq.int(1, length(letters), 1)) else input$filter2
# Filter data
filtered_df <- filter(df,
Letters %in% f1,
Numbers %in% f2)
# Update selectInput choices based on the filtered data. Update 'selected' to reflect the user input.
updateSelectInput(session, "filter1", choices = c("All", filtered_df$Letters), selected = input$filter1)
updateSelectInput(session, "filter2", choices = c("All", filtered_df$Numbers), selected = input$filter2)
datatable(filtered_df)
})
}
# Run the application
shinyApp(ui = ui, server = server)