gpt4 book ai didi

r - 如何在 R 中 react 性地更改 ShinyDashboard 框的标题?

转载 作者:行者123 更新时间:2023-12-05 01:39:59 26 4
gpt4 key购买 nike

我的代码如下所示,用户可以从 sidebarpanel 中选择位置,并根据用户选择将数据显示在 mainpanel 中。接下来,我想根据用户选择动态更改绘图的 title。例如,如果用户选择 location1,则 Plot 的图 block 应显示“Loc1”(下图突出显示了我需要更改标题的位置)。我不确定如何在 ShinyDashboard 中实现此目的

请提供解释和代码。

enter image description here

代码:

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) #https://stackoverflow.com/questions/51427189/facet-grid-in-shiny-flexdashboard-giving-error-faceting-variables-must-have-at

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")

最佳答案

您可以结合使用 uiOutputrenderUI 来实现这一点,方法是将 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)

关于r - 如何在 R 中 react 性地更改 ShinyDashboard 框的标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57333744/

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