如何在闪亮中创建层次结构反应式 table 和菜单
How to create a hierarchy reactive table and menu in shiny
我想做一个带有反应式 table 输出的层级菜单。我希望我的应用执行以下操作:
当我选择brand = w
时,我可以选择的模型输入应该只是w的较低层次(在这种情况下:model
应该是w123
和 w456
),并且 table 输出应该是 brand w
的子集
当我同时选择 brand = w 和 model = w123 时,table 输出应该列出 brand = w & model = w123
的子集
这是我的代码,有人能帮帮我吗?谢谢
ui:
library(shiny)
shinyUI((fluidPage(
titlePanel("hirearchy data group"),
sidebarLayout
(
sidebarPanel
(
selectInput("brand",label="choice the brand",choices=c("all","w","s")),
selectInput("model",label="choice the model",choices=c("all","w123","w456","s99","s88"))
),
mainPanel
(
dataTableOutput("table")
)
))))
服务器:
library(shiny)
## test dataframe
df <- data.frame(id = c("1","1","1","1","1","1","2","2","2","2"),
brand = c("w","w","w","s","s","s","w","w","w","s"),
model = c("w123","w123","w456","s99","s88","s88","w123","w456","w456","s99"),
amount = c(10,9,7,8,6,4,7,3,2,8))
df$id=as.character(df$id)
df$brand=as.character(df$brand)
df$model=as.character(df$model)
shinyServer(function(input, output) {
output$table <- renderDataTable({
if(input$brand!="all") {df=df[which(df$brand==input$brand),]}
if(input$model!="all") {df=df[which(df$model==input$model),]}
df
})
})
将此代码添加到您的服务器:
# This gets re-evaluated when input$brand changes
observeEvent(input$brand, {
brand <- input$brand
# Available model options based on brand
choices <- switch(brand, "w" = c("all","w123","w456"),
"s" = c("all","s99","s88"),
"all" = c("all","w123","w456","s99","s88"))
# Update the model input object to only display wanted choices
updateSelectInput(session, "model", choices = choices)
})
为了 updateSelectInput
工作,您还需要修改服务器函数定义以包含 session
对象:您的服务器定义应读作 shinyServer(function(input, output, session) {
.
我想做一个带有反应式 table 输出的层级菜单。我希望我的应用执行以下操作:
当我选择
brand = w
时,我可以选择的模型输入应该只是w的较低层次(在这种情况下:model
应该是w123
和w456
),并且 table 输出应该是brand w
的子集
当我同时选择 brand = w 和 model = w123 时,table 输出应该列出
brand = w & model = w123
的子集
这是我的代码,有人能帮帮我吗?谢谢
ui:
library(shiny)
shinyUI((fluidPage(
titlePanel("hirearchy data group"),
sidebarLayout
(
sidebarPanel
(
selectInput("brand",label="choice the brand",choices=c("all","w","s")),
selectInput("model",label="choice the model",choices=c("all","w123","w456","s99","s88"))
),
mainPanel
(
dataTableOutput("table")
)
))))
服务器:
library(shiny)
## test dataframe
df <- data.frame(id = c("1","1","1","1","1","1","2","2","2","2"),
brand = c("w","w","w","s","s","s","w","w","w","s"),
model = c("w123","w123","w456","s99","s88","s88","w123","w456","w456","s99"),
amount = c(10,9,7,8,6,4,7,3,2,8))
df$id=as.character(df$id)
df$brand=as.character(df$brand)
df$model=as.character(df$model)
shinyServer(function(input, output) {
output$table <- renderDataTable({
if(input$brand!="all") {df=df[which(df$brand==input$brand),]}
if(input$model!="all") {df=df[which(df$model==input$model),]}
df
})
})
将此代码添加到您的服务器:
# This gets re-evaluated when input$brand changes
observeEvent(input$brand, {
brand <- input$brand
# Available model options based on brand
choices <- switch(brand, "w" = c("all","w123","w456"),
"s" = c("all","s99","s88"),
"all" = c("all","w123","w456","s99","s88"))
# Update the model input object to only display wanted choices
updateSelectInput(session, "model", choices = choices)
})
为了 updateSelectInput
工作,您还需要修改服务器函数定义以包含 session
对象:您的服务器定义应读作 shinyServer(function(input, output, session) {
.