我有一个shiny 的应用程序,其中我希望当值为负时箭头变成红色,当值为正时箭头变成绿色,如果值为0,箭头就会变成黑色.

library(shiny)
library(shinydashboard)
library(fontawesome)
# Define UI 
ui <- fluidPage(
  
  # Application title
  titlePanel("Numeric Input App"),
  
  # Sidebar layout with input and output definitions
  sidebarLayout(
    
    # Sidebar panel for inputs
    sidebarPanel(
      numericInput("number", "Enter a number:", value = 0)  # Numeric input field
    ),
    
    # Main panel for displaying output
    mainPanel(
      box(
        width=8,title = "ABT",status="primary",solidHeader = T,
        fluidRow(
          column(12,textOutput("output"),
                 column(2,fa("arrow-trend-up", fill = "forestgreen"))
          )
          
        ))
    )
  )
)

# Define server logic
server <- function(input, output) {
  
  # Function to render the output based on user input
  output$output <- renderText({
    paste("You entered:", input$number)  # Display the entered number
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

推荐答案

试试这个

library(shiny)
library(shinydashboard)
library(fontawesome)
# Define UI 
ui <- fluidPage(
  
  # Application title
  titlePanel("Numeric Input App"),
  
  # Sidebar layout with input and output definitions
  sidebarLayout(
    
    # Sidebar panel for inputs
    sidebarPanel(
      numericInput("number", "Enter a number:", value = 0)  # Numeric input field
    ),
    
    # Main panel for displaying output
    mainPanel(
      box(
        width=8,title = "ABT",status="primary",solidHeader = T,
        fluidRow(
          column(12,textOutput("output"),
                 column(2,uiOutput("myfa"))
          )
          
        ))
    )
  )
)

# Define server logic
server <- function(input, output) {
  
  # Function to render the output based on user input
  output$output <- renderText({
    paste("You entered:", input$number)  # Display the entered number
  })
  
  output$myfa <- renderUI({
    if (input$number >0) {
      mycolor = "forestgreen"
      arw = "arrow-trend-up"
    } else if(input$number == 0) {
      mycolor = "black"
      arw = "arrow-right"
    }else {
      mycolor = "red"
      arw = "arrow-trend-down"
    }
    fa(arw, fill = mycolor)
  })
  
}

# Run the application 
shinyApp(ui = ui, server = server)

R相关问答推荐

棒棒糖图表大小和线宽参数故障标签未出现

导入到固定列宽的R中时出现问题

在通过最大似然估计将ODE模型与数据匹配时,为什么要匹配实际参数的转换值?

根据模式将一列拆分为多列,并在R中进行拆分

用相同方法得到不同函数的ROC最优截断值

将小数分隔符放在R中的前两位数字之后

从一个列表的框架中移除列表包装器

解析R函数中的变量时出现的问题

在保留列表元素属性的同时替换列表元素

为左表中的所有行使用值Fill滚动左连接

自动STAT_SUMMARY统计与手动标准误差之间的差异

提高圣彼得堡模拟的速度

优化从每个面的栅格中提取值

为什么我对圆周率图的蒙特卡罗估计是空的?

有没有办法定制Plot(allEffects())面板标题?

如何预测原始数据集并将值添加到原始数据集中

R将函数参数传递给ggploy

Broom.Mixed::Augment不适用于Sample::分析

重写时间间隔模糊连接以减少内存消耗

R/shiny APP:如何充分利用窗口?