添加工具提示到R闪亮仪表板valueBox时出现问题。

huangapple go评论76阅读模式
英文:

Problem adding a tool tip to R shiny dashboard valueBox

问题

我想给一个valueBox副标题添加一个工具提示,但我在这样做时遇到了问题 - 特别是当valueBox已在服务器中生成时。

在我的示例中,您可以看到对于Value 2,我在UI中生成了valueBox,将副标题包装在一个div中,并成功添加了工具提示。但在Value 1中,我在服务器中生成了valueBox,尝试使用相同的方法将副标题包装在div中,但没有工具提示出现!

我不清楚为什么在服务器中给副标题添加div()包装不起作用,而当我在UI中使用此方法添加包装时,它完美运行。

我已经查看了这个问题,它显示了如何给整个valueBox添加工具提示,而不仅仅是副标题,这是我的问题所在。

非常感谢您的帮助!我的完整应用程序要求我在服务器中动态生成valueBoxes,因为它们的数量和内容可能根据用户输入而不同 - 这就是我需要在服务器中生成的原因。

英文:

I would like to add a tooltip to a valueBox subtitle, but I have an issue doing this - specifically when the valueBox has been generated in the server.

In my example you can see for Value 2, I generate the valueBox in the UI, wrap the subtitle in a div, and successfully add a tooltip. But in Value 1, I generate the valueBox in the server, try to use the same approach of wrapping the subtitle in div, but no tooltip appears!


# Load required packages
library(shinydashboard)
library(shinyBS)

# Define UI
ui <- dashboardPage(
  dashboardHeader(title = "Simple Dashboard"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "dashboard",
              fluidRow(
                div(id = "box1",
                    box(
                  title = "Value 1",
                  valueBoxOutput("valueBox1"),
                  div(id = "textbox1", textOutput("text1"))
                )),
                bsTooltip(id = "valbox1", "Not working"),
                bsTooltip(id = "textbox1", "Working as expected"),
              ),
              fluidRow(
                div(id = "box2",
                    box(
                      title = "Value 2",
                      valueBox(32,
                               div(id = "valbox2", "Value 2")),
                      div(id = "textbox2", textOutput("text2"))

                    )),
                bsTooltip(id = "valbox2", "Working as expected"),
                bsTooltip(id = "textbox2", "Working as expected"),
                
              ),
              fluidRow(
                box(
                  actionButton("testButton", "TEST")
                )
              ),
              bsTooltip(id = "testButton", "Working as expected")
      )
    )
  )
)

# Define server
server <- function(input, output, session) {
  # Generate dummy data
  data <- data.frame(
    Value1 = sample(1:100, 1)
  )
  
  # Update value boxes when the action button is clicked
  observeEvent(input$testButton, {
    data$Value1 <- sample(1:100, 1)
    output$valueBox1 <- renderValueBox({
      valueBox(data$Value1,
               div(id = "valbox1", "Value 1"))
    })
    output$text1 <- renderText({
      "More info on Value 1"
    })
  })
  
  output$text2 <- renderText({
    "More info on Value 2"
  })
}

# Run the app
shinyApp(ui = ui, server = server)

I am unclear why adding the div() wrapper to the subtitle within the server is not working, when it works perfectly when I use this approach to add a wrapper in the UI.

I've already reviewed this question which shows how to add a tooltip to the entire valuebox, rather than specifically the subtitle, which is my issue.

Any help gladly received! My full application requires me to dynamically generate the valueBoxes in the server, because they can differ in number and content depending on user input - hence my need to generate in the server.

答案1

得分: 3

要克服您的问题的一种方法是在服务器端使用 renderUI 来输出 valueBoxOutput()。请尝试以下代码:

# 加载所需的包
library(shinydashboard)
library(shinyBS)

# 定义用户界面
ui <- dashboardPage(
  dashboardHeader(title = "Simple Dashboard"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "dashboard",
              fluidRow( uiOutput("v1")
                # div(id = "box1",
                #     box(
                #       title = "Value 1",
                #       valueBoxOutput("valueBox1"),
                #       div(id = "textbox1", textOutput("text1"))
                #     )),
                # bsTooltip(id = "valbox1", "Not working"),
                # bsTooltip(id = "textbox1", "Working as expected")
              ),
              fluidRow(
                div(id = "box2",
                    box(
                      title = "Value 2",
                      valueBox(32,
                               div(id = "valbox2", "Value 2")),
                      div(id = "textbox2", textOutput("text2"))
                      
                    )),
                bsTooltip(id = "valbox2", "Working as expected"),
                bsTooltip(id = "textbox2", "Working as expected")
                
              ),
              fluidRow(
                box(
                  actionButton("testButton", "TEST")
                )
              ),
              bsTooltip(id = "testButton", "Working as expected")
      )
    )
  )
)

