1
votes

I added a function sliderInput into the following code to define a range for the smooth parameter to read this function

after_tilde <- paste0("ps(", dep_vars, ", lambda = seq(lower,upper,l=20))")

in order to do that I define

lower <- input$range[1]
upper <- input$range[2]

in reactive and renderPrint sections But I have an error and I donot know where I made a mistake in ui section or server section. I used sliderInput("range", "Smooth Parameter range:",min = 0, max = 1000, value = c(0,1000))) to defind the range. I have this error object 'lower' not found. Any advice?

library(shiny)
library(quantreg)
library(quantregGrowth)


ui = tagList(
  tags$head(tags$style(HTML("body{ background: aliceblue; }"))),
  navbarPage(title="",
             tabPanel("Data Import",
                      sidebarLayout(sidebarPanel( fileInput("file","Upload your CSV",multiple = FALSE),
                                                  tags$hr(),
                                                  h5(helpText("Select the read.table parameters below")),
                                                  checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
                                                  checkboxInput(inputId = "stringAsFactors", "StringAsFactors", FALSE),
                                                  radioButtons(inputId = 'sep', label = 'Separator', 
                                                               choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
                      ),
                      mainPanel(uiOutput("tb1"))
                      ) ),
             tabPanel("95% Continious Reference Intervals",
                      sidebarLayout(sidebarPanel(
                        uiOutput("model_select"),
                        uiOutput("var1_select"),
                        uiOutput("rest_var_select"),                        
           sliderInput("range", "Smooth Parameter range:",min = 0, max = 1000, value = c(0,1000))
                                                ),
                        mainPanel( helpText("Selected variables and Fitted values"),
                                   verbatimTextOutput("other_val_show")))),
             tabPanel("Model Summary", verbatimTextOutput("summary")), 
             tabPanel("Scatterplot", plotOutput("scatterplot"))#, # Plot             
             ,inverse = TRUE,position="static-top",theme ="bootstrap.css"))

server<-function(input,output) { data <- reactive({
  lower <- input$range[1]
  upper <- input$range[2]
  file1 <- input$file
  if(is.null(file1)){return()} 
  read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)

})  

output$table <- renderTable({
  if(is.null(data())){return ()}
  data()
})
output$tb1 <- renderUI({
  tableOutput("table")
})
#output$model_select<-renderUI({
#selectInput("modelselect","Select Algo",choices = c("Reference Interval"="Model")) #Logistic_reg
#})
output$var1_select<-renderUI({
  selectInput("ind_var_select","Select Independent Variable", choices =as.list(names(data())),multiple = FALSE)
})
output$rest_var_select<-renderUI({
  checkboxGroupInput("other_var_select","Select Dependent Variable",choices =as.list(names(data()))) #Select other Var
})

output$other_val_show<-renderPrint({
   lower <- input$range[1]
  upper <- input$range[2]
  input$other_var_select
  input$ind_var_select
  f<-data()
  library(caret)
  library(quantregGrowth)
  dep_vars    <- paste0(input$ind_var_select, collapse = "+")
  after_tilde <- paste0("ps(", dep_vars, ", lambda = seq(lower,upper,l=20))")
  dyn_string  <- paste0(input$other_var_select, " ~ ", after_tilde)
  Model<-quantregGrowth::gcrq(as.formula(dyn_string),tau=c(0.025,0.975), data=f)
  temp <- data.frame(Model$fitted)
  growthData_b <- cbind(f, temp)
  print(growthData_b)
})


}
shinyApp(ui=ui,server=server)
1

1 Answers

2
votes

This has to do with scoping. You define lower and upper inside a reactive function, but they are not defined outside of it!

You could simply add

  lower <- input$range[1]
  upper <- input$range[2]

in your RenderPrint statement, so the variables are also defined there.

Also, note that your formula contains the text lower and upper, instead of the actual numbers. You can solve that with:

