【问题标题】:How to populate csv files of a subdirectory inside shiny app in drop-down menu.?如何在下拉菜单中填充闪亮应用程序内子目录的 csv 文件。?
【发布时间】:2015-05-08 23:31:44
【问题描述】:

可能重复

Cannot populate drop down menu dynamically in R shiny

我有一个小型闪亮应用程序,其中包含一些供用户使用的下拉选项。我在包含 csv 文件的闪亮应用程序中创建了一个子目录,说 data 出现在下拉菜单中。我使用了下面的代码,但我无法访问data 子目录中的文件。

在 ui.r 上:

    filenames <- list.files(pattern="\\.csv") 
   selectInput(inputId="dataset",label= "Choose platform annotation file",filenames)

server.r:

dataInput <- reactive({
    if (grepl("[/\\\\]", input$dataset)) {
      stop("Invalid dataset")
    }
    read.csv(file.path("data", input$dataset))
  })

  output$annotation <- renderDataTable({
    withProgress(message = 'Loading Data . . .', {
      dataInput()

    })
  })

上面的代码允许我访问应用程序内的 csv 文件,就像我的 server.rui.r 一样,而不是在闪亮应用程序内的单独子目录中。

我也想知道这是访问上述代码数据的正确方法,因为我无法在下面的代码中进一步访问数据。

inputdata <- reactive({ 

    df1 <- data.frame()
    df1 <- dataInput()
    if (is.null(df1))
      return(NULL)
    df1[] <- lapply(df1, as.character)
    df1 <- df1[df1[,3]!='',]
    df1 <- df1[!grepl('[/]', df1$Gene.Title),]
  }) 

我也试过了

    filenames <- list.files(pattern="\\.csv$data") ## data is my folder inside shiny app.

使用 csv 文件访问闪亮应用程序内的data 子目录,但不能这样做。

已编辑:输入文件示例

ID                        Gene Title                      Gene Symbol
1007_s_at   discoidin domain receptor tyrosine kinase 1    DDR1
1053_at     replication factor C (activator 1) 2, 40kDa    7-Mar
117_at      heat shock 70kDa protein 6 (HSP70B')           HSPA6
121_at      paired box 8                                   PAX8
1255_g_at   guanylate cyclase activator 1A (retina)        GUCA1A
1294_at     ubiquitin-like modifier activating enzyme 7    UBA7 
1320_at     protein tyrosine phosphatase, non-receptor     PTPN21
1405_i_at   chemokine (C-C motif) ligand 5                 CCL5/CCL6
1431_at      
1438_at     EPH receptor B3                                EPHB3
1487_at     estrogen-related receptor alpha                ESRRA
1494_f_at   cytochrome P450, family 2                      CYP2A6/CYP2

【问题讨论】:

    标签: r csv shiny


    【解决方案1】:

    当您调用list.files时,您需要指定要列出文件的路径,默认路径是当前目录,即您拥有ui.Rserver.R文件的目录。

    试试:

    filenames <- list.files(path="data",pattern="\\.csv")
    

    您可以在ui.R 文件中将此行放在shinyUI(...) 之前。

    对于您的代码的第二部分,您似乎想要清理数据,您可以这样做(我将inputdata 更改为clean_data 以使其更清晰):

    clean_data <- reactive({ 
            #get the data 
            df1 <- dataInput()
    
            if (is.null(df1))
                    return(NULL)
            df1[] <- lapply(df1, as.character)
    
            #looks for lines that have only zero or more blanks or a slash in column 3 and removes them
            df1 <- df1[!grepl("$ *^|/",df1[,3]),]
            df1
    }) 
    
    #do what you want with the data, for expample render it as another table
     output$annotation <- renderDataTable({
                withProgress(message = 'Loading Data . . .', {
                      clean_data()
    
                })
        })
    

    此外,查看您的输入文件示例,您有很多带有逗号的 Gene title,如果您的 csv 文件中的分隔符是逗号,这可能会导致问题。

    【讨论】:

    • 谢谢,这行得通。上述问题中的其他问题呢。
    • 我真的不明白你的意思,或者问题是什么。加载数据后,dataInput() 会为我返回数据帧。有什么问题?也许发布几行数据也会有所帮助。
    • 这里也返回数据框。请查看inputdata 部分中的上述代码,我需要数据进行进一步处理,但我无法做到。我可能会遗漏一些我认为的东西,我不确定。需要再看代码。
    • 好的,您能否也发布您的csv 文件之一的几行以查看它的外观以及inputdata 中的代码是否可以正常运行?
    猜你喜欢
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 2021-05-01
    相关资源
    最近更新 更多