1

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)
Florian
  • 24,425
  • 4
  • 49
  • 80
shoo
  • 87
  • 1
  • 11

1 Answers1

2

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)
Florian
  • 24,425
  • 4
  • 49
  • 80
  • ,thanks so much for the suggestion, I run the code with your first suggestion but I got an error "Objekt 'lower' nicht gefunden". I edited the code based on your suggestion. could you please run the code to see that you will get the same error or not? – shoo Feb 11 '18 at 08:05
  • @ I defined lower and upper in reactive section and also in renderPrint section. Did I make a mistake? – shoo Feb 11 '18 at 08:19
  • I think I should not define lower and upper in reactive function because in this section the data has been defined based on imported variables and the lower and upper are not variables and are just the numbers. server<-function(input,output) { data <- reactive({ lower <- input$range[1] upper <- input$range[2] file1 <- input$file , should I defind a seperate reactive function for lower and upper? – shoo Feb 11 '18 at 08:49
  • I updated my answer. Note that it returns some new errors. If you keep encountering issues, I strongly recommend you to strip down your app to the bare minimum, and create a new question on Stack Overflow regarding your errors. See also [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and [here](https://stackoverflow.com/questions/48343080/how-to-convert-a-shiny-app-consisting-of-multiple-files-into-an-easily-shareable/48343110#48343110). – Florian Feb 11 '18 at 11:21