R 初学者程序员尝试为我的研究创建一个随机化接口。
我正在使用“SRS”R 包根据预后因素对受试者进行随机分组,使用招募中心和病情严重程度的最小化方法。该研究有 9 个中心、一种病症(2 个 II/III 级),患者将被随机分为两组(“标准”组、“治疗”组)。该脚本运行良好。我现在正在尝试使用函数将 Shiny 连接到 R 脚本。但是,我收到“警告:cat 中的错误:参数 1(类型‘closure’)无法由‘cat’处理”。
我正在使用 RStudio 2023.12.1 和“shiny”版本 1.8.1.1。
library(shiny)
library(SRS)
Severity <- c("II", "III")
Centre <- c("A", "B", "C","D", "E", "F","G","H","I")
expt <- ClinicalExperiment(number.of.factors = 2,
factor.names = c("Centre", "Severity"),
number.of.factor.levels = c(9, 2),
factor.level.names =
list(c(Centre),
c(Severity)),
number.of.treatments = 2,
treatment.names <- c("Standard", "Treatment"))
r.obj <- new("PocockSimonRandomizer", expt, as.integer(12345))
my_function <- function(num,Centre, Severity) {
reactive(randomize(r.obj,input$num, c(input$Centre,input$Severity)))
}
# The ui part
ui <- fluidPage(
numericInput("num", "Study ID", value = 1, min = 1, max = 200),
actionButton("click", "Randomize"),
selectInput("Severity", "What's the stage?",Severity),
selectInput("Centre", "What's the centre?", Centre),
textOutput("text")
)
# Define server
server <- function(input, output) {
result <- eventReactive(input$click, {
my_function(input$num, input$Severity, input$Centre)
})
output$text<- renderText(result())
}
shinyApp(ui = ui, server = server)
我尝试过自己调试并使用互联网作为资源,但找不到如何修复它。
您的函数应该具有您在函数中指定的参数,而不是特定的输入。当您调用该函数时,您可以包含输入。
因此将 my_function 更改为:
my_function <- function(num,Centre, Severity) {
reactive(randomize(r.obj, num, c(Centre,Severity)))
}
然后您可以在 eventReactive 中使用输入进行调用。