R闪亮显示没有过滤器输入的完整数据帧,直到过滤输入更改为止 [英] R Shiny display full dataframe without filter input until filterinputs are changed

查看:165
本文介绍了R闪亮显示没有过滤器输入的完整数据帧,直到过滤输入更改为止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



当我遇到问题时,我们会收到




  • 所有的输入都被选中

  • 它在加载应用程序时显示一个空的datatable

  • 当其中一个输入被更改时,它不会更新
    datatable。



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

 区域<  -  c(USA,UK,JPN,JPN,UK,USA -  c(A,B,C,A,B,C,A)
状态< - c(完成 在设计,新,在制作,设计,新)
日期< - 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 (日期,%Y-%m-%d)

df< - data.frame(区域,公司,状态,日期)
/ pre>

ui文件是

  ui.R 

ShinyUI(
fluidPage(
titlePanel(Summary),

sidebarLayout(
sidebarPanel(

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

selectInput(company,
label =选择公司,
choices = c(,unique(df $ Company))),

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

dateRangeInput projdate,
label =感兴趣的日期,
start = Sys.Date() - 60,
end = Sys.Date())
),

mainPanel(
dataTableOutput(table)


))

服务器文件是

  server.R 

ShinyServer(功能(输入,输出){

输出$ table< - renderTable({
datadf%>%as.data.frame()%>%filter(Region == input $地区&公司==输入公司&状态==输入$状态)
})
})

应用启动时,它不会显示表,而不应用任何过滤器。
我不知道如果我在输出$ table函数中做所有的事情来获取我想要的输出。



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



我尝试将输入因子放在反应环境中,但是如果没有反应性上下文弹出,则不允许操作。

解决方案

===更新答案===



这是一个工作的解决方案。

 库(闪亮)
库(dplyr)

区域< - c(美国,英国公司< - c(A,B,C,A,B, C,A)
状态< - c(完成,在制作,在设计,新,在制作,在设计,新 b $ b日期< - c(2015-05-01,2015-05-01,2015-06-04,2015-06-20,2015-07-15, 2015-08-12,2015-08-12)
日期< - as.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 =选择公司,
choice = c(,as.vector(unique(df $ Company))))

selectInput(status,
label =选择Proj状态,
choices = c(,as.vector(unique(df $ Status))))

dateRangeInput(projdate,
label =感兴趣的日期
start = Sys.Date() - 60,
end = Sys.Date())
),

mainPanel(
tableOutput(table )


))

服务器< - shinyServer(函数(输入,输出){

输出$ table < - renderTable({
region_sel < - if(nchar(input $ region)== 0)unique(as.vector(df $ Region))else input $ region
company_sel < (nchar(input $ company)== 0)unique(as.vector(df $ Com pany))否则输入$ company
status_sel< - if(nchar(input $ status)== 0)unique(as.vector(df $ Status))else input $ status

过滤器(df,%region_sel中的区域%,%company_sel中的公司%,%status_sel中的状态%)
})
})

shinyApp(ui = ui,server =服务器)

===上一个答案===



不是直接的答案,但这里是一个非常类似的例子,可以帮助你。

 库(闪亮)
库(dplyr)

ui< - shinyUI(fluidPage(

titlePanel(Old Faithful Geyser Data),

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

mainPanel
tableOutput(table)


))

服务器& lt; - shinyServer(function(input,output){

输出$ table< - renderTable({
cyl_sel< - if(is.null(input $ cyl))unique mtcars $ cyl)else as.numeric(输入$ cyl)
vs_sel< - if(is.null(input $ vs))unique(mtcars $ vs)else as.numeric(输入$ 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)


I am trying to build an app which displays a dataframe as a datatable. I wanted to provide users with ability to subset the datatable with selectinput and daterangeinput filters on the sidebar panel.

Heres when I ran into issues,

  • it only subsets the datatable when all the inputs are selected
  • it displays an empty datatable on loading the app
  • when one of the inputs are changed back it doesnot update the datatable.

Here is my sample code for shiny app

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)

The ui file is

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

The Server file is

    server.R

shinyServer(function(input,output){

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

When the app launches it does not display the table without any filter applied. I am not sure if I am doing everything in the output$table function to get the output I desire.

If someone could guide me as to how I can display the datatable without any user input and then the datatable updates as userinputs are changed.

I tried placing the input factors in a reactive environment but then operation not allowed without an reactive context pops up.

解决方案

===Updated answer===

Here is a working solution.

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)

===Previous Answer===

Not a direct answer, but here is a very similar example that may help you.

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)

这篇关于R闪亮显示没有过滤器输入的完整数据帧,直到过滤输入更改为止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