问题描述
我们的想法是拥有一个用户输入(访问代码),根据该用户输入可以访问不同的menuItem。所以我们基本上可以根据用户的要求提供自定义版本的应用程序。
3个menuItem的工作示例如下:
库(闪亮)
库(shinydashboard)
库(shinyjs)
ui< - dashboardPage(
dashboardHeader(title =SHOW / HIDE MULTIPLE MENU ITEMS),
dashboardSidebar(
useShinyjs(),
sidebarMenu(
id =tabs,
hidden(
menuItem(MENU ITEM 1,tabName =mi1),
menuItem(MENU ITEM 2,tabName =mi2),
menuItem(MENU ITEM 3 ,tabName =mi3)
),
textInput(inputId =accessToken,label =Access Code,value =显示/隐藏菜单项。)
)
),
dashboardBody()
)
服务器< - 函数(输入,输出,会话){
observeEvent(输入$ accessToken,{
if( input $ accessToken ==001){
hide(selector =ul li:eq(0))
hide(selector =ul li:eq(1))
show(selector =ul li:eq(2))
} else if(input $ accessToken ==010){
hide(selector =ul li:eq(0))
show(selector =ul li:eq(1))
hide(selector =ul li:eq(2))
} else if(input $ accessToken == 011){
hide(selector =ul li:eq(0))
show(selector =ul li:eq(1))
show(selector =ul li:eq(2))
}否则if(输入$ accessToken ==100){
show(selector =ul li:eq(0))
hide( selector =ul li:eq(1))
hide(selector =ul li:eq(2))
} else if(input $ accessToken ==101){
show(selector =ul li:eq(0))
hide(selector =ul li:eq(1))
show(selector =ul li:eq(2))
}否则if(输入$ accessToken ==110){
show(selector =ul li:eq(0))
show(selector =ul li:eq(1))
hide(selector =ul li:eq(2))
} else if(input $ accessToken ==111) {
show(selector =ul li:eq(0))
show(selector =ul li:eq(1))
show(selector =ul li:eq (2))
} else {
hide(selector =ul li)
}
})
}
shinyApp (ui,服务器)
这对于唯一访问代码可见的3个menuItem的所有组合完全正常。
但是你可以看到这是用于访问3个menuItems的大量重复代码。
实际上我有10个甚至更多的menuItem,所以总体来说if else语句的数量会以指数方式增加。
我做过什么:
我想到了这个逻辑:对于10个menuItems,有一个10位数的访问代码,每个数字可以有一个值0(隐藏)或1(显示)。
observeEvent(输入$ accessToken,{
tokenStr< - input $ accessToken
tokenStrShow< - which(strsplit(tokenStr,)[[1]] ==1)
tokenStrHide< - which(strsplit(tokenStr,)[[ 1]] ==0)
for(i in tokenStrShow){
#显示所有menuItems值为1
的逻辑#FULLOWING DOESNOT WORK
#paste0( show(selector ='ul li:eq(,i,)')
}
})
我还遇到了
库(闪亮)
库(shinydashboard)
库(shinyjs)
ui< - dashboardPage(
dashboardHeader(title =SHOW / HIDE MULTIPLE MENU ITEMS),
dashboardSidebar(
useShinyjs(),
uiOutput('sidebar'),
textInput(inputId =accessToken,label =Access Code,value =显示/隐藏菜单项。)
),
dashboardBody()
)
服务器< - 函数(输入,输出,会话){
输出$ sidebar< - renderUI({
menu_items = list()
if(substr(input $ accessToken,1,1)=='1')
menu_items [[length(menu_items)+1] ] = menuItem(MENU ITEM 1,tabName =mi1)
if(substr(input $ accessToken,2,2)=='1')
menu_items [[length(menu_items)+1]] = menuItem(MENU ITEM 2,tabName =mi2)
if(substr(input $ accessToken,3,3)=='1')
menu_items [[length(menu_items)+1]] = menuItem(MENU ITEM 3,tabName =mi3)
打印(menu_items)
sidebarMenu(id =tabs,menu_items)
})
}
shinyApp(ui,server)
希望这有帮助!
The idea is to have a user input (access code), based on which different menuItems can be accessed. So basically we have a custom version of app available based on user's requirement.
A working example for 3 menuItems is as follow:
library(shiny)
library(shinydashboard)
library(shinyjs)
ui <- dashboardPage(
dashboardHeader(title = "SHOW/HIDE MULTIPLE MENU ITEMS"),
dashboardSidebar(
useShinyjs(),
sidebarMenu(
id = "tabs",
hidden(
menuItem("MENU ITEM 1", tabName = "mi1"),
menuItem("MENU ITEM 2", tabName = "mi2"),
menuItem("MENU ITEM 3", tabName = "mi3")
),
textInput(inputId = "accessToken", label = "Access Code", value = "Show/Hide Menu Items.")
)
),
dashboardBody()
)
server <- function (input, output, session){
observeEvent(input$accessToken,{
if(input$accessToken == "001"){
hide(selector = "ul li:eq(0)")
hide(selector = "ul li:eq(1)")
show(selector = "ul li:eq(2)")
} else if (input$accessToken == "010"){
hide(selector = "ul li:eq(0)")
show(selector = "ul li:eq(1)")
hide(selector = "ul li:eq(2)")
} else if (input$accessToken == "011"){
hide(selector = "ul li:eq(0)")
show(selector = "ul li:eq(1)")
show(selector = "ul li:eq(2)")
} else if (input$accessToken == "100"){
show(selector = "ul li:eq(0)")
hide(selector = "ul li:eq(1)")
hide(selector = "ul li:eq(2)")
} else if (input$accessToken == "101"){
show(selector = "ul li:eq(0)")
hide(selector = "ul li:eq(1)")
show(selector = "ul li:eq(2)")
} else if (input$accessToken == "110"){
show(selector = "ul li:eq(0)")
show(selector = "ul li:eq(1)")
hide(selector = "ul li:eq(2)")
} else if (input$accessToken == "111"){
show(selector = "ul li:eq(0)")
show(selector = "ul li:eq(1)")
show(selector = "ul li:eq(2)")
} else{
hide(selector = "ul li")
}
})
}
shinyApp(ui, server)
This works perfectly fine with all combinations of 3 menuItems visible for a unique access code.
BUT as you can see this is a lot of repetitive code for accessing 3 menuItems.
In actual I have 10 or even higher number of menuItems, so overall the number of if else statements are going to increase exponentially.
WHAT I HAVE TRIED:
I thought about this logic: For 10 menuItems, have an access code of 10 digits, where each digit can have a value 0 (hide) or 1 (show).
observeEvent(input$accessToken, {
tokenStr <- input$accessToken
tokenStrShow <- which(strsplit(tokenStr, "")[[1]]=="1")
tokenStrHide <- which(strsplit(tokenStr, "")[[1]]=="0")
for (i in tokenStrShow){
# some logic to show all menuItems with value 1
# FOLLOWING DOESNOT WORK
# paste0("show(selector='ul li:eq(",i,")'")
}
})
I also came across THIS javascript logic which I am trying to implement. But I do not know how to do it in Shiny.
Here is an idea for growing your sidebarMenu
dynamically using renderUI
and uiOutput
. It is also fairly straightforward to convert to a for loop if your number of tabs grows.
library(shiny)
library(shinydashboard)
library(shinyjs)
ui <- dashboardPage(
dashboardHeader(title = "SHOW/HIDE MULTIPLE MENU ITEMS"),
dashboardSidebar(
useShinyjs(),
uiOutput('sidebar'),
textInput(inputId = "accessToken", label = "Access Code", value = "Show/Hide Menu Items.")
),
dashboardBody()
)
server <- function (input, output, session){
output$sidebar <- renderUI({
menu_items = list()
if(substr(input$accessToken,1,1)=='1')
menu_items[[length(menu_items)+1]] = menuItem("MENU ITEM 1", tabName = "mi1")
if(substr(input$accessToken,2,2)=='1')
menu_items[[length(menu_items)+1]] = menuItem("MENU ITEM 2", tabName = "mi2")
if(substr(input$accessToken,3,3)=='1')
menu_items[[length(menu_items)+1]] = menuItem("MENU ITEM 3", tabName = "mi3")
print(menu_items)
sidebarMenu(id = "tabs",menu_items)
})
}
shinyApp(ui, server)
Hope this helps!
这篇关于R shinydashboard - 根据用户输入显示/隐藏多个menuItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!