在ShinyApp中使用RadioButtons在选定列中选择列的问题

时间:2018-11-29 07:52:06

标签: r shiny radio-button dashboard dt

我正在使用R 构建 shinyApp。我正在使用 radioButtons 选择列,然后再次使用 radiobuttons 在先前选择的列中选择其他列。 我无法这样做,因为每当我从 choose variable','choose wave'和'choose wave'中选择 All 以外的任何内容时都会出现错误。

我相信问题位于服务器的活动部分中。 有人可以看看我的密码吗?我将非常感激:)

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"),

radioButtons(inputId = "columns", label = "choose variable",
           choices =c("All","TS", "NTS"), inline =TRUE,
           selected = c("TS")),

radioButtons(inputId = "regions", label = "choose region",
           choices =c("All", "lhr", "isb", "quta", "karach"), inline = TRUE,
           selected = c("lhr")),

radioButtons(inputId = "waves", label = "choose wave",
           choices =c("All", "Wave_1", "Wave_2"), inline = TRUE,
           selected = c("Wave_1")),


selectInput(inputId = "cyl",
          label = "cyl:",
          choices = c("All",
                      unique(as.character(data_table$cyl))),
          selected = "All",
          multiple = TRUE),


selectInput(inputId = "vs",
          label = "vs:",
          choices = c("All",
                      unique(as.character(data_table$vs))),
          selected = "All",
          multiple = TRUE),

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,]
}

if(input$vs != 'All'){
  data_table<-data_table[data_table$vs %in%  input$vs,]
}


#TS NTS
if  (input$columns== 'TS'){
  data_table<-  data_table[,c(1,2, 3,4,5,6),drop=FALSE]    }


if  (input$columns== 'NTS'){
  data_table<-  data_table[,c(1,2,7,8,9,10),drop=FALSE]    }



#region
if  (input$regions== 'lhr' ){
  data_table<-  data_table[,c(1,2,3,7),
                           drop=FALSE]    }

if  (input$regions== 'isb' ){
  data_table<-  data_table[,c(1,2,4,8),
                           drop=FALSE]    }


if  (input$regions== 'quta' ){
  data_table<-  data_table[,c(1,2,5,9),
                           drop=FALSE]    }


if  (input$regions== 'karach' ){
  data_table<-  data_table[,c(1,2,6,10),
                           drop=FALSE]    }


#waves
if  (input$waves== 'Wave_1' ){
  data_table<-  data_table[,c(1,2,3,5,7, 9),
                           drop=FALSE]    }

if  (input$waves== 'Wave_2' ){
  data_table<-  data_table[,c(1,2,4,6, 8, 10),
                           drop=FALSE]    }


else
  data_table })

output$ex1 <- DT::renderDataTable(DT::datatable(filter = 'top',
                                              escape = FALSE, 
                                              options = list(pageLength = 
                                                               10, 

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)

1 个答案:

答案 0 :(得分:1)

我不知道您到底想如何构建该逻辑,但是这里是一个示例,说明如何禁用和启用某些输入。它仍然会在控制台中引发一些错误,但是至少所有内容都可以在App中正确显示。

library(shiny)
library(tidyr)
library(dplyr)
library(readr)
library(DT)
library(shinyjs)

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(
  useShinyjs(),
  downloadButton('downLoadFilter',"Download the filtered data"),

  radioButtons(inputId = "columns", label = "choose variable",
               choices =c("All","TS", "NTS"), inline =TRUE,
               selected = c("All")),

  radioButtons(inputId = "regions", label = "choose region",
               choices =c("All", "lhr", "isb", "quta", "karach"), inline = TRUE,
               selected = c("All")),

  radioButtons(inputId = "waves", label = "choose wave",
               choices =c("All", "Wave_1", "Wave_2"), inline = TRUE,
               selected = c("All")),


  selectInput(inputId = "cyl",
              label = "cyl:",
              choices = c("All",
                          unique(as.character(data_table$cyl))),
              selected = "All",
              multiple = TRUE),


  selectInput(inputId = "vs",
              label = "vs:",
              choices = c("All",
                          unique(as.character(data_table$vs))),
              selected = "All",
              multiple = TRUE),

  DT::dataTableOutput('ex1', width="100%")
)}


# Define Server
server <- function(input, output, session) {

  observe({
    if (input$columns != "All") {
      updateRadioButtons(session, "regions", selected = "All")
      updateRadioButtons(session, "waves", selected = "All")

      shinyjs::disable("regions")
      shinyjs::disable("waves")
    } else {
      shinyjs::enable("regions")
      shinyjs::enable("waves")
    }

    if (input$regions != "All") {
      shinyjs::disable("waves")
    }
    if (input$waves != "All") {
      shinyjs::disable("regions")
    }
  })



  thedata <- reactive({

    #TS NTS
    if  (input$columns == 'TS'){
      data_table<-  data_table[,c("cyl","vs", "TS_lhr_Wave_1", "TS_isb_Wave_2", "TS_quta_Wave_1", "TS_karach_Wave_2"),drop=FALSE]    }
    if  (input$columns == 'NTS'){
      data_table<-  data_table[,c("cyl","vs","NTS_lhr_Wave_1", "NTS_isb_Wave_2","NTS_quta_Wave_1", "NTS_karach_Wave_2"),drop=FALSE]    }

    #waves
    if  (input$waves == 'Wave_1' ){
      data_table<-  data_table[,c("cyl","vs","TS_lhr_Wave_1","TS_quta_Wave_1","NTS_lhr_Wave_1", "NTS_quta_Wave_1"), drop=FALSE]    }
    if  (input$waves == 'Wave_2' ){
      data_table<-  data_table[,c("cyl","vs","TS_isb_Wave_2","TS_karach_Wave_2", "NTS_isb_Wave_2", "NTS_karach_Wave_2"), drop=FALSE]    }

    #region
    if  (input$regions == 'lhr' ){
      data_table<-  data_table[,c("cyl","vs","TS_lhr_Wave_1","NTS_lhr_Wave_1"), drop=FALSE]    }
    if  (input$regions == 'isb' ){
      data_table<-  data_table[,c("cyl","vs","TS_isb_Wave_2","NTS_isb_Wave_2"), drop=FALSE]    }
    if  (input$regions == 'quta' ){
      data_table<-  data_table[,c("cyl","vs","TS_quta_Wave_1","NTS_quta_Wave_1"), drop=FALSE]    }
    if  (input$regions == 'karach' ){
      data_table<-  data_table[,c("cyl","vs","TS_karach_Wave_2","NTS_karach_Wave_2"), drop=FALSE]    }

    ## cyl / vs
    if (any(input$cyl != 'All')){
      data_table<-data_table[data_table$cyl %in%   input$cyl,] 
    }
    if(any(input$vs != 'All')){
      data_table<-data_table[data_table$vs %in%  input$vs,]
    }

    req(data_table)

    data_table
  })

  output$ex1 <- DT::renderDataTable({
    req(thedata())

    DT::datatable(filter = 'top', escape = FALSE, width = "100%",
                  options = list(pageLength =  10, 
                                 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)