Shiny app (R) 中的交互式目录输入 [英] Interactive directory input in Shiny app (R)

查看:49
本文介绍了Shiny app (R) 中的交互式目录输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个闪亮的应用程序,它需要用户在本地机器上选择一个文件夹,其中包含应用程序要处理的文件.

I am building a shiny app that requires a user to select a folder on the local machine, which contains the files to be processed by the app.

我正在使用此处提出的解决方案.这在本地机器上工作正常,但如果应用程序部署到 Shinyapps 服务器,则不起作用.该解决方案的作者证实,它仅适用于本地 Shiny 应用程序,因为它会调用 OS shell 来显示目录对话框.

I am using a solution proposed here. This works fine on a local machine, but does not work if the app is deployed to a shinyapps server. The author of this solution confirmed that it was only designed to work with local Shiny apps, since it makes OS shell calls to display a directory dialog.

我想知道目录对话框是否有不同的解决方案,它适用于已部署的 Shiny 应用程序(我正在部署到 Shinyapps.io).

I am wondering if there is a different solution for directory dialog, which will work on the deployed Shiny apps (I am deploying to shinyapps.io).

请注意,我无法使用 fileInput 接口有两个原因:

Edited: Notice that I cannot use fileInput interface for two reasons:

  1. 该应用的用户不是技术人员,他们不知道该应用使用了文件夹中的哪些文件.
  2. 选定的文件夹可能包含所需文件所在的其他文件夹,因此不可能一次选择所有文件,即使 fileInput 接口启用了 multiple 选项.

文件夹/文件结构不是我可以更改的,它是从医疗设备按原样下载的,因此我对用户的唯一期望是指定父文件夹,其余的应该在 R 中完成代码.

The folder/files structure is not something I can change, it is downloaded AS IS from a medical device and therefore the only thing I can expect from the users is to specify the parent folder and the rest should be done inside the R code.

推荐答案

这是一个基于使用webkitdirectory"属性的工作示例.目前该属性被 Chrome、Opera 和 Safari(移动和桌面)支持,并且应该在 9 月发布的 Firefox 49 中支持.在此处了解更多相关信息.它也适用于子目录.

This is a working example based on using the "webkitdirectory" attribute. At the moment the attribute is supported by Chrome, Opera and Safari (mobile and desktop) and it should be supported in Firefox 49 to be released in September. More about this here. It work with subdirectories also.

它需要在 ui.R 中使用 tags 关键字.我通过上传三个 csv 文件来测试它,每个文件包含三个由昏迷分隔的数字.在本地和 Shinyapps.io 上使用 Chrome 和 Opera 进行测试.这是代码:

It requires using the tags keyword in ui.R. I have tested it by uploading three csv files each contaning three numbers separeted by a coma. Tested locally and on shinyapps.io with Chrome and Opera. This is the code:

ui.R

    library(shiny)
    library(DT)

    shinyUI(tagList(fluidPage(theme = "bootstrap.css",
                      includeScript("./www/text.js"),
                      titlePanel("Folder content upload"),

                      fluidRow(
                              column(4,
                                     wellPanel(
                                             tags$div(class="form-group shiny-input-container", 
                                                      tags$div(tags$label("File input")),
                                                      tags$div(tags$label("Choose folder", class="btn btn-primary",
                                                                          tags$input(id = "fileIn", webkitdirectory = TRUE, type = "file", style="display: none;", onchange="pressed()"))),
                                                      tags$label("No folder choosen", id = "noFile"),
                                                      tags$div(id="fileIn_progress", class="progress progress-striped active shiny-file-input-progress",
                                                               tags$div(class="progress-bar")
                                                      )     
                                             ),
                                             verbatimTextOutput("results")
                                     )
                              ),
                              column(8,
                                     tabsetPanel(
                                             tabPanel("Files table", dataTableOutput("tbl")),
                                             tabPanel("Files list", dataTableOutput("tbl2"))
                                     )
                              )
                      )
    ),
    HTML("<script type='text/javascript' src='getFolders.js'></script>")
    )

    )          

server.R

    library(shiny)
    library(ggplot2)
    library(DT)

    shinyServer(function(input, output, session) {
            df <- reactive({
                    inFiles <- input$fileIn
                    df <- data.frame()
                    if (is.null(inFiles))
                            return(NULL)
                    for (i in seq_along(inFiles$datapath)) {
                            tmp <- read.csv(inFiles$datapath[i], header = FALSE)  
                            df <- rbind(df, tmp)
                    }
                    df

            })
            output$tbl <- DT::renderDataTable(
                    df()
            )
            output$tbl2 <- DT::renderDataTable(
                    input$fileIn
            )
            output$results = renderPrint({
                    input$mydata
            })

    })

text.js

window.pressed = function(){
        var a = document.getElementById('fileIn');
        if(a.value === "")
        {
            noFile.innerHTML = "No folder choosen";
        }
        else
        {
            noFile.innerHTML = "";
        }
    };

getFolders.js

getFolders.js

     document.getElementById("fileIn").addEventListener("change", function(e) {

            let files = e.target.files;
            var arr = new Array(files.length*2);
            for (let i=0; i<files.length; i++) {

            //console.log(files[i].webkitRelativePath);
            //console.log(files[i].name);
            arr[i] = files[i].webkitRelativePath;
            arr[i+files.length] = files[i].name;


            }

            Shiny.onInputChange("mydata", arr);

    });

如果这有帮助,请告诉我.

Let me know if this helps.

这篇关于Shiny app (R) 中的交互式目录输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