闪亮:选择变量到表

时间:2017-11-23 12:36:55

标签: r shiny

我有一个问题要打开这个话题。好吧,我正在尝试为这一个Shiny做一个类似的应用程序:动态数据帧构造; renderUI,observe,reactiveValues。我想在开头添加一个新类别,它将从表中选择变量。我无法将变量与应用程序中的其他元素组合在一起。有人可以向我解释我做错了什么吗? 正如您在图形程序中看到的那样效果不佳。 enter image description here 下面是一个脚本

#rm(list = ls())
library(shiny)

data <- data.frame(Category1 = rep(letters[1:3],each=15),
                   Info = paste("Text info",1:45),
                   Category2 = sample(letters[15:20],45,replace=T),
                   Size = sample(1:100, 45),
                   MoreStuff = paste("More Stuff",1:45))

ui <- fluidPage(

  titlePanel("Test Explorer"),
  sidebarLayout(
    sidebarPanel(
      selectizeInput("show_vars", "Columns to show:",
                     choices = colnames(data), multiple = TRUE,
                     selected = c("Category1","Info","Category2")),
      uiOutput("category1"),
      uiOutput("category2"),
      uiOutput("sizeslider")
    ),
    mainPanel(
      tableOutput("table")
    )
  )
)

server <- function(input, output,session) {


  data2 <- reactive({
    req(input$table)
    if(input$table == "All"){
      return(data)
    } 
    data[,names(data) %in% input$show_vars]
  })


  output$category1 <- renderUI({
    selectizeInput('cat1', 'Choose Cat 1', choices = c("All",sort(as.character(unique(data$Category1)))),selected = "All")
  })

  df_subset <- eventReactive(input$cat1,{
    if(input$cat1=="All") {df_subset <- data}
    else{df_subset <- data[data$Category1 == input$cat1,]}
  })

  df_subset1 <- reactive({
    if(is.null(input$cat2)){df_subset()} else {df_subset()[df_subset()$Category2 %in% input$cat2,]}
  })

  output$category2 <- renderUI({
    selectizeInput('cat2', 'Choose Cat 2 (optional):', choices = sort(as.character(unique(df_subset()$Category2))), multiple = TRUE,options=NULL)
  })

  output$sizeslider <- renderUI({
    sliderInput("size", label = "Size Range", min=min(df_subset1()$Size), max=max(df_subset1()$Size), value = c(min(df_subset1()$Size),max(df_subset1()$Size)))
  })

  df_subset2 <- reactive({
    if(is.null(input$size)){df_subset1()} else {df_subset1()[df_subset1()$Size >= input$size[1] & df_subset1()$Size <= input$size[2],]}
  })

  output$table <- renderTable({
    df_subset2()
  })
}

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:0)

您不需要data2,因为您没有使用它,而是可以使用相同的条件来过滤显示数据框的所有位置的%in%列。

#rm(list = ls())
library(shiny)

data <- data.frame(Category1 = rep(letters[1:3],each=15),
                   Info = paste("Text info",1:45),
                   Category2 = sample(letters[15:20],45,replace=T),
                   Size = sample(1:100, 45),
                   MoreStuff = paste("More Stuff",1:45))

ui <- fluidPage(

  titlePanel("Test Explorer"),
  sidebarLayout(
    sidebarPanel(
      selectizeInput("show_vars", "Columns to show:",
                     choices = colnames(data), multiple = TRUE,
                     selected = c("Category1","Info","Category2")),
      uiOutput("category1"),
      uiOutput("category2"),
      uiOutput("sizeslider")
    ),
    mainPanel(
      tableOutput("table")
    )
  )
)

server <- function(input, output,session) {





  output$category1 <- renderUI({
    selectizeInput('cat1', 'Choose Cat 1', choices = c("All",sort(as.character(unique(data$Category1)))),selected = "All")
  })

  df_subset <- eventReactive(input$cat1,{
    if(input$cat1=="All") {df_subset <- data}
    else{df_subset <- data[data$Category1 == input$cat1,names(data) %in% input$show_vars]}
  })

  df_subset1 <- reactive({
    if(is.null(input$cat2)){df_subset()} else {df_subset()[df_subset()$Category2 %in% input$cat2,names(data) %in% input$show_vars]}
  })

  output$category2 <- renderUI({
    selectizeInput('cat2', 'Choose Cat 2 (optional):', choices = sort(as.character(unique(df_subset()$Category2))), multiple = TRUE,options=NULL)
  })

  output$sizeslider <- renderUI({
    sliderInput("size", label = "Size Range", min=min(df_subset1()$Size), max=max(df_subset1()$Size), value = c(min(df_subset1()$Size),max(df_subset1()$Size)))
  })

  df_subset2 <- reactive({
    if(is.null(input$size)){df_subset1()} else {df_subset1()[df_subset1()$Size >= input$size[1] & df_subset1()$Size <= input$size[2],names(data) %in% input$show_vars]}
  })

  output$table <- renderTable({
    df_subset2()
  })
}

shinyApp(ui, server)