Here, I tried to design a shiny app for plotting the restricted cubic spline. However, I encountered a problem:
#simulation data for importing
set.seed(1234)
outcome<-rbinom(500,size = 1,prob = 0.5)
a<-rnorm(500,15,3)
b<-rbinom(500,size = 2,prob = 0.4)
temp_data<-data.frame(outcome,a,b)
write.csv(temp_data,file = 'temp_data.csv')
#main program
library(shiny)
library(rms)
library(ggplot2)
shinyApp(
ui = fluidPage(
titlePanel("RCS plot"),
sidebarLayout(
sidebarPanel(
fileInput("file", "Choose CSV File",
multiple = TRUE,
accept = c("text/csv"))
),
mainPanel(
plotOutput("Plot_RCS")
)
)
),
server = shinyServer(function(input,output,session) {
base<-reactive({
req(input$file)
total_data<-read.csv(input$file$datapath,header = T)
})
OR_set<-reactive({
dd<-datadist(base())
options(datadist='dd')
fit<-lrm(outcome~rcs(a,3)+b,data = base())
dd$limits$a[2]<-4
fit<-update(fit)
OR<-Predict(fit,a,fun=exp,ref.zero=T)
})
output$Plot_RCS <- renderPlot({
ggplot()+geom_line(data = OR_set(), aes(a,yhat,color='black'), linetype=1, size=0.8, alpha=0.8)+
geom_ribbon(data = OR_set(),aes(a,ymin=lower, ymax=upper,fill="grey"),alpha=0.2)
})
})
)