简体   繁体   中英

Use string from server.R as argument in a function within ui.r

I'm using shinydashboardPlus() to include a timeline in an app I'm developing. I want each timelineItem() icon to change colour depending on whether a stage is marked as complete. When a stage is incomplete, I would like the icon to be grey. When a checkboxInput() is selected, I would like the colour to change to olive.

I have written the server-side logic such that when checkboxInput is FALSE the string 'grey' is returned but when TRUE the string 'olive' is returned. I need to pass this string to the argument color in timelineItem() . I have tried passing the string to the argument using textOutput() but this doesn't work. Any ideas how I can pass the correct colour string to color ?

Here's an MRE:

library(shiny)
library(shinyWidgets)
library(shinydashboard)
library(shinydashboardPlus)


ui <- dashboardPagePlus(

  header = dashboardHeaderPlus(title = "Quality & Assurance Dashboard"),
  sidebar = dashboardSidebar(


  ),

  body = dashboardBody(

    fluidRow(

      box(width = 9,

          title = "Assurance Timeline",
          status = "info",

          timelineBlock(
            timelineEnd(color = "danger"),
            timelineLabel("Start", color = "teal"),
            timelineItem(
              title = "Stage 1",
              icon = "gears",
              color = textOutput("survey_released_colour"), # Need to paste the correct colour string here
              time = "now",
              footer = "",

              textOutput("survey_released_colour")

            )

          )

      ),

      box(width = 3, 

          title = "Stage Sign-Off",
          status = "info",

          timelineBlock(

            timelineEnd(color = "danger"),
            timelineLabel("Start", color = "teal"),

            timelineItem(
              title = "Stage 1",
              icon = "gears",
              color = "olive",
              time = "",
              footer = "",

              "Check here when Stage 1 complete.",
              checkboxInput(inputId = "survey_release", "Surveys Released", value = FALSE, width = NULL)

            )
          )
      )
    )

  ),

)

server <- function(input, output) { 


  output$survey_released_colour<-renderText({

    if (input$survey_release == TRUE){

      paste0("olive")

    }

    else

      paste0("grey")


  })


}


app<-shinyApp(ui = ui, server = server)
runApp(app, host="0.0.0.0",port=5050, launch.browser = TRUE)




from the basic rules of Shiny you can't use any server component inside ui.R. You can use an condition for changing the color in server side. My try:

library(shinydashboardPlus)

ui <- dashboardPagePlus(

  header = dashboardHeaderPlus(title = "Quality & Assurance Dashboard"),
  sidebar = dashboardSidebar(
  ),

  body = dashboardBody(
    fluidRow(
      box(width = 9,

          title = "Assurance Timeline",
          status = "info",
          uiOutput("timeline")
      ),

      box(width = 3, 
          title = "Stage Sign-Off",
          status = "info",
          checkboxInput(inputId = "survey_release", "Surveys Released", value = FALSE, width = NULL)
      )
    )
  )
)

server <- function(input, output) { 
output$timeline<-renderUI({
  if (input$survey_release == TRUE)
  {
    timelineBlock(
      timelineEnd(color = "danger"),
      timelineLabel("Start", color = "teal"),
      timelineItem(
        title = "Stage 1",
        icon = "gears",
        #color = textOutput("survey_released_colour"), # Need to paste the correct colour string here
        color ='red',
        time = "now",
        footer = ""
      )
    )
  }
  else
  {
    timelineBlock(
      timelineEnd(color = "danger"),
      timelineLabel("Start", color = "teal"),
      timelineItem(
        title = "Stage 1",
        icon = "gears",
        #color = textOutput("survey_released_colour"), # Need to paste the correct colour string here
        color ="green",
        time = "now",
        footer = ""
      )
    )
  }
})  

}


shinyApp(ui, server) 

let me know if this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM