Loading data into Shiny for analysis

I am trying to load a file into Shiny to crunch it and then preview it.

I was already able to create a table with a CSV doc, but the myData output cannot be used as input for boxplot or any other plot.

SERVER

    shinyServer(function(input, output, session){
  myData <- reactive({
    inFile <- input$file1
    if (is.null(inFile)) return(NULL)
    data <- read.csv(inFile$datapath, header = TRUE)
    data
  })

  output$contents <- renderTable({
    myData()

  })


}

      

interface

library(shiny)

write.csv(data.frame(a = 1:10, b = letters[1:10]), 'test.csv')

shinyUI(fluidPage(
  titlePanel("Uploading Files"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept=c('text/csv',
                         'text/comma-separated-values,text/plain',
                         '.csv'))
    ),
    mainPanel(
      tableOutput('contents'),
      plotOutput('distPlot')
    )
  )
)
)

      

How can I use data from a file that I have loaded from the myData function?

+3


source to share


1 answer


You must use the renderDataTable and dataTableOutput functions from the DT package to render your tables. Your code works like this:

library(shiny)
library(DT)

server <- function(input, output, session){
  myData <- reactive({
    inFile <- input$file1
    if (is.null(inFile)) return(NULL)
    data <- read.csv(inFile$datapath, header = TRUE)
    data
  })

  output$contents <- DT::renderDataTable({
    DT::datatable(myData())       
  })
}

write.csv(data.frame(a = 1:10, b = letters[1:10]), 'test.csv')

ui<- shinyUI(fluidPage(
  titlePanel("Uploading Files"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept=c('text/csv',
                         'text/comma-separated-values,text/plain',
                         '.csv'))
    ),
    mainPanel(
      DT::dataTableOutput('contents'),
      plotOutput('distPlot')
    )
  )
)
)

shinyApp(ui,server)

      




Also see the article below:

It is also possible for the user to load the csv into your Shiny app. The code below provides a small example of how this can be done. It also includes a radioButton input so the user can interactively select the separator to use.

library(shiny)
library(DT)

# Define UI
ui <- shinyUI(fluidPage(

  fileInput('target_upload', 'Choose file to upload',
            accept = c(
              'text/csv',
              'text/comma-separated-values',
              '.csv'
            )),
  radioButtons("separator","Separator: ",choices = c(";",",",":"), selected=";",inline=TRUE),
  DT::dataTableOutput("sample_table")
)
)

# Define server logic
server <- shinyServer(function(input, output) {

  df_products_upload <- reactive({
    inFile <- input$target_upload
    if (is.null(inFile))
      return(NULL)
    df <- read.csv(inFile$datapath, header = TRUE,sep = input$separator)
    return(df)
  })

  output$sample_table<- DT::renderDataTable({
    df <- df_products_upload()
    DT::datatable(df)
  })

}
)

# Run the application 
shinyApp(ui = ui, server = server)

      

+1


source







All Articles