R shiny: how to save input data to the server or access input variables globally?
Asked Answered
O

1

22

I am making an application that asks the user a few basic survey questions. When this is done they are asked to provide a numeric input via a slidebar, press continue, then generate a plot, asks the user for input again, updates the plot, etc. The first input should be y1 on the plot, and the second input should be y2 on the plot, ect. But in addition I would like to save the data the user is inputting, so that I can access it in my R script globally, so it can be sent to me using sendmailR or so that it could be downloaded onto my computer as a text file. But I am having trouble figuring out how to do this. Here is what I have so far.

n=10 #number of times to ask the user for input which will be stored in harv[i]
Time = seq(n)
harv = rep(0,n) #initializing vector for storage of user input at time 1 through n

############### define server logic

shinyServer(function(input, output){

  # Compute the forumla text in a reactive expression since it is 
  # shared by the output$caption and output$mpgPlot expressions
  for(i in Time){

  # generate a plot
  output$yieldplot <- renderPlot({
   harv[i] = input$harvest
   plot(Time, harv, type='p', ylim=c(0,1))
  })

 }#for

})

Here is the ui.R file

###########################################
#####   User Interface  ###################
###########################################

library(shiny)

#Define UI for app
shinyUI(pageWithSidebar(

  #title  
  headerPanel("Game"),
  mainPanel(   selectInput("workexp", "Have you ever been employed:",
                            list("No"="no", "Yes" = "yes")),    
               sliderInput("push", "Choose a number", 
                           min = 0, max = 1, value = 0.5, step= 0.01),
               submitButton("Enter"),
               plotOutput("yieldplot")                                                  
  )#mainpanel

))#shinyUI  

Also my for loop to try and generate the plot over and over will not work, I assume I need to do something reactive but I need to figure out a way to plot past user defined entries all stored in harv. I looked into downloadHanlder but this downloads data and plots on the user's computer.

Overwhelming answered 10/3, 2013 at 20:57 Comment(0)
O
21

The answer is to define a variable outside of the shinyServer function. Then make a global assignment in the reactive functions by using <<- instead of <- or =. Then you have access to it outside of the reactive functions. However, You only have access to it while the application is running, but this isn't a problem for emailing yourself the input or writing the input to a text file.

Overwhelming answered 14/3, 2013 at 21:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.