闪亮-如何使用checkboxGroupInput

时间:2018-12-28 20:24:48

标签: r shiny

我不知道如何使用checkboxGroupInput对服务器进行编码以过滤变量选择。我想过滤将在箱线图和表格中显示的变量。我在UI中添加了所有变量。

我的代码:(我使用multi-multi.csv数据集)

UI

shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("fileInPath", label= h4("Import danych")),
      sliderInput("kolor", h4("Wybierz kolor"),min = 0, max = 1, value = 0.5, step=0.05),
      checkboxGroupInput("kolumna", h4("Wybierz kolumny"), choices = c('L1', 'L2', 'L3', 'L4', 'L5', 'L6', 'L7', 'L8', 'L9', 'L10', 'L11', 'L12', 'L13','L14', 'L15', 'L16', 'L17', 'L18', 'L19', 'L20')),
      mainPanel(
      tabsetPanel(type = "tabs",
                  tabPanel("Table", tableOutput("daneIn")),
                  tabPanel("Plot", plotOutput("plot")),
                  tabPanel("Model", verbatimTextOutput("model"))))) ))

服务器

library(shiny)
library(ggplot2)
library(reshape)

model <- function(d){
  tmpTab <- table(as.integer(unlist((d[,-c(1:4)]))))
  ret <- as.integer(head(names(tmpTab[order(tmpTab,decreasing=T)]),10))
  ret <- as.data.frame(matrix(ret,ncol=1))
  return(ret)
}
shinyServer(function(input, output) {
  dataIn <- reactive({
    inFile <- input$fileInPath
    if (is.null(inFile)) {
      return(NULL)
    } read.table(file=inFile$datapath,sep=";",dec=",",header=T,stringsAsFactors=FALSE)
  })
  output$daneIn <- renderTable({
    ret <- rbind(
      head(dataIn(),5),
      tail(dataIn(),5)
    )
    return(ret)
  },include.rownames=FALSE)
  output$plot <- renderPlot({
    d <- dataIn()
    InColor <- rgb(as.numeric(input$kolor),0,0,1)
    d <- melt(d[,-c(2:4)],id.vars="Numer")
    wyk <- (
      ggplot(d,aes(x=variable,y=value)) 
      + geom_boxplot(color = InColor) 
      + xlab("") + ylab("Kula")
    ) 
    return(wyk)
  }) 
  output$model <- renderPrint({
    d <- dataIn()
    ret <- model(d)
    return(ret)
  }) 
  })

1 个答案:

答案 0 :(得分:0)

您要做的是根据用户触发checkInputBoxes时所应用的规则创建条件式无条件过滤器。我无法根据提供的代码推断出规则是什么,但是我可以提出以下建议:

  1. 将以下代码添加到server.R中(其中“您的规则”是指您打算应用的过滤规则):

    filteredData <- shiny::reactive({
    switch(
      input$kolumna,
      'L1' = dataIn() %>% filter(Your Rule), 
      'L2' = dataIn() %>% filter(Your Rule), 
      'L3' = dataIn() %>% filter(Your Rule), 
      'L4' = dataIn() %>% filter(Your Rule), 
      'L5' = dataIn() %>% filter(Your Rule), 
      'L6' = dataIn() %>% filter(Your Rule), 
      'L7' = dataIn() %>% filter(Your Rule), 
      'L8' = dataIn() %>% filter(Your Rule), 
      'L9' = dataIn() %>% filter(Your Rule), 
      'L10' = dataIn() %>% filter(Your Rule), 
      'L11' = dataIn() %>% filter(Your Rule), 
      'L12' = dataIn() %>% filter(Your Rule), 
      'L13' = dataIn() %>% filter(Your Rule),
      'L14' = dataIn() %>% filter(Your Rule), 
      'L15' = dataIn() %>% filter(Your Rule), 
      'L16' = dataIn() %>% filter(Your Rule), 
      'L17' = dataIn() %>% filter(Your Rule), 
      'L18' = dataIn() %>% filter(Your Rule), 
      'L19' = dataIn() %>% filter(Your Rule), 
      'L20' = dataIn() %>% filter(Your Rule)
      )
    })
    
  2. 在渲染代码中添加触发器以渲染过滤的数据,同样我不确定您要在何处进行过滤,但是我将以renderPrint为例:

    output$model <- renderPrint({
     # You have not selected any filters, hence your original upload will be rendered
     if (is.null(input$kolumna)) {
       d <- dataIn()
       ret <- model(d)
       return(ret)
     } else { # You have filtered your data, which filteredData() will handle
       d <- filteredData()
       ret <- model(d)
       return(ret)
     }
    }) 
    
  3. 您现在将能够动态呈现经过过滤和未过滤的数据。

我希望这行得通。如果可行,请不要忘记投票:-)。