R 如果使用updateselectInput(),DT将快速刷新
在一个闪亮的应用程序中,selectInput()的选项根据数据框df中Grade列的值更新。我需要根据年级的唯一值显示一个DT表R 如果使用updateselectInput(),DT将快速刷新,r,shiny,dt,R,Shiny,Dt,在一个闪亮的应用程序中,selectInput()的选项根据数据框df中Grade列的值更新。我需要根据年级的唯一值显示一个DT表 ui <- uiOutput('mainPage') server <- function(input, output, session) { grade <- c("All",9,10,11,12) output$mainPage <- renderUI({ fluidPage( selectInput
ui <- uiOutput('mainPage')
server <- function(input, output, session) {
grade <- c("All",9,10,11,12)
output$mainPage <- renderUI({
fluidPage(
selectInput(inputId = "grade",shiny::HTML
("<span style='color: white'>Designation</span>"),
choices = grade),
DTOutput('table')
)
})
output$table <- renderDT({
df <- data.frame("Name" = c('Arun','Ram','Krishna','Rama','Ashwin'),
"Grade" = c(10,11,10,12,11),
"StressLevel" = c('Stressful','Very stressful','Very stressful','Stressful','Stressful'))
df$Name<-as.character(df$Name)
rownames(df) <- c()
selectedGrade <- as.list(unique(df[,"Grade"]))
updateSelectInput(session,inputId = "grade",
choices = c("All",selectedGrade))
if(input$grade == "All"){
dataSelected <- df[,c(1,3)]
stressCount <- length(unique(dataSelected$StressLevel))
if(stressCount == 2){
color = c('#ff684c','#e03426')
}else{
color = c('#ff684c')
}
if(stressCount == 0){
color = c()
}
datatable(dataSelected, options = list(pageLenth = 5, searching = FALSE,
lengthMenu = c(5, 10, 15, 20),lengthChange = FALSE,
scrollX = T, autoWidth = TRUE,
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({
'color': '#fff'});",
"}")))%>% formatStyle(
'StressLevel',
Color = styleEqual(unique(dataSelected$StressLevel),
color))
}else{
dataSelected <- df %>% filter(Grade == input$grade)
dataSelected <- dataSelected[,c(1,3)]
stressCount <- length(unique(dataSelected$StressLevel))
if(stressCount == 2){
color = c('#ff684c','#e03426')
}else{
color = c('#ff684c')
}
if(stressCount == 0){
color = c()
}
datatable(dataSelected, options = list(pageLenth = 5, searching = FALSE,
lengthMenu = c(5, 10, 15, 20),lengthChange = FALSE,
scrollX = T, autoWidth = TRUE,
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({
'color': '#fff'});",
"}"))) %>% formatStyle(
'StressLevel',
Color = styleEqual(unique(dataSelected$StressLevel),color))
}
})
}
shinyApp(ui = ui, server = server)
ui您需要设置updateSelectInput()
的selected
参数以保留当前选择:
library(shiny)
library(DT)
library(dplyr)
ui <- uiOutput('mainPage')
server <- function(input, output, session) {
grade <- c("All", 9, 10, 11, 12)
output$mainPage <- renderUI({
fluidPage(selectInput(
inputId = "grade",
shiny::HTML
("<span style='color: white'>Designation</span>"),
choices = grade
),
DTOutput('table'))
})
output$table <- renderDT({
DF <-
data.frame(
"Name" = c('Arun', 'Ram', 'Krishna', 'Rama', 'Ashwin'),
"Grade" = c(10, 11, 10, 12, 11),
"StressLevel" = c(
'Stressful',
'Very stressful',
'Very stressful',
'Stressful',
'Stressful'
)
)
DF$Name <- as.character(DF$Name)
rownames(DF) <- c()
selectedGrade <- as.list(unique(DF[, "Grade"]))
updateSelectInput(
session,
inputId = "grade",
choices = c("All", selectedGrade),
selected = isolate({
input$grade
})
)
if (input$grade == "All") {
dataSelected <- DF[, c(1, 3)]
stressCount <- length(unique(dataSelected$StressLevel))
if (stressCount == 2) {
color = c('#ff684c', '#e03426')
} else{
color = c('#ff684c')
}
if (stressCount == 0) {
color = c()
}
datatable(
dataSelected,
options = list(
pageLenth = 5,
searching = FALSE,
lengthMenu = c(5, 10, 15, 20),
lengthChange = FALSE,
scrollX = T,
autoWidth = TRUE,
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({
'color': '#fff'});",
"}"
)
)
) %>% formatStyle('StressLevel',
Color = styleEqual(unique(dataSelected$StressLevel),
color))
} else{
dataSelected <- DF %>% filter(Grade == input$grade)
dataSelected <- dataSelected[, c(1, 3)]
stressCount <- length(unique(dataSelected$StressLevel))
if (stressCount == 2) {
color = c('#ff684c', '#e03426')
} else{
color = c('#ff684c')
}
if (stressCount == 0) {
color = c()
}
datatable(
dataSelected,
options = list(
pageLenth = 5,
searching = FALSE,
lengthMenu = c(5, 10, 15, 20),
lengthChange = FALSE,
scrollX = T,
autoWidth = TRUE,
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({
'color': '#fff'});",
"}"
)
)
) %>% formatStyle('StressLevel',
Color = styleEqual(unique(dataSelected$StressLevel), color))
}
}, server = FALSE)
}
shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(DT)
图书馆(dplyr)
用户界面