Getting shiny to display a graph

First time using Shiny app (still new with using R), so will appreciate simple explanations. I’ve manged to piece codes from various templates to achieve what I’d ideally like which is an app that allows users to upload a csv file and click a button that will display the graph to the right. The graph will use the date column and metric column from the uploaded data to display a spc chart using the package NHSRPlotthedot
The below is what I’m using to create the app:

ui <- fluidPage(
  titlePanel("plot"),
  sidebarLayout(
    sidebarPanel("Sidebar panel",
                
                 fileInput("file1", "Choose CSV File",
                           accept = c(
                             "text/csv",
                             "text/comma-separated-values,text/plain",
                             ".csv")
                 ),
                 tags$hr(),
                 checkboxInput("header", "Header", TRUE),
                 actionButton('click', 'Chart')
    ),
    mainPanel("Plot",
              plotOutput("MyChart"),
              tableOutput("table.output")
    )
  )
)




server = function(input, output, session) {
  a <- 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 

shinyApp(ui, server)

However I’m not sure how to add the graph to the server.R part of the code.

Ideally I’d like to use the uploaded data to feed the below code:

 MyInputData%>% ptd_spc(value_field =metric, date_field = date, improvement_direction = "decrease")

And display the graph on the right side of my app.

Below is one way to show a plot and table by clicking on a button, which adds to the code you shared. The data used comes from NHSRdatasets::ae_attendances, with columns renamed for date and metric to match the fields you specified in ptd_spc().

MyInputData is a reactiveValue added to handle the data that drives the plot and table outputs. It starts as NULL, but once a file is uploaded and the button clicked, an observeEvent updates MyInputData with data from the uploaded file. Then, the chart and table are each created from MyInputData (as long as MyInputData is not NULL).

library(shiny)
library(dplyr)
library(NHSRplotthedots)

ui <- fluidPage(
  titlePanel("plot"),
  sidebarLayout(
    sidebarPanel("Sidebar panel",
                 
                 fileInput("file1", "Choose CSV File",
                           accept = c(
                             "text/csv",
                             "text/comma-separated-values,text/plain",
                             ".csv")
                 ),
                 tags$hr(),
                 checkboxInput("header", "Header", TRUE),
                 actionButton('click', 'Chart')
    ),
    mainPanel("Plot",
              plotOutput("MyChart"),
              tableOutput("table.output")
    )
  )
)

server = function(input, output, session) {
  a <- 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)
  })
  
  # create data frame to plot/show table (starts as NULL)
  MyInputData = reactiveValues(d = NULL)
  
  # when Chart button is clicked, update MyInputData$d
  observeEvent(input$click, {
    MyInputData$d <<- a()
  })
  
  # chart
  output$MyChart = renderPlot({
    # if MyInputData$d is not null, return plot of MyInputData$d
    if(!is.null(MyInputData$d)) {
      MyInputData$d %>% 
        # date is character in my data, so have to convert to date field for plotting
        mutate(date = as.Date(date)) %>% 
        ptd_spc(value_field = metric, date_field = date, improvement_direction = "decrease")
    }
  })
  
  # table
  output$table.output = renderTable({
    # if MyInputData$d is not null, return table of MyInputData$d
    if(!is.null(MyInputData$d)) {
      MyInputData$d
    }
  })
  
}

shinyApp(ui, server)

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.