我刚刚开始学习ShinyApp,并试图创建一个可以动态过滤其值的表。
我想要的结果
<filter>GlassSupplier Supplier1
WindowType AverageBreakageRate
Aluminum 3.63
Wood 7.22 的结果我得到了。
<filter>GlassSupplier Supplier1
WindowType AverageBreakageRate
Aluminum 2.815
Vinyl 6.165
Wood 7.22 我的代码创建一个表,但不根据select输入选择进行筛选。另外,是否有一种方法可以添加操作按钮,所以只有在按下操作按钮时,表才会反映由于新的select输入参数而引起的更改?
任何帮助都将不胜感激!
library(shiny)
library(dplyr)
library(readxl)
ui <- fluidPage(
titlePanel("title panel"),
sidebarLayout(position = "left",
sidebarPanel("sidebar panel",
selectInput(inputId = "table",
label = "Choose a Supplier",
"Names"),
),
mainPanel("main panel",
tableOutput("myTable")
)))
server <- function(input, output,session)
{
GlassSupplier <- c('Supplier 1','Supplier 2','Supplier 1','Supplier 4','Supplier 2')
WindowType <- c('Wood','Vinyl','Aluminum','Aluminum','Vinyl')
BreakageRate <- c(7.22,6.33,3.63,2,6)
df<- data.frame(GlassSupplier,WindowType,BreakageRate)
data <- reactive({
req(input$table)
dframe <- df %>% group_by(WindowType) %>% summarise(BrkRate = mean(BreakageRate))
})
#Update SelectInput Dynamically
observe({
updateSelectInput(session, "table", choices = df$GlassSupplier)
})
output$myTable = renderTable({
data()
})
}
shinyApp(ui,server)发布于 2021-11-21 01:30:11
你只需要filter。要使actionButton工作,只需将reactive()更改为eventReactive()对象即可。尝尝这个
library(shiny)
library(dplyr)
library(readxl)
ui <- fluidPage(
titlePanel("title panel"),
sidebarLayout(position = "left",
sidebarPanel("sidebar panel",
selectInput(inputId = "table",
label = "Choose a Supplier",
"Names"),
actionButton(inputId = "btn",label="Update")
),
mainPanel("main panel",
tableOutput("myTable")
)))
server <- function(input, output,session)
{
GlassSupplier <- c('Supplier 1','Supplier 2','Supplier 1','Supplier 4','Supplier 2')
WindowType <- c('Wood','Vinyl','Aluminum','Aluminum','Vinyl')
BreakageRate <- c(7.22,6.33,3.63,2,6)
df<- data.frame(GlassSupplier,WindowType,BreakageRate)
data <- eventReactive(input$btn, {
req(input$table)
df %>% dplyr::filter(GlassSupplier %in% input$table) %>%
group_by(WindowType) %>%
dplyr::summarise(BrkRate = mean(BreakageRate))
})
#Update SelectInput Dynamically
observe({
updateSelectInput(session, "table", choices = df$GlassSupplier)
})
output$myTable = renderTable({
data()
})
}
shinyApp(ui,server)https://stackoverflow.com/questions/70050465
复制相似问题