R shiny: read the table from the file and use it

I want to read the data.frame file from a file and then use it to create graphs later. The problem is that when I read the data, it is part of the output object and can no longer be used. I want to use data from a csv file to create many different graphs. Here is a small example when there are two columns in the csv file, what I want to do is at the end of the functional server.

shiny::runApp(list(
 ui=pageWithSidebar(
    headerPanel('Simple matrixInput')
    ,
    sidebarPanel(
        fileInput('file1', 'Choose CSV File',
                  accept=c('text/csv', 'text/comma-separated- values,text/plain', '.csv'))
        ,
        tags$hr(),
        checkboxInput('header', 'Header', TRUE),
        radioButtons('sep', 'Separator',
                     c(Comma=',',
                       Semicolon=';',
                       Tab='\t'),
                     'Comma'),

        radioButtons('dec', 'Desimal seperator',
                     c('komma'=",",
                       'punktum'="."), 'komma')
    )
    ,
    mainPanel(

        tableOutput(outputId = 'table.output'),
        plotOutput("plot1")
    ))
,
server=function(input, output){
    output$table.output <- renderTable({

        inFile <- input$file1

        if (is.null(inFile))
            return(NULL)

        tbl <- read.csv(inFile$datapath, header=input$header, sep=input$sep,  dec = input$dec)

        return(tbl)
    })

    #I also want to do this:
    #  output$plot1 <- plot(tbl[,1], tbl[,2])
}
)) 
+4
source share
1 answer

Use reactive function:

Make the following changes to the server side of your application

library(shiny)

shinyServer(function(input, output) {

mydata <- reactive({

inFile <- input$file1

if (is.null(inFile))
  return(NULL)

tbl <- read.csv(inFile$datapath, header=input$header, sep=input$sep,  dec = input$dec)

return(tbl)
})

 output$table.output <- renderTable({
 mydata()
 })

 output$plot1 <- renderPlot({
  x <- mydata()[,1]
  plot(x)
 })
})
+7
source

Source: https://habr.com/ru/post/1665522/


All Articles