You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在RShiny中使用renderPlot绘制患者评分梯度指示图

适配方案

你只需要将原有绘图逻辑整体放入renderPlot的作用域内即可正常运行,若需要支持动态调整患者评分,还可以将固定的评分值、指示线位置关联Shiny的输入控件,适配后的代码如下:

output$plot <- renderPlot({
  # 加载依赖包
  require(plotrix)
  
  # 生成渐变配色
  greens <- colorRampPalette(c('green','yellow'))(50)
  yellows <- colorRampPalette(c('yellow','orange'))(50)
  oranges <- colorRampPalette(c('orange','red'))(50)
  reds <- colorRampPalette(c('red','darkred'))(60)
  the.colors <- c(greens, yellows, oranges, reds)
  
  # 绘制基准画布
  plot(0,0, type='n', axes=FALSE, xlab='', ylab='', xlim=c(0,210), ylim=c(0,5))
  gradient.rect(0,1,210,2, col=the.colors, gradient='x')
  
  # 标注评分等级
  text(25,0.75, 'None')
  text(75, 0.75, 'Mild')
  text(125, 0.75, 'Moderate')
  text(180, 0.75, 'Severe')
  
  # 患者评分,如需动态取值可替换为对应input变量,例如input$patient_score
  patient_score <- 10
  # 换算指示线位置,可根据实际评分区间调整换算逻辑
  line_pos <- patient_score * 10 + 10
  text(105, 4.5, 'Your score is', cex=4)
  text(105, 3, patient_score, cex=4)
  lines(c(line_pos, line_pos),c(1,2), lwd=3)
})

补充说明

  • 若要实现动态调整评分的效果,可以在Shiny的UI部分添加对应输入控件,例如numericInput("patient_score", "输入患者评分", min = 0, max = 20, value = 10),将代码中固定赋值patient_score <- 10替换为patient_score <- input$patient_score即可
  • 需在UI部分添加plotOutput("plot")承接绘图输出,才能在页面上正常展示该图表

内容的提问来源于stack exchange,提问作者Ethan Arenson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 20:09:04