繁体   English   中英

Shiny - 如何使用 checkboxGroupInput 过滤表和箱线图中的变量

[英]Shiny - How to filter variables in table and boxplot using checkboxGroupInput

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

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

用户界面

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)
  }) 
  })

您想要做的是根据用户触发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. 您现在可以动态呈现过滤和未过滤的数据。

我希望这有效。 如果这有效,请不要忘记投票:-)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM