Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 200
WetlabStudent Avatar asked Mar 10 '13 20:03

WetlabStudent


People also ask

Is R Shiny difficult to learn?

Along with Shiny elements, you can use HTML elements to stylize your content in your application. In my opinion, R Shiny is very easy to learn despite how powerful the tool is. If you're working on a side project or looking to add something to your portfolio, I highly recommend trying it out.

What is UI and server in R Shiny?

Shiny applications have two components, a user interface object and a server function, that are passed as arguments to the shinyApp function that creates a Shiny app object from this UI/server pair.

How do you enter Shiny data?

To add an input in a Shiny app, we need to place an input function *Input() in the ui object. Each input function requires several arguments. The first two are inputId , an id necessary to access the input value, and label which is the text that appears next to the input in the app.


1 Answers

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.

like image 158
WetlabStudent Avatar answered Sep 19 '22 09:09

WetlabStudent