R 如何在shiny中创建层次结构反应表和菜单
我想做一个层次菜单与反应表输出。我希望我的应用程序可以执行以下操作:R 如何在shiny中创建层次结构反应表和菜单,r,shiny,hierarchy,R,Shiny,Hierarchy,我想做一个层次菜单与反应表输出。我希望我的应用程序可以执行以下操作: 当我选择brand=w时,我可以选择的模型输入应该是w的较低层次(在这种情况下:model应该是w123和w456),表输出应该是品牌w的子集 当我同时选择brand=w和model=w123时,表输出应该列出brand=w&model=w123的子集 这是我的密码,有人能帮我吗?谢谢 ui: library(shiny) shinyUI((fluidPage( titlePanel("hirearchy data g
brand=w
时,我可以选择的模型输入应该是w的较低层次(在这种情况下:model
应该是w123
和w456
),表输出应该是品牌w
的子集brand=w&model=w123的子集
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
})
})
服务器:
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
})
})
库(闪亮)
##测试数据帧
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)
})
#当输入$brand发生变化时,将重新评估该值
ObserveeEvent(输入$brand{
品牌签出?conditionalPanel
。您可以使用它为模型创建单独的选择输入
,并根据品牌
的值选择要显示的项目。另一个选项是使用更新选择输入
更改模型的选择输入
中的可用选项
基于品牌的价值
。进一步考虑,更新选择输入
肯定是一种方法。使用条件面板
只会导致不同输入ID出现问题。