I find the usecase artificial, and I should caution that I've worked on many shiny apps, and never had to do anything close to this, but it literally achieves what it seems you say you want to achieve.
library(tidyverse)
library(shiny)
library(lubridate)
library(ggplot2)
set_up_lists <- function(env,x){
assign("base.list",value = reactive(
{
req(x())
as.list(x()[,1])
}
),envir = env)
assign("launch.list",value = reactive(
env$base.list()
),envir = env)
}
ui <- fluidPage(
titlePanel("What if? Trend"),
sidebarLayout(
sidebarPanel(
fileInput("upload.file", "Upload trend data file",
multiple = FALSE,
accept = ".csv"),
uiOutput("base.date.output"),
uiOutput("launch.date.output")
),
mainPanel(
tableOutput("date.list")
)
)
)
server <- function(input, output, session) {
t.input <- eventReactive(input$upload.file, {
read_csv(input$upload.file$datapath)
})
set_up_lists(env=environment(),x=t.input)
output$date.list <- renderTable({
base.list()
})
# Specify parameters ----
output$base.date.output <- renderUI({
selectInput("base.date",
label = "Select base date",
choices = base.list(),
selected = "")
})
output$launch.date.output <- renderUI({
selectInput("launch.date",
label = "Select launch date",
choices = launch.list(),
selected = "")
})
}
shinyApp(ui, server)
I think really, if you wanted to just have the code appear elsewhere (even though it shuold be in your app server code, then you would normally write yourself a little R file, that has exactlly the original statements, and simply source that file into the server ({}) section of your app.
You would typically use traditional 'functions' to transform data, this would happen within reactives or observes, it wouldnt substitute for reactives in the way this example seems to want to do.,,,