Linking 2 Shiny Apps

shiny

#1

Hi There,

I would like to build an app that exchanges info with an app I already built, in other words they link to each other and can share information. Does anyone know of any examples that I can use? The problem is better stated in this example. I just wondered if there was a clear solution out there. I wonder if plumber would be useful? Any suggestions would be wonderful.

Thanks,
gwynn


#2

I wrote two apps that can share date by uploading to AWS, then downloading to the app. This uploads to AWS:

library(shiny)

library(aws.s3)

get_time_human <- function() {
  format(Sys.time(), "%Y%m%d-%H%M%OS")
}

s3BucketName <- "goldilocksapp"
Sys.setenv("AWS_ACCESS_KEY_ID" = "PUT KEY HERE",
           "AWS_SECRET_ACCESS_KEY" = "PUT OTHER KEY HERE",
           "AWS_DEFAULT_REGION" = "PUT REGION HERE1")


saveData <- function(data, FN) {
  # Create a temporary file to hold the data
  data <- t(data)
  file_name <- paste0(FN,".csv")
  file_path <- file.path(tempdir(), file_name)
  write.csv(data, file_path, row.names = FALSE, quote = TRUE)
  
  # Upload the file to S3
  put_object(file = file_path, object = file_name, bucket = s3BucketName)
}

## Loads all of the data from the bucket
loadData <- function(FN) {
  object <- get_object(FN, s3BucketName)
  object_data <- readBin(object, "character")
  data = read.csv(text = object_data, stringsAsFactors = FALSE)
  data  
}

# Define UI for application
ui <- fluidPage(
   
   # Application title
   titlePanel("Upload"),
   
   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30),
         br(),
         
         textInput("FN", label = h3("File name:"), 
                   value = "File name"),
         
         actionButton("save", "Save data with above name")
      ),
      
      
      
      # Show the data
      mainPanel(
         tableOutput("data")
      )
   )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  output$data <- renderTable(
    faithful[1:input$bins,]
  )
  
  observeEvent(input$save, {
    saveData(faithful[1:input$bins,], FN = input$FN)
  }) ## observeEvent
   
}

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

This app downloads the data from AWS:

 library(shiny)
library(aws.s3)
get_time_human <- function() {
  format(Sys.time(), "%Y%m%d-%H%M%OS")
}

s3BucketName <- "goldilocksapp"
Sys.setenv("AWS_ACCESS_KEY_ID" = "PUT KEY HERE",
           "AWS_SECRET_ACCESS_KEY" = "PUT OTHER KEY HERE",
           "AWS_DEFAULT_REGION" = "PUT REGION HERE")


saveData <- function(data, FN) {
  # Create a temporary file to hold the data
  data <- t(data)
  file_name <- paste0(FN,".csv")
  file_path <- file.path(tempdir(), file_name)
  write.csv(data, file_path, row.names = FALSE, quote = TRUE)
  
  # Upload the file to S3
  put_object(file = file_path, object = file_name, bucket = s3BucketName)
}

## Loads all of the data from the bucket
loadData <- function(FN) {
  object <- get_object(FN, s3BucketName)
  object_data <- readBin(object, "character")
  data = read.csv(text = object_data, stringsAsFactors = FALSE)
  data  
}

# Define UI for application that draws a histogram
ui <- fluidPage(
   
   # Application title
   titlePanel("Download"),
   
   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30),
         br(),
         
         textInput("FN", label = h3("File name:"), 
                   value = "File name"),
         
         actionButton("save", "Save data with above name")
      ),
      
      
      
      # Show a plot of the generated distribution
      mainPanel(
         tableOutput("data")
      )
   )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  output$data <- renderTable(
    faithful[1:input$bins,]
  )
  
  observeEvent(input$save, {
    saveData(faithful[1:input$bins,], FN = input$FN)
  }) ## observeEvent
   
}

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

Thanks to Joe Cheng who helped me recognize that an observe needed to be a reactive or the other way around. Can't remember which. I hope this is useful to someone.