赞
踩
R Shiny是一种基于Web的交互式数据可视化工具,能够帮助研究人员和临床医生快速构建交互式应用程序,从而进行数据分析和可视化。
在临床决策中,R Shiny可以用于以下方面:
- 数据可视化:医生可以使用R Shiny构建交互式图表和图形,以更好地展示和解释患者的病情和治疗效果。
- 临床预测模型:R Shiny可以帮助医生构建和验证临床预测模型,以便更好地了解患者的风险和预测未来病情的可能性。
- 决策支持系统:R Shiny可以用于构建决策支持系统,帮助医生制定更准确、更个性化的治疗方案。
- 临床试验监管:R Shiny可以用于临床试验监管,帮助研究人员快速掌握数据,监测研究的进展和效果。
那么,结合R强大的数据分析能力,在医学领域Shiny有哪些应用呢?这里给出了介绍。
https://zhuanlan.zhihu.com/p/471281332
1.准备数据(测试集/训练集)
2.建立Logistics回归模型
3.预测指标(AUC)
4.个体预测概率
上述模型的准备是关键,其实Shiny只是可视化的展示网页,并进行交互式的操作。详细案例见:OR与RR的计算及可视化展示
这里不多做介绍,直接看官网链接。
image.png
ChatGPT编程运行的怎么样,我们来看看。
image.png
在这个示例程序中,使用了numericInput
和selectInput
函数创建输入变量,使用actionButton
函数创建计算患病概率的按钮。在Server
端,使用reactive
函数创建数据框data
和逻辑回归模型model
。
image.png
一个大致的界面就完成了,而且出现了一些错误,所以ChatGPT也并不是完美的。
接下来我们将对界面这个进行完成
在空白处增加两个数据输出
跟图像输出
框架,可以借助tabBox
完成。
tabBox(title = "Data Result",height = "500px", width = NULL,
tabPanel("Dataview",h4("we can see the inut data"),
textOutput("text1"),
dataTableOutput("data1"), # 展示录入数据
dataTableOutput("data2"),
textOutput("prediction_text")
), # 展示模型输出
tabPanel("Plotfigure", h4("Here output the AUC"),
textOutput("text2"),
verbatimTextOutput("Data_Management_Normal"),
plotOutput("plot1"))
)
这样就可以自由切换。
image.png
这里贴上,其人网站的优化UI,给大家做扩展,以后可以按照这些来设计。
image.png
image.png
这里附上源代码:
library(shiny) library(ggplot2) library(pROC) library(DT) library(tidyverse) library(shinydashboard) library(shinydashboardPlus) library(shinyWidgets) # 产生fake数据 set.seed(123) # for reproducibility # create a data frame with patient data patients <- data.frame( age = round(rnorm(500, 50, 10)), # age (normally distributed, mean=50, sd=10) sex = sample(c("M", "F"), 500, replace = TRUE), # sex blood_pressure = round(rnorm(500, 120, 10)), # blood pressure (normally distributed, mean=120, sd=10) cholesterol = round(rnorm(500, 200, 30)), # cholesterol (normally distributed, mean=200, sd=30) blood_sugar = round(rnorm(500, 5, 1)), # blood sugar (normally distributed, mean=5, sd=1) heart_rate = round(rnorm(500, 70, 10)), # heart rate (normally distributed, mean=70, sd=10) disease = sample(c(0, 1), 500, replace = TRUE, prob = c(0.8, 0.2)) # heart disease (20% of patients have the disease) ) test_data<- data.frame( age = round(rnorm(200, 50, 10)), # age (normally distributed, mean=50, sd=10) sex = sample(c("M", "F"), 200, replace = TRUE), # sex blood_pressure = round(rnorm(200, 120, 10)), # blood pressure (normally distributed, mean=120, sd=10) cholesterol = round(rnorm(200, 200, 30)), # cholesterol (normally distributed, mean=200, sd=30) blood_sugar = round(rnorm(200, 5, 1)), # blood sugar (normally distributed, mean=5, sd=1) heart_rate = round(rnorm(200, 70, 10)), # heart rate (normally distributed, mean=70, sd=10) disease = sample(c(0, 1), 200, replace = TRUE, prob = c(0.8, 0.2)) # heart disease (20% of patients have the disease) ) # create a factor variable for sex patients$sex <- factor(patients$sex) # show the first few rows of the dataset head(patients) # fit a logistic regression model modelx <- glm(disease ~ age + sex + blood_pressure + cholesterol + blood_sugar + heart_rate, data = patients, family = "binomial") # show the model summary summary(modelx) library(broom) # convert model output to a tidy data frame model_summary <- tidy(modelx) ## Shiny web ui <- fluidPage( titlePanel("临床预测模型"), sidebarLayout( sidebarPanel( numericInput("age", "年龄(岁):", min = 0, max = 150, value = 50), selectInput("sex", "性别:", choices = c("F", "M")), numericInput("blood_pressure", "血压(mmHg):", min = 0, max = 300, value = 120), numericInput("cholesterol", "胆固醇(mg/dL):", min = 0, max = 500, value = 200), numericInput("blood_sugar", "血糖(mmol/L):", min = 0, max = 50, value = 5), numericInput("heart_rate", "心率(次/分):", min = 0, max = 300, value = 70), actionButton("calculate", "计算患病概率") ), mainPanel( tabBox(title = "Data management",height = "500px", width = NULL, tabPanel("Dataview",h4("we can see the inut data"), textOutput("text1"), dataTableOutput("data1"), # 展示录入数据 dataTableOutput("data2"), textOutput("prediction_text") ), # 展示模型输出 tabPanel("Plotfigure", h4("Here output the AUC"), textOutput("text2"), verbatimTextOutput("Data_Management_Normal"), plotOutput("plot1")) ), ) ) ) server <- function(input, output) { # 定义数据框 datax <- reactive({ data.frame( age = input$age, sex = factor(input$sex), blood_pressure =input$blood_pressure, cholesterol = input$cholesterol, blood_sugar = input$blood_sugar, heart_rate = input$heart_rate) }) # 输出录入数据 output$data1 <- renderDataTable({ datax() }) # 输出LR模型结果 output$data2 <- renderDataTable({ model_summary }) # 计算患病概率 prediction <- eventReactive(input$calculate, { prob <- predict(modelx,newdata = datax(),type = "response") prob }) # 输出患病概率 output$text1 <- renderText({ paste0("该病人的患病概率为:", round(prediction() * 100, 2), "%") #print(str(datax())) }) # 绘制AUC图表 output$plot1 <- renderPlot({ ggplot(mtcars)+ geom_point(aes(mpg,hp)) #验证集 test_data$P1<-predict(modelx,newdata = test_data , type = "response") roc1 <- plot.roc(test_data$disease,test_data$P1,ci=TRUE,print.auc=TRUE,levels = c(0,1), direction='<') plot(roc1, print.auc=TRUE,auc.polygon=TRUE, grid=c(0.2, 0.2), grid.col=c("green", "red"), max.auc.polygon=TRUE,legacy.axes=T, auc.polygon.col="skyblue", print.thres=F,main="mtDNA-CN") }) } shinyApp(ui = ui, server = server)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。