【问题标题】:R Shiny display full dataframe without filter input until filterinputs are changedR Shiny 显示没有过滤器输入的完整数据帧,直到改变过滤器输入
【发布时间】:2016-11-01 16:30:34
【问题描述】:

我正在尝试构建一个将数据框显示为数据表的应用程序。我想为用户提供使用侧边栏面板上的 selectinput 和 daterangeinput 过滤器对数据表进行子集化的能力。

当我遇到问题时,

  • 仅在选择所有输入时才对数据表进行子集化
  • 它在加载应用程序时显示一个空数据表
  • 当其中一个输入被改回时,它不会更新 数据表。

这是我的闪亮应用示例代码

Region <- c("USA","UK","JPN","JPN","UK","USA","USA")
Company <- c("A","B","C","A","B","C","A")
Status <- c("Completed","In Production","In Design","New","In Production","In Design","New")
date <- c("2015-05-01","2015-05-01","2015-06-04","2015-06-20","2015-07-15","2015-08-12","2015-08-12")
date <- as.Date(date,"%Y-%m-%d")

df <- data.frame(Region, Company, Status, date)

ui文件是

ui.R

shinyUI(
   fluidPage(
       titlePanel("Summary"),

sidebarLayout(
        sidebarPanel(

            selectInput("region", 
                        label = "Choose a Region",
                        choices = c("", unique(df$Region))),

            selectInput("company", 
                        label = "Choose a company",
                        choices = c("", unique(df$Company))),

            selectInput("status", 
                        label = "Choose Proj status",
                        choices = c("", unique(df$Status))),

            dateRangeInput("projdate", 
                        label = "Date of interest:",
                        start = Sys.Date() - 60, 
                        end = Sys.Date())
            ),

        mainPanel(
            dataTableOutput("table")
        )
        )
    ))

服务器文件是

    server.R

shinyServer(function(input,output){

    output$table <- renderTable({
    datadf %>% as.data.frame() %>% filter(Region == input$region & Company == input$company & Status == input$status)
        })
})

当应用启动时,它不会在未应用任何过滤器的情况下显示表格。 我不确定我是否在 output$table 函数中做所有事情以获得我想要的输出。

如果有人可以指导我如何在没有任何用户输入的情况下显示数据表,然后数据表会随着用户输入的更改而更新。

我尝试将输入因子放置在响应式环境中,但随后弹出没有响应式上下文的操作。

【问题讨论】:

  • 您需要将过滤器更改为filter(Region %in% selection),其中selection是所有选项的向量,如果没有选择任何选项。
  • 应该像 'selection = c(input$Region, input$Company, input$Status)' 这样如果选择其中一个输入,则显示与这些输入匹配的数据表?谢谢
  • 不,您仍然需要多个过滤器,但我的意思是对于每个过滤器,如果未选择任何内容,则需要假设所有选项都已选择。例如,如果没有选择区域,selection 应该是所有区域的向量。否则你会得到一个空的结果。
  • 所以我为每个输入创建了单独的向量,例如'regionselect

标签: r dynamic datatable shiny user-input


【解决方案1】:

服务器.R

shinyServer(function(input,output,session){


  data_update <- reactive({

    # Compose data frame
    data.frame(
      Name = c("Region", 
               "Company",
               "Status"),
      Value = as.character(c(input$region, 
                             input$company,
                             input$status)), 
      stringsAsFactors=FALSE)
  }) 

  # Show the values using an HTML table
  output$table <- renderTable({
    data_update()
  })
})

UI.R

shinyUI(
  fluidPage(
    titlePanel("Summary"),

    sidebarLayout(
      sidebarPanel(

        selectInput("region", 
                    label = "Choose a Region",
                    choices = c("", unique(df$Region)), selected= '1'),

        selectInput("company", 
                    label = "Choose a company",
                    choices = c("", unique(df$Company)), selected= '1'),

        selectInput("status", 
                    label = "Choose Proj status",
                    choices = c("", unique(df$Status)), selected= '1'),

        dateRangeInput("projdate", 
                       label = "Date of interest:",
                       start = Sys.Date() - 60, 
                       end = Sys.Date())
      ),

      mainPanel(
        tableOutput("table")
      )
    )
  ))

GLOBAL.R

library(shiny)
library(dplyr)
library(DT)

Region <- c("USA","UK","JPN","JPN","UK","USA","USA")
Company <- c("A","B","C","A","B","C","A")
Status <- c("Completed","In Production","In Design","New","In Production","In Design","New")
date <- c("2015-05-01","2015-05-01","2015-06-04","2015-06-20","2015-07-15","2015-08-12","2015-08-12")
date <- as.Date(date,"%Y-%m-%d")

