RShiny应用程序-呈现数据表条件

RShiny应用程序-呈现数据表条件,r,function,user-interface,shiny,R,Function,User Interface,Shiny,此响应函数的目标是在RShiny应用程序的初始测试期间显示原始starwars数据集(可从dplyr库获得) 在当前场景中,用户必须首先点击runquery以弹出数据,然后可以转到过滤器,相应地进行过滤,再次点击runquery以显示结果 理想的目标是让最终用户首先弹出数据,然后用户能够选择他们想要的过滤器,然后点击runquery,相应地显示结果 任何帮助都将不胜感激!谢谢 library(DT) library(shiny) library(shinydashboard) library(d

此响应函数的目标是在RShiny应用程序的初始测试期间显示原始
starwars
数据集(可从
dplyr
库获得)

在当前场景中,用户必须首先点击
runquery
以弹出数据,然后可以转到过滤器,相应地进行过滤,再次点击
runquery
以显示结果

理想的目标是让最终用户首先弹出数据,然后用户能够选择他们想要的过滤器,然后点击
runquery
,相应地显示结果

任何帮助都将不胜感激!谢谢

library(DT)
library(shiny)
library(shinydashboard)
library(dplyr)
library(dbplyr)
library(tidyverse)
library(DBI)

ui <- function(request) {
  dashboardPage(
    dashboardHeader(title = "SW Pivot"),
    dashboardSidebar(
      actionButton("runit", "RUN QUERY"),
      hr(),
      h4(HTML("&nbsp"), "Filter Data Set"),
      
      uiOutput("hairColorFilter"),
      uiOutput("skinColorFilter")
    ),
    dashboardBody(dataTableOutput("data"))
    
  )
}

data <- starwars

server<-shinyServer(function(input, output, session) {
  
  # Identify Filter Choices -----------------------------------------------
  
  hairColorChoices <- sort(unique(data$hair_color))
  skinColorChoices <- sort(unique(data$skin_color))
  
  # Define User Inputs ----------------------------------------------------
  
  output$hairColorFilter <- renderUI({
    sidebarMenu(
      menuItem(
        text = "Hair Color",
        icon = icon("briefcase"),
        checkboxGroupInput(
          inputId = "hairColorChoices",
          label = NULL,
          choices = hairColorChoices,
          selected = hairColorChoices
        )
      )
    )
  })
  
  output$skinColorFilter <- renderUI({
    sidebarMenu(
      menuItem(
        text = "Skin Color",
        icon = icon("thermometer-half"),
        checkboxGroupInput(
          inputId = "skinColorChoices",
          label = NULL,
          choices = skinColorChoices,
          selected = skinColorChoices
        )
      )
    )
  })
  
  # Table Data --------------------------------------------------------------
  
  filterData <- reactive({
    input$runit

    isolate({
      filterData <- data %>%
        filter(
          hair_color %in% input$hairColorChoices,
          skin_color %in% input$skinColorChoices
        )
    })
  })
  
  output$data <- renderDataTable({
    filterData()
    
  })
})

shinyApp(ui, server)
库(DT)
图书馆(闪亮)
图书馆(shinydashboard)
图书馆(dplyr)
图书馆(dbplyr)
图书馆(tidyverse)
图书馆(DBI)

ui我会尝试用它来代替
#表数据
。不要忘记关闭
服务器后的最后一个
}
。在我闪亮的应用程序中,我使用
observeEvent
进行此类操作。翻译成文字…当我按下
runit
按钮时,我想使用
input
s过滤
数据
,然后将该数据馈送到
DT
输出

observeEvent(input$runit, {
    
      filterData <- data %>%
        filter(
          hair_color %in% isolate(input$hairColorChoices),
          skin_color %in% isolate(input$skinColorChoices)
        )
    
  output$data <- renderDataTable({
    DT::datatable(filterData)
  })

})
observeEvent(输入$runit{
过滤数据%
滤器(
头发颜色%(单位为%)隔离(输入$hairColorChoices),
皮肤颜色%in%隔离(输入$skinColorChoices)
)

输出$data一种方法是创建一个
reactiveValues
对象,该对象可以在开头显示。然后
eventReactive
可以创建过滤后的数据。请尝试此操作

server<-shinyServer(function(input, output, session) {
  
  DF1 <- reactiveValues(data=NULL)
  # Identify Filter Choices -----------------------------------------------
  
  hairColorChoices <- sort(unique(data$hair_color))
  skinColorChoices <- sort(unique(data$skin_color))
  
  # Define User Inputs ----------------------------------------------------
  
  output$hairColorFilter <- renderUI({
    sidebarMenu(
      menuItem(
        text = "Hair Color",
        icon = icon("briefcase"),
        checkboxGroupInput(
          inputId = "hairColorChoices",
          label = NULL,
          choices = hairColorChoices,
          selected = hairColorChoices
        )
      )
    )
  })
  
  output$skinColorFilter <- renderUI({
    sidebarMenu(
      menuItem(
        text = "Skin Color",
        icon = icon("thermometer-half"),
        checkboxGroupInput(
          inputId = "skinColorChoices",
          label = NULL,
          choices = skinColorChoices,
          selected = skinColorChoices
        )
      )
    )
  })
  
  # Table Data --------------------------------------------------------------
  
  filterData <- eventReactive(input$runit, {
    
    isolate({
      filterData <- data %>%
        filter(
          hair_color %in% input$hairColorChoices,
          skin_color %in% input$skinColorChoices
        )
    })
  })
  
  observe({
    DF1$data <- starwars
    if (!is.null(filterData())) DF1$data <- filterData()
  })
  
  output$data <- renderDataTable({
    DF1$data
  })
})

shinyApp(ui, server)

服务器太棒了,谢谢!有没有办法使用“反应式”功能来创建过滤后的数据集?如果您的目标是在单击“运行查询”时更新表,
事件反应式
就足够了。但是,如果您想在用户更改选择时更新表(无需单击“运行查询”),然后将
eventReactive
更改为
reactive
。请注意,filteredData是reactive。