Import Data Frame into Shiny

I watched the shiny tutorial and created a very basic shiny app. However, I have no idea how to include the data from my dataframe in the shiny output. Someone that is not in Academia and does this in the real world please help.

Your title asks about importing a data frame into shiny. That can be done by storing the data frame either as a binary file using the save() function or a csv file using write.csv() and having the shiny app read it in using load() for a binary file or read.csv() for a csv file. There are other options also.

In the text of your question you mention "include the data from my dataframe in the shiny output". That would be done with the renderTable() and tableOutput() functions.

I am looking got the following:

  1. How does shiny know what data it is working with
  2. What format does this data need to be in for shiny to plot it
  3. Specific code example of incorporating data into shiny (a csv or dataframe)

The shiny tutorial does not explain this, which is a core concept to getting shiny to actually work with your data

library(shiny)
#a file with two columns, ds and  y
MyDat <- read.csv("c:/users/fjcc/Documents/R/Play/FJCC_data.csv") 
ui <- fluidPage(
  numericInput("Mult", "Multiplier", value = 1),
  tableOutput("TBL"),
  plotOutput("Plot", width = "300px", height = "250px")
)

server <- function(input, output) {
  NewDat <- reactive({
    MyDat %>% mutate(y = y * input$Mult)
  })
  #Show the data with column y multiplied by the chosen value
  output$TBL <- renderTable({
      NewDat()
  })
  
  output$Plot <- renderPlot({
     plot(NewDat()$ds, NewDat()$y)
  })

}
shinyApp(ui, server)

Shiny is not an independent thing - it is an extension of your R code, where you write interactive application to access the same data or perform the same actions as you do in your R Script.
Data could be in any format - depends on what you use for plotting - data frame for ggplot, xts for dygraphs, etc...
https://github.com/edgararuiz/databases-w-r/blob/1.0/app.R - look at this example, where data comes from databases - but idea is the same - you are writing code to handle data and shiny works with what data is brought in by your script.

1 Like

Thank you this helps a lot

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.