【问题标题】:defining sliderInput function in Shiny app在 Shiny 应用程序中定义 sliderInput 函数
【发布时间】:2018-07-21 11:44:55
【问题描述】:

我在下面的代码中添加了一个函数sliderInput来定义smooth参数读取这个函数的范围

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

为了做到这一点,我定义了

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

reactiverenderPrint 部分但我有一个错误,我不知道我在ui 部分或服务器部分中哪里出错了。我用了 sliderInput("range", "Smooth Parameter range:",min = 0, max = 1000, value = c(0,1000))) 定义范围。我有这个错误找不到对象'下'。有什么建议吗?

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)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    这与范围界定有关。您在 reactive 函数中定义了 lowerupper,但它们没有在函数之外定义!

    你可以简单地添加

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

    在您的 RenderPrint 语句中,因此变量也在那里定义。

    另外,请注意,您的公式包含文本 lower 和 upper,而不是实际数字。您可以通过以下方式解决:

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

    我从shinyjs 中添加了一些logjs 语句,以便我们可以看到新公式和旧公式。

      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)
    

    以下是解决您错误的代码,虽然它为我返回了新错误,但我们没有您的数据。您可能希望在没有闪亮的情况下使其正常工作。希望这可以帮助!


    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)
    

    【讨论】:

    • ,非常感谢您的建议,我使用您的第一个建议运行代码,但出现错误“Objekt 'lower' nicht gefunden”。我根据您的建议编辑了代码。你能运行一下代码看看你会不会得到同样的错误?
    • @ 我在反应部分和 renderPrint 部分定义了 lower 和 upper。我是不是搞错了?
    • 我认为我不应该在反应函数中定义lower和upper,因为在本节中,数据是基于导入的变量定义的,lower和upper不是变量,只是数字。 server
    • 我更新了我的答案。请注意,它会返回一些新错误。如果您一直遇到问题,我强烈建议您将您的应用程序精简到最低限度,并在 Stack Overflow 上创建一个关于您的错误的新问题。另见herehere
    猜你喜欢
    • 2022-01-22
    • 2016-05-17
    • 2017-03-25
    • 2017-04-13
    • 2023-01-31
    • 2018-07-08
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多