Help with shiny basics

I'm a retired professor trying to use R to create online apps for the methods in my widely used textbook. I can handle basic R but tiny is driving me crazy. I'd really appreciate some help with some basic shiny concepts that have escaped me.

The enclosed script illustrates a current problem. It reads a .csv file like the one I've copied below the script and loads it into two selectInputs. It works fine in the body of the script but not if I comment that out and try to do it with my create_date_lists function. I guess I don't know how to use reactive expressions in function calls. I'd appreciate any help you can give me on this,

library(tidyverse)
library(shiny)
library(lubridate)
library(ggplot2)

create_date_lists <- function(t.input){
  base.list <-reactive(
    as.list(t.input[,1])
  )
  
  launch.list <-reactive(
    base.list()
  )
  dates <- c(base.list(),target.list())
  return(dates)
}

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) {
  
  # read trend data file
  t.input <- eventReactive(input$upload.file, {
    read_csv(input$upload.file$datapath)
  })
  
   base.list <-reactive(
      {
        req(t.input())
        as.list(t.input()[,1])
      }
    )
     
   launch.list <- reactive(
       base.list()
   )
  
#   reactive(
#      {
#        create_date_lists(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)

|date|cases|
|---|---|
|2020_01|21.112|
|2020_02|27.881|
|2020_03|44.051|
|2020_04|70.278|
|2020_05|86.942|
|2020_06|136.395|
|2020_07|256.782|
|2020_08|415.387|
|2020_09|483.024|
|2020_10|545.837|
|2020_11|665.865|
|2020_12|691.893|

Can you edit you post so that all the code is in the code chunk?

Note, you had an ellipsis at the start rather than the backtick.

Update: edited for you below:

library(tidyverse)
library(shiny)
library(lubridate)
library(ggplot2)

create_date_lists <- function(t.input){
    base.list <-reactive(
        as.list(t.input[,1])
    )
    
    launch.list <-reactive(
        base.list()
    )
    dates <- c(base.list(),target.list())
    return(dates)
}

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) {
    
    # read trend data file
    t.input <- eventReactive(input$upload.file, {
        read_csv(input$upload.file$datapath)
    })
    
    base.list <-reactive(
        {
            req(t.input())
            as.list(t.input()[,1])
        }
    )
    
    launch.list <- reactive(
        base.list()
    )
    
    reactive(
        {
            create_date_lists(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)

The reactive expressions should probably have curly brackets and possibly be in the server side.

create_date_lists <- function(t.input){
    base.list <-reactive({ # added curly brackets
        as.list(t.input[,1])
    })
    
    launch.list <-reactive({ # added curly brackets
        base.list()
    })
    dates <- c(base.list(),target.list())
    return(dates)
}

Thanks for getting back to me!

I'm sorry but I don't know how to delete my posting and how to format it correctly. :frowning:

Three dots on the bottom right of the post to get extra options, then you can delete from there.

Thanks!

I think I deleted the duplicate post and tried adding { } to the function and moving it to the Server part of the code but it still doesn't work. Apparently, I'm still missing something.

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.,,,

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.