2017-11-09 4 views
1

Ich rufe R-Skript in Shiny. R-Skript hat eine Funktion, die eine Liste von Dateien als Argument akzeptiert. Aber lies dann jede Datei in der Funktion. Hier ist das BeispielWie erstellt man eine Liste von Dateien in glänzend?

folder <- "~/Desktop/shiny" 
file_list <- list.files(path=folder, pattern="*.XLS") 


count_genes = function(file_list){ 
#Reading multiple files into a list of data frames, 
    my_data <- lapply(file_list, read.table) 
. 
. 
.} 

Wie kann ich eine ähnliche Liste von (file_list) in glänzend erstellen und das Ergebnis aus count_genes machen.

Der Code soll 1- wählen mehrere Dateien in einem Verzeichnis. 2 - Wenden Sie die Funktion auf ausgewählte Dateien an und 3 - rendern Sie die Tabelle.

Folgendes habe ich ausprobiert. Bitte entschuldigen Sie meine Einbuchtung

source('ReadFiles.R', local = TRUE) 
library(shiny) 

ui = navbarPage(HTML("Title"), 
       tabPanel(HTML("<font size=3>Start</font>"), 
         sidebarPanel(width = 2, 
             fileInput('files', 'UPLOAD', multiple = TRUE, accept = c('.XLS')), 
         mainPanel(
          tabsetPanel(
          tabPanel("StaVst", 
             fluidRow(column(6,dataTableOutput("tabel1"))))          
)) 
server = function(input, output, session) { 
    lst1 <- eventReactive(input$files, { 
    req(input$Load!=0) 
    lst <- list() 
    for(i in 1:length(input$files[,1])){ 
     lst[[i]] <- input$files[[i, 'datapath']] 
    } 
    count_genes(lst) 
    }) 
    output$tabel1 <- renderDataTable({ 
    req(!is.null(input$files) & input$Load!=0) 
    lst1() 
    }) 
} 
+0

Ich bin verwirrt, das ist dein neuer Versuch? – amrrs

+0

Nein ist es nicht. Es ist eine separate Frage. – user91

Antwort

1

fileInput speichert Informationen über die ausgewählten Dateien in einem Datenrahmen. Der Datenrahmen hat die Spalten 'Name', 'Größe', 'Typ' und 'Datenpfad'. Die Dateien werden intern mit zufälligen Pfadnamen kopiert (z. B. "... AppData \ Local \ Temp \ RtmpMxmB1G/48101199adb41c8ae7de0e78/3.txt"), auf die mit input$files$datapath zugegriffen werden kann.

Statt Ihre for-Schleife und Ihre Funktion count_genes() können Sie: tbl_list <- lapply(input$files$datapath, read.table, header=TRUE, sep=";") direkt jede Datei in in der Liste ‚tbl_list‘ zu lesen, oder wenn Sie die die Dateipfade nur benötigen, können Sie mit Ihrer Funktion count_genes()

Abhängig von Ihrer Funktion count_genes() enthält Ihr Objekt lst1() verschiedene Dinge, dh das Objekt, das in der Funktion count_genes() zurückgegeben wird. Mit Ihrer Funktion count_genes(), wie in Ihrer Frage angegeben, ist lst1() ein Listenobjekt, das nicht von renderDataTable verarbeitet werden kann. Um renderDataTable zu verwenden, müssen Sie zunächst Ihre Liste in einen Datenrahmen in der Funktion count_genes() konvertieren.

Hier ist ein minimales Beispiel, die helfen können:

library(shiny) 

# Define UI for data upload app ---- 
ui <- fluidPage(

    # App title ---- 
    titlePanel("Uploading Files"), 

    # Sidebar layout with input and output definitions ---- 
    sidebarLayout(

    # Sidebar panel for inputs ---- 
    sidebarPanel(

     # Input: Select a file ---- 
     fileInput("files", "Upload", multiple = TRUE, accept = c(".xls")) 
    ), 

    # Main panel for displaying outputs ---- 
    mainPanel(
     # Output: Data file ---- 
     dataTableOutput("tbl_out") 

    ) 

) 
) 

# Define server logic to read selected file ---- 
server <- function(input, output) { 
    lst1 <- reactive({ 
    validate(need(input$files != "", "select files...")) 

    if (is.null(input$files)) { 
     return(NULL) 
    } else { 

     path_list <- as.list(input$files$datapath) 
     tbl_list <- lapply(input$files$datapath, read.table, header=TRUE, sep=";") 

     df <- do.call(rbind, tbl_list) 
     return(df) 
    } 
    }) 

    output$tbl_out <- renderDataTable({ 
    lst1() 
    }) 

} 

# Create Shiny app ---- 
shinyApp(ui, server) 
+0

Ich kann dir nicht genug danken. Es klappt – user91

0

Ich weiß nicht, ob die folgende Lösung für Sie akzeptabel ist, weil es VBA/Excel, und nicht R verwendet, aber man kann es trotzdem betrachten .

Public fPath As String 
Public IsSubFolder As Boolean 
Public iRow As Long 
Public FSO As Scripting.FileSystemObject 
Public SourceFolder As Scripting.folder, SubFolder As Scripting.folder 
Public FileItem As Scripting.File 
Public IsFileTypeExists As Boolean 



Public Sub ListFilesInFolder(SourceFolder As Scripting.folder, IncludeSubfolders As Boolean) 


    On Error Resume Next 
    For Each FileItem In SourceFolder.Files 


' display file properties 
     Cells(iRow, 2).Formula = iRow - 13 
     Cells(iRow, 3).Formula = FileItem.Name 
     Cells(iRow, 4).Formula = FileItem.Path 
     Cells(iRow, 5).Formula = Int(FileItem.Size/1024) 
     Cells(iRow, 6).Formula = FileItem.Type 
     Cells(iRow, 7).Formula = FileItem.DateLastModified 
     Cells(iRow, 8).Select 
     Selection.Hyperlinks.Add Anchor:=Selection, Address:= _ 
     FileItem.Path, TextToDisplay:="Click Here to Open" 

'Cells(iRow, 8).Formula = "=HYPERLINK(""" & FileItem.Path & """,""" & "Click Here to Open" & """)" 

     iRow = iRow + 1 ' next row number 
     Next FileItem 

     If IncludeSubfolders Then 
      For Each SubFolder In SourceFolder.SubFolders 
       ListFilesInFolder SubFolder, True 
       Next SubFolder 
      End If 

      Set FileItem = Nothing 
      Set SourceFolder = Nothing 
      Set FSO = Nothing 



     End Sub 




     Public Sub ListFilesInFolderXtn(SourceFolder As Scripting.folder, IncludeSubfolders As Boolean) 


      On Error Resume Next 
      Dim FileArray As Variant 

      FileArray = Get_File_Type_Array 

      For Each FileItem In SourceFolder.Files 

       Call ReturnFileType(FileItem.Type, FileArray) 

       If IsFileTypeExists = True Then 

        Cells(iRow, 2).Formula = iRow - 13 
        Cells(iRow, 3).Formula = FileItem.Name 
        Cells(iRow, 4).Formula = FileItem.Path 
        Cells(iRow, 5).Formula = Int(FileItem.Size/1024) 
        Cells(iRow, 6).Formula = FileItem.Type 
        Cells(iRow, 7).Formula = FileItem.DateLastModified 

        Cells(iRow, 8).Select 
        Selection.Hyperlinks.Add Anchor:=Selection, Address:= _ 
        FileItem.Path, TextToDisplay:="Click Here to Open" 

'Cells(iRow, 8).Formula = "=HYPERLINK(""" & FileItem.Path & """,""" & "Click Here to Open" & """)" 

        iRow = iRow + 1 ' next row number 

       End If 
       Next FileItem 

       If IncludeSubfolders Then 
        For Each SubFolder In SourceFolder.SubFolders 
         ListFilesInFolderXtn SubFolder, True 
         Next SubFolder 
        End If 

        Set FileItem = Nothing 
        Set SourceFolder = Nothing 
        Set FSO = Nothing 



       End Sub 



       Sub ResultSorting(xlSortOrder As String, sKey1 As String, sKey2 As String, sKey3 As String) 
        Range("C13").Select 
        Range(Selection, Selection.End(xlDown)).Select 
        Range(Selection, Selection.End(xlToRight)).Select 

        Selection.Sort Key1:=Range(sKey1), Order1:=xlSortOrder, Key2:=Range(sKey2 _ 
        ), Order2:=xlAscending, Key3:=Range(sKey3), Order3:=xlSortOrder, Header _ 
        :=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom _ 
        , DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:= _ 
        xlSortNormal 

        Range("B14").Select 
       End Sub 


       Sub ClearResult() 
        If Range("B14") <> "" Then 

         Range("B14").Select 
         Range(Selection, Selection.End(xlDown)).Select 
         Range(Selection, Selection.End(xlToRight)).Select 
         Range(Selection.Address).ClearContents 
        End If 
       End Sub 


       Public Function Get_File_Type_Array() As Variant 

        Dim i, j, TotalSelected As Integer 
        Dim arrList() As String 
        TotalSelected = 0 
        For i = 0 To Sheet1.ListBoxFileTypes.ListCount - 1 
         If Sheet1.ListBoxFileTypes.Selected(i) = True Then 
          TotalSelected = TotalSelected + 1 
         End If 
        Next 

        ReDim arrList(0 To TotalSelected - 1) As String 
        j = 0 
        i = 0 
        For i = 0 To Sheet1.ListBoxFileTypes.ListCount - 1 

         If Sheet1.ListBoxFileTypes.Selected(i) = True Then 
          arrList(j) = Left(Sheet1.ListBoxFileTypes.List(i), InStr(1, Sheet1.ListBoxFileTypes.List(i), "(") - 1) 
          j = j + 1 
         End If 

        Next 

        Get_File_Type_Array = arrList 

       End Function 


       Public Function ReturnFileType(fileType As String, FileArray As Variant) As Boolean 

        Dim i As Integer 

        IsFileTypeExists = False 

        For i = 1 To UBound(FileArray) + 1 

         If FileArray(i - 1) = fileType Then 

          IsFileTypeExists = True 
          Exit For 

         Else 
          IsFileTypeExists = False 
         End If 

        Next 

       End Function 



       Sub textfile(iSeperator As String) 

        Dim iRow, iCol 
        Dim iLine, f 




        ThisWorkbook.Activate 
        Range("B13").Select 
        TotalRowNumber = Range(Selection, Selection.End(xlDown)).Count - 12 

        If iSeperator <> "vbTab" Then 

         Open ThisWorkbook.Path & "\File1.txt" For Output As #1 
         Print #1, "" 
         Close #1 

         Open ThisWorkbook.Path & "\File1.txt" For Append As #1 
         For iRow = 13 To TotalRowNumber 

          iLine = "" 

          For iCol = 2 To 7 

           iLine = iLine & iSeperator & Cells(iRow, iCol).Value 
          Next 
          Print #1, iLine 
         Next 
         Close #1 


        Else 

         Open ThisWorkbook.Path & "\File1.txt" For Output As #1 
         Print #1, "" 
         Close #1 

         Open ThisWorkbook.Path & "\File1.txt" For Append As #1 
         For iRow = 13 To TotalRowNumber 

          iLine = "" 

          For iCol = 2 To 7 

           iLine = iLine & vbTab & Cells(iRow, iCol).Value 
          Next 
          Print #1, iLine 
         Next 
         Close #1 

        End If 



        f = Shell("C:\WINDOWS\notepad.exe " & ThisWorkbook.Path & "\File1.txt", vbMaximizedFocus) 

'MsgBox "Your File is saved" & ThisWorkbook.Path & "\File1.txt" 

       End Sub 





       Sub Export_to_excel() 
        On Error GoTo err 



        Dim xlApp As New Excel.Application 
        Dim xlWB As New Workbook 

        Set xlWB = xlApp.Workbooks.Add 
'xlWB.Add 
        xlApp.Visible = False 


        ThisWorkbook.Activate 
        Range("B13").Select 
        Range(Selection, Selection.End(xlDown)).Select 
        Range(Selection, Selection.End(xlToRight)).Select 

        Selection.Copy 

        xlApp.Visible = True 
        xlWB.Activate 
        xlWB.Sheets("Sheet1").Select 
        xlWB.Sheets("Sheet1").Range("B2").PasteSpecial Paste:=xlPasteValues 
        xlWB.Sheets("Sheet1").Cells.Select 
        xlWB.Sheets("Sheet1").Cells.EntireColumn.AutoFit 
        xlWB.Sheets("Sheet1").Range("B2").Select 
        Exit Sub 
err: 
        MsgBox ("Error Occured while exporting. Try again") 

       End Sub 

Die Idee kommt von hier.

http://learnexcelmacro.com/wp/2011/11/how-to-get-list-of-all-files-in-a-folder-and-sub-folders/

können Sie eine Beispieldatei die URL bilden herunterladen. Viel Glück.

Verwandte Themen