【问题标题】:How to reactively change title of ShinyDashboard box in R?如何在 R 中被动更改 ShinyDashboard 框的标题?
【发布时间】:2019-12-11 12:15:02
【问题描述】:

我的代码如下所示,用户可以从sidebarpanel 中选择位置,并根据用户选择数据显示在mainpanel 中。接下来,我想根据用户选择动态更改绘图的title。例如,如果用户选择 location1,那么 Plot 的 tile 应该显示“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) #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")

【问题讨论】:

    标签: r shiny shinydashboard shiny-reactivity


    【解决方案1】:

    您可以结合使用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)
    

    【讨论】:

    • 如果我想显示文本说“选定位置:”然后选择要显示的位置怎么办?
    • 使用paste(...)
    • shud paste(...) 去哪儿了?盒子前?抱歉,我对 ShinyDashBord 有点陌生
    • box(title = paste("Selected Location: ",input$slct1),plotlyOutput('out'),status = 'warning',solidHeader = T)
    • 完美。谢谢!
    【解决方案2】:

    好的,所以你需要在服务器端渲染盒子并将其推送到 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")
    
    
        )
    ...
    

    【讨论】:

    • 我想在第一个方框部分看到类似的内容-rstudio.github.io/shinydashboard/structure.html
    • 我认为@Sada93 早在几秒钟前就提供了他的答案,所以感谢他
    • 你们俩的好方法!!感谢您的时间和努力!这就是我如此喜欢的原因。今天学到了一些新东西:)
    猜你喜欢
    • 2018-02-04
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2018-04-08
    • 2011-02-13
    • 2017-06-19
    相关资源
    最近更新 更多