简体   繁体   English

在闪亮的R中从反应性数据集中调用文件名

[英]Calling the filename from a reactive dataset in shiny r

I am currently writing a shiny app which imports a dataset and displays a manipulated version. 我目前正在编写一个闪亮的应用程序,该应用程序可以导入数据集并显示经过处理的版本。 To work on the shiny methods I am currently working on a simplified version which displays the imported dataset. 为了使用闪亮的方法,我目前正在使用简化版本,以显示导入的数据集。 I currently assign the imported dataset to a reactive value, and then use the render table as follows:- 我目前将导入的数据集分配给反应值,然后按如下所示使用渲染表:

shinyServer(function(input, output) {

 DATA<-reactive({
    input$filein
 })



 output$Dataset <- renderTable({ 
   DATA()
 })


})

The interface then produces a table with the following columns:- name, size, type, datapath. 然后,该接口将生成一个包含以下列的表:-名称,大小,类型,数据路径。

What I had in mind was to call the datapath variable, and use read.csv to call it within the renderTable function. 我想到的是调用datapath变量,并使用read.csv在renderTable函数中调用它。 I tried using:- 我尝试使用:-

DATA()$datapath

However that doesn't seem to produce any result. 但是,这似乎没有产生任何结果。 Are there any other ways to extract this data within Shiny? 还有其他方法可以在Shiny中提取此数据吗? I contemplated using vector indices as you would using regular R code however I am unsure as to whether or not that'll work within Shiny. 我打算像使用常规R代码一样使用向量索引,但是我不确定它是否可以在Shiny中使用。

Here is an example for files in the current working directory. 这是当前工作目录中文件的示例。 The example file I used was a minimal csv file (see bottom). 我使用的示例文件是一个最小的csv文件(请参阅底部)。 Please note however that this is indeed limited to files in your working directory. 但是请注意,这确实仅限于您的工作目录中的文件。 If you want other files to be loaded you will need to have a further component to specify the path (possibly in the selectInput ). 如果要加载其他文件,则需要具有其他组件来指定路径(可能在selectInput )。

library(shiny)
library(tools)

runApp(
    list(
        ui = pageWithSidebar(
            headerPanel("File Info Test"),
            sidebarPanel(
                p("Demo Page."),
                selectInput("filein", "Choose File", choices=c("test.csv"))
            ),
            mainPanel(
                tableOutput("myTableInfo"),
                tableOutput("myTable")
            )
        ),
        server = function(input, output){

            mydata <- reactive({
                read.csv(input$filein)
            })

            file_info <- reactive({

                validate(
                    need(!is.null(input$filein), "please select file"
                        )
                    )

                name <- input$filein
                size <- file.info(input$filein)[['size']]
                type <- file_ext(input$filein)
                datapath <- file_path_as_absolute(input$filein)
                cbind(name, size, type, datapath)
            })

            output$myTableInfo <- renderTable({
                file_info()
            })

            output$myTable <- renderTable({
                mydata()
            })

        }
    )
)

test.csv test.csv

X1,X2,X3
1,2,3
4,5,6

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

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