# 定义服务器
server <- function(input, output, session) {
  # 生成虚拟数据
  myvalue <- eventReactive(input$testButton, {
    sample(1:100, 1)
  })
  
  output$valueBox1 <- renderValueBox({
    valueBox(myvalue(), 
             div(id = "valbox1", "Value 1"))
  })
  
  output$v1 <- renderUI({
    req(myvalue())
    tagList(
      div(id = "box1",
          box(
            title = "Value 1",
            valueBoxOutput("valueBox1"),
            div(id = "textbox1", textOutput("text1"))
          )),
      bsTooltip(id = "valbox1", "Working as expected?"),
      bsTooltip(id = "textbox1", "Working as expected")
    )
  })
  output$text1 <- renderText({
    "More info on Value 1"
  })
  output$text2 <- renderText({
    "More info on Value 2"
  })
}

# 运行应用程序
shinyApp(ui = ui, server = server)
英文:

One way to overcome your issue is to use renderUI to output valueBoxOutput() on the server side. Try this

# Load required packages
library(shinydashboard)
library(shinyBS)
# Define UI
ui &lt;- dashboardPage(
dashboardHeader(title = &quot;Simple Dashboard&quot;),
dashboardSidebar(
sidebarMenu(
menuItem(&quot;Dashboard&quot;, tabName = &quot;dashboard&quot;, icon = icon(&quot;dashboard&quot;))
)
),
dashboardBody(
tabItems(
tabItem(tabName = &quot;dashboard&quot;,
fluidRow( uiOutput(&quot;v1&quot;)
# div(id = &quot;box1&quot;,
#     box(
#       title = &quot;Value 1&quot;,
#       valueBoxOutput(&quot;valueBox1&quot;),
#       div(id = &quot;textbox1&quot;, textOutput(&quot;text1&quot;))
#     )),
# bsTooltip(id = &quot;valbox1&quot;, &quot;Not working&quot;),
# bsTooltip(id = &quot;textbox1&quot;, &quot;Working as expected&quot;)
),
fluidRow(
div(id = &quot;box2&quot;,
box(
title = &quot;Value 2&quot;,
valueBox(32,
div(id = &quot;valbox2&quot;, &quot;Value 2&quot;)),
div(id = &quot;textbox2&quot;, textOutput(&quot;text2&quot;))
)),
bsTooltip(id = &quot;valbox2&quot;, &quot;Working as expected&quot;),
bsTooltip(id = &quot;textbox2&quot;, &quot;Working as expected&quot;)
),
fluidRow(
box(
actionButton(&quot;testButton&quot;, &quot;TEST&quot;)
)
),
bsTooltip(id = &quot;testButton&quot;, &quot;Working as expected&quot;)
)
)
)
)
# Define server
server &lt;- function(input, output, session) {
# Generate dummy data
myvalue &lt;- eventReactive(input$testButton, {
sample(1:100, 1)
})
output$valueBox1 &lt;- renderValueBox({
valueBox(myvalue(), 
div(id = &quot;valbox1&quot;, &quot;Value 1&quot;))
})
output$v1 &lt;- renderUI({
req(myvalue())
tagList(
div(id = &quot;box1&quot;,
box(
title = &quot;Value 1&quot;,
valueBoxOutput(&quot;valueBox1&quot;),
div(id = &quot;textbox1&quot;, textOutput(&quot;text1&quot;))
)),
bsTooltip(id = &quot;valbox1&quot;, &quot;Working as expected?&quot;),
bsTooltip(id = &quot;textbox1&quot;, &quot;Working as expected&quot;)
)
})
output$text1 &lt;- renderText({
&quot;More info on Value 1&quot;
})
output$text2 &lt;- renderText({
&quot;More info on Value 2&quot;
})
}
# Run the app
shinyApp(ui = ui, server = server)

huangapple
  • 本文由 发表于 2023年7月20日 21:20:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定