0

In my way to learning Shiny rudiments, I do want to build a simple application that presents a ui with boxes with different background colours based on the value of a variable calculated in server.

I'm trying to use a classical if-statement, being n_add_due_all in server:

fluidRow(
  if(n_add_due_all > 0)
     {box(title = "to add", background = "red")} 
  else
     {box(title = "to add", background = "green")}
)

I've being trying to use renderUI -> uiOutput and renderPrint -> textOutput server/ui pairs, but I'm not able to get a numeric value suitable for the n_add_due_all > 0 evaluation.

Please, is it doable? Is there any way of simply passing a numeric value from server to ui?

I found numerous related question and answers, but all of them seam much complex user-cases where some kind of interaction with user by selecting or entering values is required. In this case, the variable is completely calculated in server and should only be recalculated upon a page reload.

Thanks for your help!

  • 1
    To increase the chances someone can help you, consider taking your program and making a version as small as possible that shows your issue. I imagine this particular question only needs a very simple `ui` and `server` to demonstrate your problem. – kybazzi Jan 08 '22 at 23:57

1 Answers1

0

Are you looking for this?

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  header = dashboardHeader(),
  sidebar = dashboardSidebar(),
  body = dashboardBody(uiOutput("box"))
)



server <- function(input, output, session) {
  n_add_due_all <- -1

  output$box <- renderUI({
    fluidRow(
      if (n_add_due_all > 0) {
        shinydashboard::box(title = "to add", background = "red")
      } else {
        shinydashboard::box(title = "to add", background = "green")
      }
    )
  })
}

shinyApp(ui, server)
jpdugo17
  • 6,816
  • 2
  • 11
  • 23
  • Thanks! I see now that I should move the condition evaluation to `code`. But I'm facing now a new doubt: please, how could I show **mtcars** as generated with `output$mtcars <- renderDT(mtcars)` in any of the boxes generated by the nice code you proposed? – Ricardo Rodríguez Jan 09 '22 at 19:53
  • Found this https://stackoverflow.com/questions/31813601/using-renderdatatable-within-renderui-in-shiny. I think it solves my doubt. I'll elaborate further and report back! – Ricardo Rodríguez Jan 09 '22 at 23:10