How to call an R script from my Shiny app and pass to the script the input parameters from Shiny, then output results.

Hello,

I hope I have provided a simple example that will allow someone to help me better understand my issue...

I am trying to develop a Shiny app that will enable my users to choose a date range of transactions and a product of interest, at which point I will perform some market basket analysis techniques with all transactions that include the product of interest. For performance reasons, I was looking to load the data (from Amazon Redshift using JDBC/sql/etc) and transform the data in a separate script. I will worry about all of this later...

The Shiny app will allow the user to choose a date range and product, so at the most basic level, I am trying to learn/understand how to have my Shiny app call on the script and run it, using the input variables from the app itself.

To be clear - this is a rough example and a starting point. I'm unsure what needs to be a function, how to call it, etc.

sampleScript:

library("xlsx")
library("readxl")

setwd("C:/Users/yourpathfortheapp")

data <- read_excel("sampledata.xlsx")

data$date <- as.Date(data$date)

my_data_for_shiny <- function(start,end) {

data_new <- data[data$date >= {'my R shiny start date'} &
                   data$date <= {'my R shiny end date'}, ]
}

## Does this need to be a function? I just don't really understand how to pass the Shiny app input values to this script
## then output the results in the server function in the app

app.r File here:

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)

skus <- list('Group1' = list("item1","item2","item3"),
             'Group2' = list("item4","item5")
)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Here is where I'll do my analysis"),

    # Sidebar with a input from user
    sidebarLayout(
      position = "right",
      sidebarPanel (
        
        selectInput("sku", "Select SKU:", skus), 
        textOutput("skuchoice"), p(),
        
        dateInput('start', "Select start date:", '2022-05-05', min='2022-05-05', max = '2022-05-10', weekstart = 0, autoclose = TRUE),
        
        dateInput('end', "Select end date:", '2022-05-10', min='2022-05-05', max = '2022-05-10', weekstart = 0, autoclose = TRUE),
        
      ),
      mainPanel("output stuff here",
                tableOutput('table'))
)
)

server <- function(input, output) {
  
  
  
  output$skuchoice <- renderText({
    paste("Analyzing all transactions that contain", input$sku)
  })
  
  ## How do I call in "samplescript" and pass it start/end from the input, then output data_new from samplescript?
  # output$table <- renderTable(
  #   source(file=samplescript.R))
    
  
}

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

I have provided a very simple sample of data, plus a sample script and app.R file code which I hope will be enough for someone be able to help me. The sample script is pulling in excel but I have provided a Google Sheet since you can't attach files here.

Thank you!

This topic was automatically closed 54 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.