df <- data.frame(Region, Company, Status, date)

我还建议使用 DT 包来绘制表格。

希望对您有所帮助!干杯

【讨论】:

  • 感谢您的回复。我能够使用 DT 包生成数据表输出,但该应用程序生成了一个表,该表将输入显示为值,而不是对数据帧进行子集化。当给出其中一个输入时,我希望显示成为 df 数据表的子集。例如,如果 Region 选择为 USA,则显示应该是只有字段匹配 USA in Region 的数据表,类似地,如果 Company 选择为 C,则显示应该是 USA 在 region 和 C 在 company 的行。这就是我遇到的麻烦。
【解决方案2】:

===更新答案===

这是一个可行的解决方案。

library(shiny)
library(dplyr)

Region <- c("USA","UK","JPN","JPN","UK","USA","USA")
Company <- c("A","B","C","A","B","C","A")
Status <- c("Completed","In Production","In Design","New","In Production","In Design","New")
date <- c("2015-05-01","2015-05-01","2015-06-04","2015-06-20","2015-07-15","2015-08-12","2015-08-12")
date <- as.Date(date,"%Y-%m-%d")

df <- data.frame(Region, Company, Status, date)

ui <- shinyUI(
  fluidPage(
    titlePanel("Summary"),

    sidebarLayout(
      sidebarPanel(

        selectInput("region", 
                    label = "Choose a Region",
                    choices = c("", as.vector(unique(df$Region)))),

        selectInput("company", 
                    label = "Choose a company",
                    choices = c("", as.vector(unique(df$Company)))),

        selectInput("status", 
                    label = "Choose Proj status",
                    choices = c("", as.vector(unique(df$Status)))),

        dateRangeInput("projdate", 
                       label = "Date of interest:",
                       start = Sys.Date() - 60, 
                       end = Sys.Date())
      ),

      mainPanel(
        tableOutput("table")
      )
    )
  ))

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

  output$table <- renderTable({
    region_sel <- if (nchar(input$region)==0) unique(as.vector(df$Region)) else input$region
    company_sel <- if (nchar(input$company)==0) unique(as.vector(df$Company)) else input$company
    status_sel <- if (nchar(input$status)==0) unique(as.vector(df$Status)) else input$status

    filter(df, Region %in% region_sel, Company %in% company_sel, Status %in% status_sel)
  })
})

shinyApp(ui = ui, server = server)

===上一个答案===

不是直接的答案,但这里有一个非常相似的示例,可能会对您有所帮助。

library(shiny)
library(dplyr)

ui <- shinyUI(fluidPage(

   titlePanel("Old Faithful Geyser Data"),

   sidebarLayout(
      sidebarPanel(
        selectInput("cyl", "cyl", unique(mtcars$cyl), multiple = TRUE),
        selectInput("vs", "vs", unique(mtcars$vs), multiple = TRUE),
        selectInput("am", "am", unique(mtcars$am), multiple = TRUE)
      ),

      mainPanel(
         tableOutput("table")
      )
   )
))

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

    output$table <- renderTable({
        cyl_sel <- if (is.null(input$cyl)) unique(mtcars$cyl) else as.numeric(input$cyl)
        vs_sel <- if (is.null(input$vs)) unique(mtcars$vs) else as.numeric(input$vs)
        am_sel <- if (is.null(input$am)) unique(mtcars$am) else as.numeric(input$am)
        filter(mtcars, cyl %in% cyl_sel, vs %in% vs_sel, am %in% am_sel)
    })

})

shinyApp(ui = ui, server = server)

【讨论】:

  • 这确实很清楚,我能够编辑以显示字符输入而不是数字。我试图删除 multiple = T 以便该表仅从每个输入中过滤一个变量,但结果并不好。如何在每个 selectInput 函数中选择一个输入而不是多个输入并相应地对数据框进行排序,以及如何在没有选择时让该函数显示整个数据框?任何帮助深表感谢。非常感谢您指导可行的解决方案。
  • 我已经发布了一个完整的解决方案,请参阅更新的答案
  • 使用 nchar 函数做到了!!!感谢您的输入,现在我可以更清楚地理解它了!
猜你喜欢
  • 1970-01-01
  • 2018-12-29
  • 2020-03-30
  • 2011-03-07
  • 2017-07-16
  • 1970-01-01
  • 2017-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多