R Shiny 显示没有过滤器输入的完整数据帧,直到过滤器输入更改 [英] R Shiny display full dataframe without filter input until filterinputs are changed
问题描述
我正在尝试构建一个将数据框显示为数据表的应用程序.我想让用户能够在侧边栏面板上使用 selectinput 和 daterangeinput 过滤器对数据表进行子集化.
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.
当我遇到问题时,
- 它仅在选择所有输入时对数据表进行子集化
- 它在加载应用程序时显示一个空的数据表
- 当其中一个输入被改回时,它不会更新数据表.
这是我的闪亮应用示例代码
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)
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 函数中执行所有操作以获得我想要的输出.
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.
推荐答案
===更新答案===
这是一个可行的解决方案.
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 Shiny 显示没有过滤器输入的完整数据帧,直到过滤器输入更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!