after_tilde <- paste0("ps(", dep_vars, ", lambda = seq(",lower,",",upper,",l=20))")

I added some logjs statements from shinyjs so we can see both the new and the old formula.

  after_tilde <- paste0("ps(", dep_vars, ", lambda = seq(",lower,",",upper,",l=20))")
  old_formula <- paste0("ps(", dep_vars, ", lambda = seq(lower,upper,l=20))")
  dyn_string  <- paste0(input$other_var_select, " ~ ", after_tilde)
  logjs(after_tilde)
  logjs(old_formula)

enter image description here

Below is code that solves you error, although it returns new ones for me, but we do not have your data. You may want to get this working without shiny first. Hope this helps!


library(shiny)
library(quantreg)
library(quantregGrowth)
library(shinyjs)


ui = tagList(
  tags$head(tags$style(HTML("body{ background: aliceblue; }"))),
  navbarPage(title="",
             tabPanel("Data Import",
                      sidebarLayout(sidebarPanel( fileInput("file","Upload your CSV",multiple = FALSE),
                                                  tags$hr(),
                                                  h5(helpText("Select the read.table parameters below")),
                                                  checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
                                                  checkboxInput(inputId = "stringAsFactors", "StringAsFactors", FALSE),
                                                  radioButtons(inputId = 'sep', label = 'Separator', 
                                                               choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
                      ),
                      mainPanel(uiOutput("tb1"))
                      ) ),
             tabPanel("95% Continious Reference Intervals",
                      sidebarLayout(sidebarPanel(
                        uiOutput("model_select"),
                        uiOutput("var1_select"),
                        uiOutput("rest_var_select"),                        
                        sliderInput("range", "Smooth Parameter range:",min = 0, max = 1000, value = c(0,1000))
                      ),
                      mainPanel( helpText("Selected variables and Fitted values"),
                                 verbatimTextOutput("other_val_show")))),
             tabPanel("Model Summary", verbatimTextOutput("summary")), 
             tabPanel("Scatterplot", plotOutput("scatterplot"))#, # Plot             
             ,inverse = TRUE,position="static-top",theme ="bootstrap.css"),
  useShinyjs())

server<-function(input,output) { data <- reactive({
  lower <- input$range[1]
  upper <- input$range[2]
  file1 <- input$file
  if(is.null(file1)){return()} 
  read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)

})  

output$table <- renderTable({
  if(is.null(data())){return ()}
  data()
})
output$tb1 <- renderUI({
  tableOutput("table")
})
#output$model_select<-renderUI({
#selectInput("modelselect","Select Algo",choices = c("Reference Interval"="Model")) #Logistic_reg
#})
output$var1_select<-renderUI({
  selectInput("ind_var_select","Select Independent Variable", choices =as.list(names(data())),multiple = FALSE)
})
output$rest_var_select<-renderUI({
  checkboxGroupInput("other_var_select","Select Dependent Variable",choices =as.list(names(data()))) #Select other Var
})

output$other_val_show<-renderPrint({
  lower <- input$range[1]
  upper <- input$range[2]
  input$other_var_select
  input$ind_var_select
  f<-data()
  library(caret)
  library(quantregGrowth)
  dep_vars    <- paste0(input$ind_var_select, collapse = "+")
  after_tilde <- paste0("ps(", dep_vars, ", lambda = seq(",lower,",",upper,",l=20))")
  old_formula <- paste0("ps(", dep_vars, ", lambda = seq(lower,upper,l=20))")
  dyn_string  <- paste0(input$other_var_select, " ~ ", after_tilde)
  logjs(after_tilde)
  logjs(old_formula)
  Model<-quantregGrowth::gcrq(as.formula(dyn_string),tau=c(0.025,0.975), data=f)
  temp <- data.frame(Model$fitted)
  growthData_b <- cbind(f, temp)
  print(growthData_b)
})


}
shinyApp(ui=ui,server=server)