如何修复R Shiny中的“错误:可变长度不同(为'input $ s'找到)" [英] how to fix 'Error: variable lengths differ (found for 'input$s')' in R Shiny
问题描述
我正在尝试制作一个简单的闪亮ap,以创建kaplan-meier生存曲线,并根据用户做出的选择对其进行分层.当我对KM计算进行静态编码(列名称为thorTr)时,它可以工作,但计算和绘图是静态的.当我用input $ s替换时,出现错误:可变长度不同(为"input $ s"找到)
I'm trying to make a simple shiny ap for creating kaplan-meier survival curves that are stratified by selection the user makes. When I code the KM calculation statically (with the column name thorTr) it works but the calculation and plot is static. When I replace with input$s I get ERROR:variable lengths differ (found for 'input$s')
我尝试查看其他使用as.formula并粘贴的代码,但我不理解,无法正常工作.但是我是R and Shiny的新用户,所以也许我做对了.这是一个类似的闪亮ap,但我想使用survminer和ggsurvplot进行绘图
I've tried looking at other code which use as.formula and paste, but I don't understand and couldn't get to work. But I am a new R and Shiny user so maybe I didn't get it right. Here is a similar shiny ap but I want to use survminer and the ggsurvplot for plotting
library(shiny)
library(ggplot2)
library(survival)
library(survminer)
#load data
data(GBSG2, package = "TH.data")
#Define UI for application that plots stratified km curves
ui <- fluidPage(
# Sidebar layout with a input and output definitions
sidebarLayout(
# Inputs
sidebarPanel(
# Select variable strat
selectInput(inputId = "s",
label = "Select Stratification Variable:",
choices = c("horTh","menostat","tgrade"),
selected = "horTh")
),
# Outputs
mainPanel(
plotOutput(outputId = "km")
)
)
)
# Define server function required to create the km plot
server <- function(input, output) {
# Create the km plot object the plotOutput function is expecting
output$km <- renderPlot({
#calc KM estimate with a hard coded variables - the following line works but obviously is not reactive
#km <- survfit(Surv(time,cens) ~ horTh,data=GBSG2)
#replaced hard coded horTh selection with the respnse from the selection and I get an error
km <- survfit(Surv(time,cens) ~ input$s ,data=GBSG2)
#plot km
ggsurvplot(km)
})
}
# Create a Shiny app object
shinyApp(ui = ui, server = server)
我希望有一个可以根据用户选择来更新分层变量的图
I expect to have a plot that updates the stratification variable with the users selection
推荐答案
两件事:
-
survfit()
调用中的公式需要明确定义.原始代码中传递给survfit()
的对象使用函数右侧的字符值.这会引发错误,我们可以通过将整个粘贴的值转换为公式即as.formula(paste('Surv(time,cens) ~',input$s))
来解决.
- 需要在对
ggsurvplot()
的调用中定义公式,以避免范围问题.这有点技术性,并且与ggsurvplot()
的编程方式有关.基本上,ggsurvplot()
不能访问在其自身调用之外定义的公式.
- The formula in the call to
survfit()
needs to be defined explicitly. The object being passed tosurvfit()
in the original code uses a character value on the right hand side of the function. This throws an error, which we can address by translating the entire pasted value into a formula, i.e.,as.formula(paste('Surv(time,cens) ~',input$s))
- The formula needs to be defined in the call to
ggsurvplot()
to avoid scoping issues. This is a little more technical and has to do with the way thatggsurvplot()
is programmed. Basically,ggsurvplot()
can't access a formula that is defined outside of its own call.
尝试更换
km <- survfit(Surv(time,cens) ~ input$s ,data=GBSG2)
ggsurvplot(km)
与
ggsurvplot(survfit(as.formula(paste('Surv(time,cens) ~',input$s)),data=GBSG2))
这篇关于如何修复R Shiny中的“错误:可变长度不同(为'input $ s'找到)"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!