【发布时间】:2019-05-01 11:30:58
【问题描述】:
我正在使用 R 构建 shinyApp。目前我正在使用selectinput 来选择多个区域/列,方法是设置multiple=TRUE。但由于某些不可预见的原因,它无法正常工作。 它仅在我将 ALL 放入选择区域 时才有效。 我相信我的问题在于服务器的反应部分。我附上了我的代码如下。有人可以看看他们,让我知道他们有什么问题。非常感谢:)
**Updated Codes**
library(shiny)
library(tidyr)
library(dplyr)
library(readr)
library(DT)
data_table<-mtcars[,c(2,8,1,3,4,5,9,6,7, 10,11)]
data_table$disp<-NULL
names(data_table)[3:10]<- rep(x =
c('TS_lhr_Wave_1','TS_isb_Wave_2','TS_quta_Wave_1','TS_karach_Wave_2',
'NTS_lhr_Wave_1','NTS_isb_Wave_2','NTS_quta_Wave_1','NTS_karach_Wave_2'),
times=1, each=1)
# Define UI
ui <- fluidPage(
downloadButton('downLoadFilter',"Download the filtered data"),
selectInput(inputId = "cyl",
label = "cyl:",
choices = c("All",
unique(as.character(data_table$cyl))),
selected = "All",
multiple = TRUE),
checkboxGroupInput(inputId = "regions", label = "choose region",
choices =c("All", "lhr", "isb", "quta", "karach"),
inline = TRUE, selected = c("All")),
checkboxGroupInput(inputId = "waves", label = "choose wave",
choices =c("All", "Wave_1", "Wave_2"), inline = TRUE,
selected = c("All")),
DT::dataTableOutput('ex1'))
# Define Server
server <- function(input, output) {
thedata <- reactive({
if(input$cyl != 'All'){
data_table<-data_table[data_table$cyl %in% input$cyl,] }
#region
cols <- c(1, 2)
# print(input$regions)
if ('All' %in% input$regions){
cols <- 1:ncol(data_table)
}
else{
if ('lhr' %in% input$regions){
cols <- c(cols, c(3,7))
}
if ('isb' %in% input$regions){
cols <- c(cols, c(4,8))
}
if ('quta' %in% input$regions){
cols <- c(cols, c(5,9))
}
if ('karach' %in% input$regions){
cols <- c(cols, c(6,10))
}}
#else
data_table<-data_table[,cols, drop=FALSE]
#waves
colss <- c(1, 2)
# print(input$regions)
if ('All' %in% input$waves){
colss <- 1:ncol(data_table)
}
else{
if ('Wave_1' %in% input$waves){
colss <- c(colss, c(3,5,7, 9))
}
if ('Wave_2' %in% input$waves){
colss <- c(colss, c(4,6, 8, 10))
}}
#else
data_table<-data_table[,colss, drop=FALSE]
})
output$ex1 <- DT::renderDataTable(DT::datatable(filter = 'top',
escape = FALSE,
options = list(
scrollX='500px',autoWidth = TRUE),{
thedata() }))
output$downLoadFilter <- downloadHandler(
filename = function() {
paste('Filtered data-', Sys.Date(), '.csv', sep = '') },
content = function(path){
write_csv(thedata(),path) })}
shinyApp(ui = ui, server = server)
【问题讨论】:
标签: r datatable shiny dashboard dt