简体   繁体   中英

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. 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? 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.

Here is an example for files in the current working directory. The example file I used was a minimal csv file (see bottom). 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 ).

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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