如何在 R 中反应性地更改 ShinyDashboard 框的标题?
How to reactively change title of ShinyDashboard box in R?
我的代码如下所示,用户可以从 sidebarpanel
select 定位,并根据用户 select 离子数据显示在 mainpanel
中。接下来,我想根据用户 selection 动态更改情节的 title
。例如,如果用户 selects location1 那么 Plot 的图块应该显示 "Loc1"(下图突出显示了我需要更改标题的地方)。我不确定如何在 ShinyDashboard
请提供代码解释。
代码:
library(shiny)
library(shinydashboard)
resetForm<-function(session){
updateSelectInput(session,"slct1",selected = ' ')
}
ui<-dashboardPage(
dashboardHeader(title="System Tracker"),
dashboardSidebar(
selectInput('slct1',"Select Location",choices = c(" ",d$Locations)),
actionButton('clear',"Reset Form"),
h4("Powered by:"),
tags$img(src='baka.png',height=50,width=50)
),
dashboardBody(
#fluidRow(
# box( DT::dataTableOutput("mytable")),
# box(plotlyOutput('out'))
conditionalPanel(
#Uses a Javascript formatted condition
condition="input.slct1 !== ' '",
box( DT::dataTableOutput("mytable")),
box(plotlyOutput('out'),status = 'warning',solidHeader = T)
)
)
)
server<-function(input, output,session) {
output$mytable = DT::renderDataTable({
req(input$slct1)
d %>%
filter(Locations==input$slct1)
})
output$out<-renderPlotly({
req(input$slct1)
data_filter<-dd %>%
filter(Locations==input$slct1)
req(nrow(data_filter)>0) #
ggplotly(ggplot(data_filter, aes(Systems,frequency,fill=year)) +
geom_col(position = 'stack')+geom_text(aes(label=label), position = position_stack(vjust = .5)))#+
#facet_grid(.~Locations, space= "free_x", scales = "free_x"))
})
observeEvent(input$clear,{
req(input$slct1)
resetForm(session)
})
}
shinyApp(ui, server)
数据:
structure(list(Systems = c("Sys1", "Sys1", "Sys2", "Sys3", "Sys4",
"Sys6", "Sys7"), Locations = c("loc1", "loc1", "loc1", "loc2",
"loc2", "loc3", "loc1"), year = structure(c(2L, 1L, 1L, 1L, 1L,
3L, 3L), .Label = c("2019", "2018", "0"), class = "factor"),
frequency = c(1L, 2L, 1L, 1L, 1L, 0L, 0L), freq_cal = c(33.33,
66.67, 100, 100, 100, 0, 0), label = c("33.33%", "66.67%",
"100.00%", "100.00%", "100.00%", "0.00%", "0.00%")), row.names = c(NA,
-7L), class = "data.frame")
好的,所以您需要在服务器端渲染框并将其推送到 ui
尝试在您的 server
中添加以下部分
...
output$box_test <- renderUI({
req(input$slct1)
box(title = input$slct1, status = "primary",solidHeader = TRUE)
})
...
并关注您的 ui
...
dashboardBody(
#fluidRow(
# box( DT::dataTableOutput("mytable")),
# box(plotlyOutput('out'))
conditionalPanel(
#Uses a Javascript formatted condition
condition="input.slct1 !== ' '",
box( DT::dataTableOutput("mytable")),
box(plotlyOutput('out'),status = 'warning',solidHeader = T)
),
uiOutput("box_test")
)
...
您可以结合使用 uiOutput
和 renderUI
来实现此目的,方法是将 box()
函数从 UI 移动到服务器中,如下所示,
library(shiny)
library(plotly)
library(shinydashboard)
d = structure(list(Systems = c("Sys1", "Sys1", "Sys2", "Sys3", "Sys4",
"Sys6", "Sys7"), Locations = c("loc1", "loc1", "loc1", "loc2",
"loc2", "loc3", "loc1"), year = structure(c(2L, 1L, 1L, 1L, 1L,
3L, 3L), .Label = c("2019", "2018", "0"), class = "factor"),
frequency = c(1L, 2L, 1L, 1L, 1L, 0L, 0L), freq_cal = c(33.33,
66.67, 100, 100, 100, 0, 0), label = c("33.33%", "66.67%",
"100.00%", "100.00%", "100.00%", "0.00%", "0.00%")), row.names = c(NA,
-7L), class = "data.frame")
ui<-dashboardPage(
dashboardHeader(title="System Tracker"),
dashboardSidebar(
selectInput('slct1',"Select Location",choices = c(" ",d$Locations)),
actionButton('clear',"Reset Form"),
h4("Powered by:"),
tags$img(src='baka.png',height=50,width=50)
),
dashboardBody(
#fluidRow(
# box( DT::dataTableOutput("mytable")),
# box(plotlyOutput('out'))
conditionalPanel(
#Uses a Javascript formatted condition
condition="input.slct1 !== ' '",
box(DT::dataTableOutput("mytable")),
uiOutput("placeholder")
)
)
)
server<-function(input, output,session) {
output$placeholder = renderUI({
req(input$slct1)
box(title = input$slct1,plotlyOutput('out'),status = 'warning',solidHeader = T)
})
output$mytable = DT::renderDataTable({
req(input$slct1)
d %>%
filter(Locations==input$slct1)
})
output$out<-renderPlotly({
req(input$slct1)
data_filter<-d %>%
filter(Locations==input$slct1)
req(nrow(data_filter)>0)
ggplotly(ggplot(data_filter, aes(Systems,frequency,fill=year)) +
geom_col(position = 'stack')+geom_text(aes(label=label), position = position_stack(vjust = .5)))#+
#facet_grid(.~Locations, space= "free_x", scales = "free_x"))
})
observeEvent(input$clear,{
req(input$slct1)
updateSelectInput(session,"slct1",selected = ' ')
})
}
shinyApp(ui, server)
这个 post 可以追溯到很久以前,但如果有人感兴趣的话,我已经找到了一种无需将 box
放在服务器部分的方法!诀窍是只为框标题创建 renderUI
。它实际上可以在 box(title = ...)
参数中输入 uiOutput
。
使用上面@Sada93提供的正确解决方案,你只需要更换
uiOutput("placeholder")
来自
box(title = uiOutput("placeholder"), plotlyOutput('out'), status = 'warning',solidHeader = T)
和
output$placeholder = renderUI({
req(input$slct1)
box(title = input$slct1,plotlyOutput('out'),status = 'warning',solidHeader = T)
})
来自
output$placeholder = renderUI({
req(input$slct1)
paste("Pasting something before the input$slct1 value :", input$slct1)
})
希望对您有所帮助:)!干杯
我的代码如下所示,用户可以从 sidebarpanel
select 定位,并根据用户 select 离子数据显示在 mainpanel
中。接下来,我想根据用户 selection 动态更改情节的 title
。例如,如果用户 selects location1 那么 Plot 的图块应该显示 "Loc1"(下图突出显示了我需要更改标题的地方)。我不确定如何在 ShinyDashboard
请提供代码解释。
代码:
library(shiny)
library(shinydashboard)
resetForm<-function(session){
updateSelectInput(session,"slct1",selected = ' ')
}
ui<-dashboardPage(
dashboardHeader(title="System Tracker"),
dashboardSidebar(
selectInput('slct1',"Select Location",choices = c(" ",d$Locations)),
actionButton('clear',"Reset Form"),
h4("Powered by:"),
tags$img(src='baka.png',height=50,width=50)
),
dashboardBody(
#fluidRow(
# box( DT::dataTableOutput("mytable")),
# box(plotlyOutput('out'))
conditionalPanel(
#Uses a Javascript formatted condition
condition="input.slct1 !== ' '",
box( DT::dataTableOutput("mytable")),
box(plotlyOutput('out'),status = 'warning',solidHeader = T)
)
)
)
server<-function(input, output,session) {
output$mytable = DT::renderDataTable({
req(input$slct1)
d %>%
filter(Locations==input$slct1)
})
output$out<-renderPlotly({
req(input$slct1)
data_filter<-dd %>%
filter(Locations==input$slct1)
req(nrow(data_filter)>0) #
ggplotly(ggplot(data_filter, aes(Systems,frequency,fill=year)) +
geom_col(position = 'stack')+geom_text(aes(label=label), position = position_stack(vjust = .5)))#+
#facet_grid(.~Locations, space= "free_x", scales = "free_x"))
})
observeEvent(input$clear,{
req(input$slct1)
resetForm(session)
})
}
shinyApp(ui, server)
数据:
structure(list(Systems = c("Sys1", "Sys1", "Sys2", "Sys3", "Sys4",
"Sys6", "Sys7"), Locations = c("loc1", "loc1", "loc1", "loc2",
"loc2", "loc3", "loc1"), year = structure(c(2L, 1L, 1L, 1L, 1L,
3L, 3L), .Label = c("2019", "2018", "0"), class = "factor"),
frequency = c(1L, 2L, 1L, 1L, 1L, 0L, 0L), freq_cal = c(33.33,
66.67, 100, 100, 100, 0, 0), label = c("33.33%", "66.67%",
"100.00%", "100.00%", "100.00%", "0.00%", "0.00%")), row.names = c(NA,
-7L), class = "data.frame")
好的,所以您需要在服务器端渲染框并将其推送到 ui
尝试在您的 server
...
output$box_test <- renderUI({
req(input$slct1)
box(title = input$slct1, status = "primary",solidHeader = TRUE)
})
...
并关注您的 ui
...
dashboardBody(
#fluidRow(
# box( DT::dataTableOutput("mytable")),
# box(plotlyOutput('out'))
conditionalPanel(
#Uses a Javascript formatted condition
condition="input.slct1 !== ' '",
box( DT::dataTableOutput("mytable")),
box(plotlyOutput('out'),status = 'warning',solidHeader = T)
),
uiOutput("box_test")
)
...
您可以结合使用 uiOutput
和 renderUI
来实现此目的,方法是将 box()
函数从 UI 移动到服务器中,如下所示,
library(shiny)
library(plotly)
library(shinydashboard)
d = structure(list(Systems = c("Sys1", "Sys1", "Sys2", "Sys3", "Sys4",
"Sys6", "Sys7"), Locations = c("loc1", "loc1", "loc1", "loc2",
"loc2", "loc3", "loc1"), year = structure(c(2L, 1L, 1L, 1L, 1L,
3L, 3L), .Label = c("2019", "2018", "0"), class = "factor"),
frequency = c(1L, 2L, 1L, 1L, 1L, 0L, 0L), freq_cal = c(33.33,
66.67, 100, 100, 100, 0, 0), label = c("33.33%", "66.67%",
"100.00%", "100.00%", "100.00%", "0.00%", "0.00%")), row.names = c(NA,
-7L), class = "data.frame")
ui<-dashboardPage(
dashboardHeader(title="System Tracker"),
dashboardSidebar(
selectInput('slct1',"Select Location",choices = c(" ",d$Locations)),
actionButton('clear',"Reset Form"),
h4("Powered by:"),
tags$img(src='baka.png',height=50,width=50)
),
dashboardBody(
#fluidRow(
# box( DT::dataTableOutput("mytable")),
# box(plotlyOutput('out'))
conditionalPanel(
#Uses a Javascript formatted condition
condition="input.slct1 !== ' '",
box(DT::dataTableOutput("mytable")),
uiOutput("placeholder")
)
)
)
server<-function(input, output,session) {
output$placeholder = renderUI({
req(input$slct1)
box(title = input$slct1,plotlyOutput('out'),status = 'warning',solidHeader = T)
})
output$mytable = DT::renderDataTable({
req(input$slct1)
d %>%
filter(Locations==input$slct1)
})
output$out<-renderPlotly({
req(input$slct1)
data_filter<-d %>%
filter(Locations==input$slct1)
req(nrow(data_filter)>0)
ggplotly(ggplot(data_filter, aes(Systems,frequency,fill=year)) +
geom_col(position = 'stack')+geom_text(aes(label=label), position = position_stack(vjust = .5)))#+
#facet_grid(.~Locations, space= "free_x", scales = "free_x"))
})
observeEvent(input$clear,{
req(input$slct1)
updateSelectInput(session,"slct1",selected = ' ')
})
}
shinyApp(ui, server)
这个 post 可以追溯到很久以前,但如果有人感兴趣的话,我已经找到了一种无需将 box
放在服务器部分的方法!诀窍是只为框标题创建 renderUI
。它实际上可以在 box(title = ...)
参数中输入 uiOutput
。
使用上面@Sada93提供的正确解决方案,你只需要更换
uiOutput("placeholder")
来自
box(title = uiOutput("placeholder"), plotlyOutput('out'), status = 'warning',solidHeader = T)
和
output$placeholder = renderUI({
req(input$slct1)
box(title = input$slct1,plotlyOutput('out'),status = 'warning',solidHeader = T)
})
来自
output$placeholder = renderUI({
req(input$slct1)
paste("Pasting something before the input$slct1 value :", input$slct1)
})
希望对您有所帮助:)!干杯