使用 shiny::req() 引入 valueBox 占位符
Posted
技术标签:
【中文标题】使用 shiny::req() 引入 valueBox 占位符【英文标题】:Introduce a valueBox placeholder with shiny::req() 【发布时间】:2021-11-13 06:34:57 【问题描述】:我希望当我的应用程序启动时,会出现一个 valueBox 占位符,而不是一个空白区域。比如:
在flexdashboard
中进行以下工作:
req(input$execute1)
但是,shinydashboard
中没有(出现空白)请参阅:
我的代码:
library(shiny)
library(shinydashboard)
header <- dashboardHeader()
sidebar <- dashboardSidebar(
sidebarMenu(
id = "tabs", width = 300,
menuItem("Analysis", tabName = "dashboard", icon = icon("list-ol"))
)
)
body <- dashboardBody(
tabItems(
tabItem(tabName = "dashboard", titlePanel("Analysis"),
fluidPage(
column(2,
box(title = "Analysis", width = 75,
sliderInput(
inputId = 'aa', label = 'AA',
value = 0.5 * 100,
min = 0 * 100,
max = 1 * 100,
step = 1
),
sliderInput(
inputId = 'bb', label = 'BB',
value = 0.5 * 100,
min = 0 * 100,
max = 1 * 100,
step = 1
),
sliderInput(
inputId = 'cc', label = 'CC',
value = 2.5, min = 1, max = 5, step = .15
),
sliderInput(
inputId = 'dd', label = 'DD',
value = 2.5, min = 1, max = 5, step = .15
),
actionButton("execute1", "Click me")
)
),
column(8,
tagAppendAttributes(class = "b1", valueBoxOutput(outputId = "box1", width = 6)))
)
)
)
)
ui <- dashboardPage(header, sidebar, body)
server <- function(input, output, session)
ac <- function(aa, bb, cc, dd)
(aa + cc) + (bb + dd)
reac_1 <- reactive(
tibble(
aa = input$aa,
bb = input$bb,
cc = input$cc,
dd = input$dd
)
)
pred_1 <- reactive(
temp <- reac_1()
ac(
aa = input$aa,
bb = input$bb,
cc = input$cc,
dd = input$dd
)
)
output$box1 <- renderValueBox(
req(input$execute1)
expr = isolate(valueBox(
value = pred_1(),
subtitle = "cap"
))
)
shinyApp(ui, server)
【问题讨论】:
【参考方案1】:您可以使用if
代替req()
来显示一个空的valueBox
,直到用户单击execute1
按钮。
output$box1 <- renderValueBox(
if (input$execute1 == 0)
valueBox("?", subtitle = "cap")
else
expr = isolate(valueBox(
value = pred_1(),
subtitle = "cap"
))
)
【讨论】:
以上是关于使用 shiny::req() 引入 valueBox 占位符的主要内容,如果未能解决你的问题,请参考以下文章