Shiny dashboard - title of a box to be a value extract from selected row of data table

Hello,

I am very new about asking questions in R so I hope I would be able to make myself understandable.

I am starting to use shinydashboard to develop a web application.

The main body of my dashboard is made of 2 parts:

  1. a DT which has been obtained by filtering an initial DF (called 'df') thanks to some radioButtons available in the sidebar of the dashboard.

  2. a box almost empty at the moment.

The user is allowed to select only one row of the DT. I would like to extract a value from the selected row and let it be the title of the box. I want the font to be the same as it would have been by writing simply title = 'Title' in box().

You will find the current code here below.
I succeeded to have the title of the box getting the right value.
However, I have a [1] before the value and the font is horrible and does not look like a title at all.

Any help please?

body <- dashboardBody(
sidebarLayout(
sidebarPanel(dataTableOutput("table"),width = 4),
mainPanel(
box(title = textOutput('product'), solidHeader = TRUE,status="primary", width = 12,
actionButton("Price", label="Price this product!")))
)
)

server = function(input, output) {
output$table <- DT::renderDataTable(df[df$Asset.Class==input$Asset_Class & df$Assets==input$Assets & df$Class==input$Class,c('Category','Product')],options=list(pageLength=10),selection='single')

output$product = renderPrint({
prod = input$table_rows_selected

product_name <- df[df$Asset.Class==input$Asset_Class & df$Assets==input$Assets & df$Class==input$Class,'Product']
product_name = product_name[prod]

if (length(prod)) {product_name}

})

}

Hi,

I don't know if this is actually how Shiny intends to update such values as box titles, but your experiment actually works using an output element for the title! That's cool ... I would have never thought of that :slight_smile:

With that in mind, I was able to alter the idea a bit and make you put in any type of HTML code there reactive to any variable. See my dummy example:

library(shiny)
library(shinydashboard)


ui <- dashboardPage(
 dashboardHeader(),
 dashboardSidebar(),
 dashboardBody(
   sidebarLayout(
     sidebarPanel(dataTableOutput("table"),width = 4),
     mainPanel(
       box(title = uiOutput('product'), solidHeader = TRUE,status="primary", width = 12,
           actionButton("Price", label="Price this product!")))
   )
 )
) 
                
server <- function(input, output, session) {

 output$product = renderPrint({
   HTML(cat("<font color='orange'>Times clicked:", input$Price), "</font>")
 })
                
}
shinyApp(ui, server)

Tip: the 'cat' function prevents the [1] ... output format that 'paste' gives

Hope this helps!
PJ

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.