gpt4 book ai didi

r - 如何在 Shiny 的r中隐藏特定用户的menuItem及其包含内容?

转载 作者:行者123 更新时间:2023-12-03 02:58:30 25 4
gpt4 key购买 nike

我正在创建我的应用程序,我想根据用户凭据隐藏特定的菜单项及其包含内容。我想向管理员/测试人员显示所有内容,但不是所有用户。我在堆栈溢出中发现了问题,Hide an element (box/tabs) in shiny dashbaord ,我修改代码如下

library(shiny)
library(shinydashboard)
library(shinyjs)

ui <- dashboardPage(

dashboardHeader(title = "Set")
,dashboardSidebar(
sidebarSearchForm(label = "Search...", "searchText", "searchButton"),
sidebarMenu(
menuItem("Port", tabName = "P", icon = icon("cog"))
,menuItem("Rank", tabName = "Rank", icon = icon("cog"))
,menuItem("Mark", tabName = "Mark", icon = icon("bar-chart"))
,menuItem("Read", tabName = "Read", icon = icon("file-pdf-o"))
,useShinyjs()
,menuItem("Port, tabName = "Ocean", icon = icon("heart"))
)
,uiOutput('Super')
,uiOutput('AList')
,uiOutput('BList')
,uiOutput('DList')
,uiOutput('SList')
)


,dashboardBody(
....
)
)

server <- shinyServer(function(input, output, session) {
observeEvent(session$user,{
if (session$user == "tester") return(NULL)
hide(id = "Port", anim = TRUE)
})
}

shinyApp(ui = ui, server = server)

但是,它不起作用,有什么提示吗?

最佳答案

因此,您的方法有一个问题:menuItems 在最终文档中的外观。

您提供的代码表示:隐藏 ID 为“Port”的元素! 如果 menuItems 实际上有一个 Id,那么一切都很好,但是当您查看它们(右键单击 + 检查)时,您会发现情况并非如此。

仔细检查表明,您的 menuItems 可以在文档中通过标签名称 (= a) 和 data-value (对应于分配的 tabName)。这是隐藏命令的选择参数。我不知道Shinyjs是否提供通过其他属性进行搜索,但是你也可以自己编写JS代码。

在下面的代码中,我使用 textInput 伪造了用户登录。您可以观察到,如果您在文本字段中插入“tester”,则第一个 menuItem 仅显示。

完成方式:您向客户端发送一条消息,以显示/隐藏具有特定 tabName 的项目。 JS 脚本搜索所有a 标签,您的名字存储在其 data-value 属性中。隐藏是通过 display: none 完成的。

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
dashboardHeader(title = "Set"),
dashboardSidebar(
sidebarSearchForm(label = "Search...", "searchText", "searchButton"),
tags$head(tags$script(HTML("
Shiny.addCustomMessageHandler('manipulateMenuItem', function(message){
var aNodeList = document.getElementsByTagName('a');

for (var i = 0; i < aNodeList.length; i++) {
if(aNodeList[i].getAttribute('data-value') == message.tabName) {
if(message.action == 'hide'){
aNodeList[i].setAttribute('style', 'display: none;');
} else {
aNodeList[i].setAttribute('style', 'display: block;');
};
};
}
});
"))),
sidebarMenu(
menuItem("Port", tabName = "P", icon = icon("cog")),
menuItem("Rank", tabName = "Rank", icon = icon("cog")),
menuItem("Mark", tabName = "Mark", icon = icon("bar-chart")),
menuItem("Read", tabName = "Read", icon = icon("file-pdf-o")),
menuItem("Port", tabName = "Ocean", icon = icon("heart"))
),
uiOutput('Super'),
uiOutput('AList'),
uiOutput('BList'),
uiOutput('DList'),
uiOutput('SList')
),
dashboardBody(
textInput("user", "User ID fake.")
)
)

server <- function(input, output, session) {
observeEvent(input$user,{
if(input$user != "tester"){
session$sendCustomMessage(type = "manipulateMenuItem", message = list(action = "hide", tabName = "P"))
}else{
session$sendCustomMessage(type = "manipulateMenuItem", message = list(action = "show", tabName = "P"))
}
})
}

shinyApp(ui, server)

关于r - 如何在 Shiny 的r中隐藏特定用户的menuItem及其包含内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36560304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com