I would like to set the format of an expression depending on the input. E.g. when it is a negative value I want it to be red. There are probably multiple ways to achieve this. I tried to create an ifelse statement in the UI part and depending on the condition I would display the value with the desired styling. However, the if-condition does not work, because I seem not be able to access the actual value (see substr(kpiModuleUI_Test("calledName")[2],1,25), what I get when I want to look inside the expression).
How can I access the reactive value in the UI? Do you know a better way than to do logical operations in the UI in order to have conditional formatting on reactive expressions?
Reproducible example Main file:
packages <- c( "data.table","ggthemes","ggExtra","grid","gridExtra","extrafont","stringi","plyr","dplyr","reshape2","shiny","shinydashboard","shinythemes","shinyjs","stats","plotly","ggplot2","lattice","cowplot","lubridate","rstudioapi","zoo")
for (i in packages){
if (!is.element(i,installed.packages()[,1])) {
install.packages(i,dependencies = TRUE)
}
}
lapply(packages, require, character.only = TRUE)
# Set directory to file location
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
getwd()
source("Modules_Test_1.R")
server <- function(input,output,session) {
val1<-reactive({input$testinput})
callModule(kpiModule_Test,"calledName",val1)
}
header<-dashboardHeader(title = "Module Test",titleWidth = 280)
sidebar<-dashboardSidebar(width = 280,sidebarMenu(id="sidebar_tabs",
menuItem("AAA", tabName = "AAA"),
menuItem("BBB", tabName = "BBB")))
body<-dashboardBody(title="Main",
tabItem(tabName = "Overview",h1("Overview"),
fluidPage(
box(sliderInput(inputId = "testinput",label="testinput",min=1,max=20,value=5)),
box(title="KPIs",tags$p(kpiModuleUI_Test("calledName")[2],style="color:#ff5733"),br(),
class(kpiModuleUI_Test("calledName")[2]),br(),
substr(kpiModuleUI_Test("calledName")[2],1,25))
# ,
# box(title="KPIs",if(kpiModuleUI_Test("calledName")[2]>20){tags$p(kpiModuleUI_Test("calledName")[2],style="color:#ff5733")}
# else{tags$p(kpiModuleUI_Test("calledName")[2],style="color:#1E90FF")})
)
)
)
sdb_ui <- dashboardPage(skin = "black",
header,
sidebar,
body
)
shinyApp(ui = sdb_ui, server = server)
File with modules ():
kpiModule_Test <- function(input, output, session,show1) {
output$kpi1a <- renderText({show1()})
output$kpi1b <- renderText({(show1()+20)})
}
kpiModuleUI_Test <- function(id) {
# Create a namespace function using the provided id
ns <- NS(id)
tagList(
textOutput(ns("kpi1a"),inline=TRUE),
textOutput(ns("kpi1b"),inline=TRUE)
)
}
So far I could not find this problem on Stackoverflow. The closest threads cover formatting in tables. In my real problem the reactive value is not a simple value anymore (-5$). That's why I tried to extract the first character with substr() in order to create a condition.
Your help is very much appreciated!