在一个闪亮的应用程序中使用actionButton和updateSelectInput
问题描述:
我有一个闪亮的应用程序使用UpdateselectInput,我想添加一个actionButton,因为有一些bug单独updateselectInput。 它似乎没有工作,我要显示该表只如果我操作的按钮 我的应用程序是与此类似:在一个闪亮的应用程序中使用actionButton和updateSelectInput
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)),
actionButton("go_button", "GO !")),
mainPanel(
DT::dataTableOutput("tableprint")
)
)
)
server <- function(input, output, session) {
output$tableprint <- DT::renderDataTable({
input$go_button
# 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)
感谢您的帮助!
答
您是否在寻找类似的东西?只有当您点击Go
按钮时,表格才会显示。过滤器的工作方式是一样的。
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)),
actionButton("go_button", "GO !")),
mainPanel(
DT::dataTableOutput("tableprint")
)
)
)
server <- function(input, output, session) {
goButton <- eventReactive(input$go_button,{
# 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)
})
output$tableprint <- DT::renderDataTable({
goButton()
})
}
# Run the application
shinyApp(ui = ui, server = server)
我已经移动所述过滤器代码到一个eventReactive
功能。所以当你点击按钮时,它会根据过滤器对数据进行分类。而output$tableprint
函数调用这个反应函数,所以只有当你点击按钮时才会看到表格。
+0
非常感谢你,但我想保留'updateselectinput()'函数来更新我的过滤器,例如我想在过滤器2选择中只显示1和27,如果我选择A in第一过滤器 –
不太清楚你在找什么。你想添加一个新的'actionButton'并且只在你点击按钮时才显示表格吗?另外'updateSelectInput'有什么问题? – krish
是的,我只想点击按钮就显示我的表格。但在我的例子中,该表自动... 我的真实数据是相当大的,当我过滤它时,出现了一些错误,当我尝试改变变量筛选器中的模态时,应用程序卡住了,我必须关闭它来解决问题。我认为一个动作按钮可以完成这项工作 –