如何将一段R代码转换为闪亮的应用程序?



一个新的闪亮应用自学教程,仍在尝试探索闪亮应用的结构。我有一段代码,我想转换成闪亮的应用程序。我想知道有没有人能告诉我这个过程是怎么进行的。我的目标是根据输入(即lnHR0, p_ctl0, tfu)使图标题/副标题动态运行。谢谢! !

R代码:

library(tidyverse)
library(ggplot2)
WR_sim_OC <- function(n_trt, n_ctl, lnHR0, p_trt0, med_ctl, tfu, enroll, dur_boost){
#n_trt=60: number of subjects (treatment)
#n_ctl=30: number of subjects (control)
#lnHR0=log(1): Overall Survival Log Hazard Ratio
#p_trt0 = seq(0.46, 0.66, 0.01): Response Rate (Treatment)
#med_ctl=21.8: Median Overall Survival (Control)
#tfu=9: Minimum Follow-up Time
#enroll=19: Enrollment Time
#dur_boost=0: Durability Boost (percentage)

## insert real simulation code here ##

## Fake Results
results <- tibble(ORR_trt = p_trt0, avg_HR = rep(0.774, times = 4), maturity = rep(36.7, times = 4), ORR = c(20.71, 38.87, 60.61, 78.95), 
WR = c(46.8, 56.0, 64.3, 72.8), WO = c(46.0, 55.0, 63.7, 71.9), OS = rep(55.0, times = 4))
## Create Operating Characteristic Figure
dat <- results %>% pivot_longer(c(ORR, WR, WO, OS), names_to = 'Method', values_to = 'POS') 

out1 <- ggplot(data = dat, aes(x = ORR_trt, y = POS, group = Method)) +
geom_line(aes(color = Method), size = 1) +
geom_point(aes(color = Method), size = 2.5) + 
theme(legend.position = 'bottom') + 
labs(title = 'HR=1.0 Treatment vs. Control', subtitle = 'ORR in Control Arm=46%, 9mo follow-up', color = 'Method') + 
ylab('Probability of Incorrect Go') + 
xlab('ORR in Treatment Arm') +
ylim(0, 100)

## Output OC Table + Figure to shiny app
list(results, out1)
}
WR_sim_OC(n_trt=60, n_ctl=30, lnHR0=log(1), p_ctl0=0.46, p_trt0 = seq(0.46, 0.66, 0.06), med_ctl=21.8, tfu=9, enroll=19, dur_boost=0)

我试着写ui。R如下(假设suv_plot是输出名称),我知道这是错误的。服务器。R部分对我来说太难了…有人能帮忙吗?

fluidPage(
numericInput("lnHRO", 
label = h3("ln(HRO)"), 
value = log(1)),
numericInput("pctl", 
label = h3("Response Rate (Control)"), 
value = 0.46),
numericInput("tfu", 
label = h3("Minimum Follow-up Time (Month)"), 
value = 9),
hr(),
plotOutput("suv_plot")

)

我的第一个建议是看看shiny的教程,他们对如何开始一个项目给出了很好的概述:https://shiny.rstudio.com/tutorial/

几年前我对编程一无所知,所以我知道很难弄清楚从哪里开始,所以我想给你一个如何实现一个函数的想法,并使用闪亮的输入使结果表/图是动态的。

我修改了你的代码,以便更容易为我自己复制。我希望这能给你一个你需要的起点:

library(tidyverse)
library(ggplot2)
library(shiny)
WR_sim_OC <- function(MPG, CYL, DISP){

results <- mtcars%>% #Function to make a table
filter(cyl > CYL,
mpg > MPG,
disp > DISP)

out1 <- ggplot(data = results, aes(x = mpg, y = disp, group = cyl)) +
geom_line(aes(color = hp), size = 1) #Function to make a plot

list(results, out1) #List to create table and function
}

ui <- fluidPage(
numericInput("MilesPerGallon", "mpg", value = 15),
numericInput("Cylinders", "cyl", value = 4),
numericInput("Displacement", "disp", value = 200),
tableOutput("TABLE"),
plotOutput("PLOT")
)
server <- function(input, output, session) {
output$TABLE<-renderTable({
req(input$MilesPerGallon, input$Cylinders, input$Displacement) #Requires all three inputs before it makes the table

WR_sim_OC(input$MilesPerGallon, input$Cylinders, input$Displacement)[1] #Only pulling the table from the function

})

output$PLOT<-renderPlot({
req(input$MilesPerGallon, input$Cylinders, input$Displacement) #Requires all three inputs before it makes the plot

WR_sim_OC(input$MilesPerGallon, input$Cylinders, input$Displacement)[2] #Only pulling the plot from the function
})
}
shinyApp(ui, server)

基本上在呈现图表或表格的服务器端,您使用ui中的这些输入作为函数中的动态点。我对renderTable和renderPlot都使用了req(),以确保在表绘制之前填写输入。祝你好运!

相关内容

  • 没有找到相关文章

最新更新