简体   繁体   English

Shiny 中滑块的动态数量

[英]Dynamic number of sliders in Shiny

I am trying to create an application to do exploratory analysis of simulation results datasets that are always in the same (csv) format: first column with run number, several columns that contain the input parameters, one column with the timestep and then several columns that contain the values of interest.我正在尝试创建一个应用程序来对始终采用相同 (csv) 格式的模拟结果数据集进行探索性分析:第一列包含运行编号,几列包含输入参数,一列包含时间步长,然后几列包含感兴趣的值。 The number of input parameters and output values change but the column names that separate these sections are always the same.输入参数和输出值的数量会发生变化,但分隔这些部分的列名称始终相同。

Typical data looks like:典型数据如下:

[run number],capital,weekly,[step],report1
1,10000,100,0,0
1,10000,100,1,2
1,10000,100,2,3
1,10000,100,3,3

I want the user to be able to select a subset of simulation runs to analyse, using sliders over the input parameters.我希望用户能够使用输入参数上的滑块选择模拟运行的子集进行分析。 This means that I need to create the appropriate number of sliders, one for each parameter input.这意味着我需要创建适当数量的滑块,每个参数输入一个。

I have it reading the file and extracting the variable names, and the variables get listed properly.我让它读取文件并提取变量名称,然后正确列出变量。 I also have some code working to get a single chooser with all the variables I want (inVarsChooser in code below), so the variable name construction is all correct.我还有一些代码可以用来获得一个包含我想要的所有变量的单一选择器(下面代码中的 inVarsChooser),所以变量名构造都是正确的。 But I can't make it create multiple sliders (restrictRuns in code below).但我不能让它创建多个滑块(restrictRuns 在下面的代码中)。

ui code is: ui代码是:

library(shiny)

shinyUI(navbarPage("Test",

  # Choose dataset and display variables
  tabPanel("Input Data",
           sidebarLayout(

             sidebarPanel(
               uiOutput("restrictRuns"),
               br(),
               htmlOutput("inVarsChooser")
             ),

             mainPanel(
               fileInput(inputId = "bsFilename",
                         label = "Load file (table format)",
                         accept=c('text/csv', 'text/comma-separated-values,text/plain',
                                  '.csv'),
                         width = "800px"),

               column(width = 6,
                      h4("Simulation parameters"),
                      htmlOutput("inVarsDisplay")
                      ),

               column(width = 6,
                      h4("Simulation reporters"),
                      htmlOutput("outVarsDisplay")
               )
             )
           )
  )

))

server code is:服务器代码是:

library(shiny)

shinyServer(function(input, output, session) {

  bsData <- reactive({
    infile <- input$bsFilename
    if (is.null(infile)){
      return(NULL)      
    }
    read.csv(infile$datapath, stringsAsFactors = TRUE)
  })

  inVars <- reactive({
    df <- bsData()
    if (is.null(df)) return(NULL)
    bsVarnames <- names(df)
    inVars <- bsVarnames[(which(bsVarnames=="X.run.number.")+1):(which(bsVarnames=="X.step.")-1)]
  })

  outVars <- reactive({
    df <- bsData()
    if (is.null(df)) return(NULL)
    bsVarnames <- names(df)
    outVars <- bsVarnames[(which(bsVarnames=="X.step.")+1):length(bsVarnames)]
  })

  output$restrictRuns <- renderUI({
    for (ii in 1:length(inVars())) {
      sliderInput(inputId = paste("range", inVars()[ii], sep=""),
                  label = inVars()[ii],
                  min = 1, max = 1000, value = c(200,500))
    }
  })

  output$inVarsDisplay <- renderUI({
    HTML(paste(inVars(), collapse = '<br/>'))
  })

  output$outVarsDisplay <- renderUI({
    HTML(paste(outVars(), collapse = '<br/>'))
  })

  output$inVarsChooser <- renderUI({
    selectInput("dependent","Select ONE variable as dependent variable from:", inVars())
  })

})

If you want to add sliders for all variables, no matter which one you select in restrictRuns , add this to server.R:如果您想为所有变量添加滑块,无论您在restrictRuns选择哪一个,请将其添加到 server.R:

output$sliders <- renderUI({
  pvars <- inVars()
  lapply(seq(pvars), function(i) {
    sliderInput(inputId = paste0("range", pvars[i]),
                label = pvars[i],
                min = 1, max = 1000, value = c(200, 500))
  })

})

and this to ui.R in your sidebarPanel(...) :并将其添加到 sidebarPanel sidebarPanel(...) ui.R :

uiOutput("sliders")

Sidenote:边注:

If you replace:如果更换:

bsData <- reactive({
  infile <- input$bsFilename
  if (is.null(infile)){
    return(NULL)      
  }
  read.csv(infile$datapath, stringsAsFactors = TRUE)
})

With:和:

bsData <- reactive({
  validate(
    need(input$bsFilename, "Input a valid filepath.")
  )
  infile <- input$bsFilename
  read.csv(infile$datapath, stringsAsFactors = TRUE)
})

You can get rid of all of the if (is.null(...)) return(NULL)你可以去掉所有的if (is.null(...)) return(NULL)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM