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

How to convert a piece of R code into Shiny apps?

Shiny app自学新人,还在探索Shiny app的结构。我有一段代码想转换成闪亮的应用程序。我想知道是否有人可以指导我完成整个过程。我的目标是根据输入(即 lnHR0、p_ctl0、tfu)使图形 title/subtitle 动态运行。谢谢!!!

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 是输出名称),我知道这是错误的。 server.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/

几年前我对编程一无所知,所以我知道很难弄清楚从哪里开始,所以我想告诉你如何实现一个功能,并使用 shiny输入以使结果 table/plot 成为动态的。

我切换了你的代码,以便更容易为我自己重现。我希望这能为您提供所需的起点:

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)

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