Run R-Code on different imported xlsx-Sheets in Shiny

shiny

#1

Hi,

I have also asked this question on Stakoverflow (https://stackoverflow.com/questions/50826464/run-r-code-on-different-imported-xlsx-sheets-in-shiny) with no answers, so I thought I would ask here.

My goal is to build a shiny-app where you can upload an xlsx-file (as template) with multiple sheets. After that, a long R-code runs on these imported sheets, which supplies the outputs (prepared as data.frames) for the tables and figures shown in the app.

If I load the xlsx-file with the sheets locally in R-Shiny (--> no reactive() variables because no FileInput) the app works fine.

Is it possible to save the different sheets of the xlsx-file loaded in Shiny as individual data.frames, then use these data.frames as input for the long R code, and use the data.frames written in this R code as an ouput for the tables and graphics?

Despite intensive reading, I am far away from a (working) solution.

 library(shiny)
 library(xlsx)

 runApp(
 list(

 ui = fluidPage(
  titlePanel("Use readxl"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose xlsx file',
                accept = c(".xlsx")
      )
    ),
    mainPanel(
      tableOutput('contents'))
  )),
    mainPanel(
      tableOutput('contents2'))
  )),


  server = function(input, output){

  storedData1 <- reactiveValues
  storedData2 <- reactiveValues

  reactive(
    storedData1$sheet1 <- input$file1
    if(is.null(inFile))
      return(NULL)
    sheet1 <- read_xlsx(inFile$datapath, sheet =  "Sheet1")

    storedData2$sheet2 <- input$file1
    if(is.null(inFile))
      return(NULL)
    sheet2 <- read_xlsx(inFile$datapath, sheet =  "Sheet2")

    sheet1 <- reactive({
      return(as(storedData2$sheet1(), "data.frame"))
    })

    sheet2 <- reactive({
      return(as(storedData2$sheet2(), "data.frame"))
    })

    source("MyRCode.R", local=TRUE)

    # This My.RCode.R-File is very long. 
    # A simple output for better understanding:
    # sheet3 <- as.data.frame(rbind(sheet1, sheet2))
    # sheet4 <- as.data.frame(cbind(sheet1, sheet2))       
    )

    output$contents <- renderDataTable(output_df_from_MyRCode.R()) # or in the example, sheet3

    output$contents2 <- renderDataTable(a_second_output_df_from_MyRCode.R()) # or in the example, sheet4

   }


      )))

Is something like that even possible? Thanks for your help!