Shiny NavBar添加其他信息

时间:2016-12-30 07:25:08

标签: r shiny

enter image description here

由于我的代码很长,我考虑了here

中的示例代码

我尝试从here添加以下代码(在ADAY_XXX_YYY.HHHH_NO之后)。但没有运气。

tabPanel

我想要的只是一个纯文本(信息)位于NavBar的右侧。

2 个答案:

答案 0 :(得分:2)

以下代码为您提供所需内容。主要问题是使用tour html标签。

library(shiny)


server <- function(input, output, session) {
  output$plot <- renderPlot({
    plot(cars, type=input$plotType)
  })

  output$summary <- renderPrint({
    summary(cars)
  })

  output$table <- DT::renderDataTable({
    DT::datatable(cars)
  })
}


ui <-shinyUI(navbarPage("Navbar!",
           tabPanel("Plot",
                    sidebarLayout(
                      sidebarPanel(
                        radioButtons("plotType", "Plot type",
                                     c("Scatter"="p", "Line"="l")
                        )
                      ),
                      mainPanel(
                        plotOutput("plot")
                      )
                    )
           ),
           tabPanel("Summary",
                    verbatimTextOutput("summary")
           ),


           tags$script(HTML("var header = $('.navbar > .container');
                       header.append('<div style=\"float:right\"><h3>Company name text here</h3></div>');
                       console.log(header)"))
))

shinyApp(ui = ui, server = server)

希望它有所帮助!

<强> [编辑]: 由于导航栏类的最近更改,上述代码不再起作用。以下代码似乎有效:

library(shiny)


server <- function(input, output, session) {
  output$plot <- renderPlot({
    plot(cars, type=input$plotType)
  })

  output$summary <- renderPrint({
    summary(cars)
  })

  output$table <- DT::renderDataTable({
    DT::datatable(cars)
  })
}


ui <-shinyUI(navbarPage("Navbar!",
                        tabPanel("Plot",
                                 sidebarLayout(
                                   sidebarPanel(
                                     radioButtons("plotType", "Plot type",
                                                  c("Scatter"="p", "Line"="l")
                                     )
                                   ),
                                   mainPanel(
                                     plotOutput("plot")
                                   )
                                 )
                        ),
                        tabPanel("Summary",
                                 verbatimTextOutput("summary")
                        ),


                        tags$script(HTML("var header = $('.navbar> .container-fluid');
                       header.append('<div style=\"float:right\"><h3>Company name text here</h3></div>');
                       console.log(header)"))
))

shinyApp(ui = ui, server = server)

唯一的变化是html脚本.navbar > .container已更改为.navbar> .container-fluid

答案 1 :(得分:0)

我发现了另一种优雅/极简主义的方式,使用“ shinyjs”包将文本添加到导航栏中,它具有我想要的附加好处,即可以在导航栏的右侧堆叠文本。它位于“服务器”中的“ shinyjs :: html()”函数中

library(shiny)

ui <- fluidPage(
    shinyjs::useShinyjs(),#this line NEEDS to be somewhere in the ui!
    navbarPage(
        title = HTML("<b><u>Title</u></b>"),
        id = 'banner'
    )
)

server <- function(input, output) {
    
    shinyjs::addClass(id = "banner", class = "navbar-right")#moves navbar right
    #this next line is the one APPENDING text to the navbar, thanks to "add = TRUE"
    shinyjs::html(id = "banner", html = "<p>companyName</p><p>company@place.com</p>", add = TRUE)

}

# Run the application 
shinyApp(ui = ui, server = server)
相关问题