I have created a survey in Shiny and I would like that the options in this survey are always different. To do so, I have used the sample
function and each time I run the app on my local machine the possible options for the answers are always different as I want. However, I have recently deployed the app through shinyapps.io and it seems that the there is no more randomness in the possible options. This is the code of my shiny app :
# Loading the needed libraries
library(shiny)
library(shinythemes)
library(googlesheets4)
library(googledrive)
library(shinyalert)
setwd('C:/Users/alber/Desktop/UniTn/Data Science/Third Semester/Laboraotry of Business and Customer analytics/Project_Real')
#gs4_auth(cache = ".secrets") #for the first time
gs4_auth(cache = ".secrets", email = TRUE, use_oob = TRUE) # when you deploy
sheet_id <- "1-l3D2dhWjwv1hWXs97db08pJUKZ3DF1DZ4d4yWAVsik"
#sheet_id <- "1MdqGpii3hfoG1OcvlAQjbQ171UOwxCR3Qfc8aIKfZIo"
# Let0s define the demographic variables that will constitute the first part
# of our survey. These infos could be then used for market segmentation
# Defining the Demographic Variable
ages_interval <- c('0-18', '19-25', '25-35', '35-45', '45-55', '55-65', '>65')
sex_interval <- c('Male', 'Female', 'Other')
educ_interval <- c('Middle school', 'High School', 'Bachelor\'s', 'Master\'s', 'PhD')
Marital_Status <- c('Married', 'Not Married')
job_sector <- c('Agricolture', 'Industry', 'Services', 'Student')
# In the following line of code we will implement a function that creates random product
# profiles given the attributes.
# More precisely, trading platform has been chosen as product of the analysis.
# Now will follow the attributes that we will use to investigate the preferences of
# possible customers.
platform_type <- c('Web App', 'Desktop App', 'Mobile App')
deposit_minmax <- c('min 0€ max 1000€', 'min 10€ max 10000€', 'min 100€ max infinte')
fees_on_purchases <- c('0%', '0.015%', '0.025%')
#https://www.investopedia.com/terms/f/financialinstrument.asp
financial_instruments <- c('Stocks', 'Crypto', 'ETFs', 'Commodities')
leverage <- c('YES', 'NO')
social_copy <- c('YES', 'NO')
n_a <- 5
# Now that we have defined the attributes and their levels we can implement a function
# that creates random profiles
create_options <- function(){
list_prod <- c()
for(i in 1:1000){
# initialize the product profile
prod_prof <- c(
paste('Platform Type:', sample(platform_type,1), '|',
'Amount of Deposit:', sample(deposit_minmax,1), '|',
'Fees on buy & sell orders:', sample(fees_on_purchases,1), '|',
'Financial Instruments:', sample(financial_instruments,1), '|',
'Leverage:', sample(leverage,1), '|',
'Social/Copy Trading', sample(social_copy,1))
)
# in order to avoid clones
if (is.element(prod_prof, list_prod) == FALSE){
list_prod <- append(prod_prof, list_prod)
}
}
return (list_prod)
}
################################################################################
# START DEVELOPING THE APP
# User Interface
ui <- fluidPage(
# Theme
theme = shinytheme("cerulean"),
# Creating a navigation bar
navbarPage( h1('Trading App Survey'),
tabPanel(
h3('Survey'),
h5('You may have noticed that filling up your car, going to the hairdresser
and shopping for groceries have become more expensive lately.
While some things are cheaper than a year ago, overall we are paying more for what we buy.
This is what we call inflation.
This November 2021 inflation hit its highest level in 13 years. This is happening
for three main reasons: our economy is reopening fast, higher energy prices are pushing up
inflation, and something that statisticians call the "base effect". Due to inflation there is
the risk that the value of your savings decreases.'),
h5('At the same time, there is an increasing interest in share-trading apps.'),
h5('For the above reasons this could be the right time to invest and these relatively
new share-trading apps could be the "sexiest" solutions for small investors.'),
h5('Hence, with the following questionnaire, we would like to understand what you customers find relevant for these apps. '),
h5('Please remember that this survey HAS NO MARKETING PURPOSES and it is
thought just for EDUCATIONAL PURPOSES. Lastly, this survey is fully compliant with GDPR.'),
h4('NB: In the Appendix section are provided some definitions in order to make less experienced
customers more confortable.'),
# Page Title
h3("Demographic Questions"),
tags$head(tags$style(type='text/css', ".span4 { max-width: 600px; }")),
# Age
selectInput("Age", "Please indicate your age", ages_interval,multiple = FALSE),
# Gender
selectInput("Gender", "Please indicate your gender", sex_interval, multiple = FALSE),
# Education
selectInput("Education", "Please indicate your education level", educ_interval, multiple = FALSE),
# Job
selectInput("Job", "Please indicate your job sector", job_sector, multiple = FALSE),
# Home Country
textInput('Country', 'Please type the name of your home country (en)'),
# Marital Status
radioButtons("Marital_Status", "What is your marital status?", Marital_Status),
h2('Questionnaire on product preferences'),
# 1st Question
checkboxGroupInput('Choice1', 'Which product do you prefer ? \n(Please pick ONLY ONE)', sample(create_options(),3, replace = F)),
# 2nd Question
checkboxGroupInput('Choice2', 'Which product do you prefer ? \n(Please pick ONLY ONE)', sample(create_options(),3, replace = F)),
# 3rd Question
checkboxGroupInput('Choice3', 'Which product do you prefer ? \n(Please pick ONLY ONE)', sample(create_options(),3, replace = F)),
# 4th Question
checkboxGroupInput('Choice4', 'Which product do you prefer ? \n(Please pick ONLY ONE)', sample(create_options(),3, replace = F)),
# 5h Question
checkboxGroupInput('Choice5', 'Which product do you prefer ? \n(Please pick ONLY ONE)', sample(create_options(),3, replace = F)),
# 6th Question
checkboxGroupInput('Choice6', 'Which product do you prefer ? \n(Please pick ONLY ONE)', sample(create_options(),3, replace = F)),
# 7th Question
checkboxGroupInput('Choice7', 'Which product do you prefer ? \n(Please pick ONLY ONE)', sample(create_options(),3, replace = F)),
# 8th Question
checkboxGroupInput('Choice8', 'Which product do you prefer ? \n(Please pick ONLY ONE)', sample(create_options(),3, replace = F)),
# 9th Question
checkboxGroupInput('Choice9', 'Which product do you prefer ? \n(Please pick ONLY ONE)', sample(create_options(),3, replace = F)),
# 10th Question
checkboxGroupInput('Choice10', 'Which product do you prefer ? \n(Please pick ONLY ONE)', sample(create_options(),3, replace = F)),
# 11th Question
checkboxGroupInput('Choice11', 'Which product do you prefer ? \n(Please pick ONLY ONE)', sample(create_options(),3, replace = F)),
# 12th Question
checkboxGroupInput('Choice12', 'Which product do you prefer ? \n(Please pick ONLY ONE)', sample(create_options(),3, replace = F)),
#downloadButton('Results', label = 'Conclude the survye'),
useShinyalert(),
actionButton("submit", "Submit"),
),
tabPanel(h3('Appendix'),
h2('Glossary'),
)) )
# Define server function
server <- function(input, output) {
observeEvent(input$submit, {
results_d <- data.frame(input$Age, input$Gender, input$Education, input$Job,
input$Country, input$Marital_Status)
sheet_append(data = results_d, ss = sheet_id, sheet = 'Demography')
results_s <- data.frame(input$Choice1, input$Choice2, input$Choice3,
input$Choice4, input$Choice5, input$Choice6,
input$Choice7, input$Choice8, input$Choice9,
input$Choice10, input$Choice11, input$Choice12)
sheet_append(data = results_s, ss = sheet_id, sheet = 'Survey_Answers')
shinyalert("Thank you!", "Your answers have been collected. You can close the survey", type = "success")
})
}
# Create Shiny object
shinyApp(ui = ui, server = server)
How can I make it work when I deploy the app?
Thank you in advance for your help