Error in Design: dataset dd not found for options(datadist=)

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)
  })
})
)

I dont know the ins and outs of rms but the options(datadist='dd') is a code smell for relying on data in the global environment, which when you are working in shiny is typically avoided (and for good reason). I would hope theres a more sophisticated way to dodge these limitations but a hacky solution would be to change

dd<-datadist(base())

to

 dd<<-datadist(base())

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.