如何使用inputdata后面的选项列表创建selectInput

时间:2018-03-20 09:35:33

标签: r shiny

我的想法是在用户上传文件时从列中获取所有唯一值,之后我在selectinput上的选择将更改为这些唯一值。

有我的ui.R代码:

`library(shinythemes)
 shinyUI(fluidPage(theme = "mystyle.css",
  titlePanel("Pruebas"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file2", HTML("<table><tr><td><strong>Choose your VCF file</strong></td>
<td>
<div class='help-tip'>
    <p>Upload your VCF file with which you will work.</p>
                                          </div></td></tr>
                                          </table>
                                          "),
                  multiple = TRUE,
                  accept = c("text/csv",
                             "text/comma-separated-values,text/plain",
                             ".vcf")),
      selectInput("filter", h3("Choose your filter"),
                   choices = list("Chr 1" = 1, "Chr 2" = 2,
                                   "Chr 3" = 3),selected = 1)`

所以我的想法是使用“file2”做一个过滤器并使用它来获取那些唯一值:

`df <- read.csv(input$file2$datapath, header = TRUE, sep = "\t", quote = '"', skip = 56)
x <- unique(df[,1]
x <- levels(x)`

然后,如果我们做x [1],我们将得到第一个唯一元素,然后我想做一个选项列表,其中选项是列表“x”上的所有元素。

我在server.R尝试了一些东西:

`        outVar <- reactive({
  inFile <- input$file2

  if (is.null(inFile))
    return(NULL)
  dff <- read.csv(inFile$datapath, header = TRUE, sep = "\t", quote = '"', skip = 56)
  y <- unique(dff[,1])
  y <- levels(y)
  y
})
observe({
  updateSelectInput(session, "mychrlist",
                    choices = outVar()
  )})`

然后在ui.R:

`selectInput("select2", HTML("<table><tr><td><strong>Chromosome</strong></td>
<td>
<div class='help-tip'>
    <p>Filter by chromosome to focus one chromosome insted all of them.</p>
                                          </div></td></tr>
                                          </table>
                                          "), 
                              choices = ("mychrlist")),`

但选择列表保留在“mychrlist”,我想要像Chr0,Chr1,Chr2等。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

documentation中的示例一样,在NULL的读者逻辑中添加input$file2项检查。

当应用程序启动时,输入具有默认的NULL值,直到第一次设置为止。因此,您最初提供read.csv NULL引用来阅读。