Hello, recently I had the question about the usage of updateTextInput(). The whole codes are shown as follows:
I want to achieve the goal (maybe a little confusion), that is, when I load the interested dataset, the corresponding contents would be returned by the updateSelectInput(), and I can choose the specific variables for next steps. Then I want to return the scale of selected variable by updateTextInput() and modify the value to better control the scale of KM plot. But I also get Error, anyone could help?
library(shiny)
library(data.table)
library(survival)
library(survminer)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
#file reading
fileInput("file", "Choose CSV File:",accept = c(".csv")),
fluidRow(
column(4,selectInput(inputId = "survival_time","Survival time",choices = NULL)),
column(4,selectInput(inputId = "outcome","Outcome",choices = NULL)),
column(4,selectInput(inputId = 'group','Group_var',choices = NULL))
),
fluidRow(
column(6,textInput(inputId = "bin9",label="x_distance",value=15)),
column(6,textInput(inputId = "bin10",label="intervals of x",value=1))
),
actionButton('change',label = 'update',icon = icon('arrow-alt-circle-up'))
),
# Show a plot of the generated distribution
mainPanel(tabsetPanel(tabPanel('Kaplan Meier',plotOutput("distPlot"))))
))
server<-function(input, output,session) {
options(shiny.maxRequestSize=60*1024^2)
#reading data
base<-reactive({
req(input$file)
dat<-fread(input$file$datapath)
dat<-setDF(dat)
})
data1<-reactive({
data1<-base()[,c(input$survival_time,input$outcome,input$group)]
colnames(data1)<-c("time","outcome","group")
data1<-data1
})
#return the parameters
observe({updateSelectInput(session,inputId = 'survival_time',choices = colnames(base()))})
observe({updateSelectInput(session,inputId = 'outcome',choices = colnames(base()))})
observe({updateSelectInput(session,inputId = 'group',choices = colnames(base()))})
observe({updateTextInput(session,inputId = 'bin9',value = max(data1()[,1]))})
observe({updateTextInput(session,inputId = 'bin10',value = round(max(data1()[,1]))/20)})
#fitting model
fit_model<-reactive({
fit <- survfit(Surv(time, outcome==1) ~ group, data = data1())
})
#plotting
KM_plot<-eventReactive(input$change,{
ggsurvplot(fit_model(),data = data1(),
conf.int = T,
pval = T,pval.size=5,
pval.method = T,pval.method.size=5,
censor.size=2,
add.all = F, #add total survival curve
palette = 'npg',#select color pattern
legend.title = "",
font.legend=c(13,'plain','black'),
linetype = 2,
xlim=c(0,as.numeric(input$bin9)),
break.time.by=as.numeric(input$bin10))
})
#show plot
output$distPlot <- renderPlot({
KM_plot()
})
}
shinyApp(ui, server)