R 闪亮:如何将输入数据保存到服务器或全局访问输入变量? [英] R shiny: how to save input data to the server or access input variables globally?

查看:38
本文介绍了R 闪亮:如何将输入数据保存到服务器或全局访问输入变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个应用程序,向用户询问一些基本的调查问题.完成后,他们被要求通过滑动条提供数字输入,按继续,然后生成一个绘图,再次要求用户输入,更新绘图等.第一个输入应该是绘图上的 y1,第二个输入应该是 y1输入应该是图上的 y2,等等.但另外我想保存用户输入的数据,以便我可以在我的 R 脚本中全局访问它,这样它就可以使用 sendmailR 发送给我,或者它可以作为文本文件下载到我的计算机上.但我无法弄清楚如何做到这一点.这是我目前所拥有的.

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

})

这是ui.R文件

###########################################
#####   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  

此外,我的 for 循环试图一遍又一遍地生成绘图是行不通的,我假设我需要做一些反应性的事情,但我需要找出一种方法来绘制过去所有存储在 harv 中的用户定义条目.我查看了 downloadHanlder,但这会在用户的计算机上下载数据和绘图.

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.

推荐答案

答案是在 ShinyServer 函数之外定义一个变量.然后使用 <<- 而不是 <-= 在反应式函数中进行全局赋值.然后你可以在反应式函数之外访问它.但是,您只能在应用程序运行时访问它,但这对于将输入通过电子邮件发送给自己或将输入写入文本文件不是问题.

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.

这篇关于R 闪亮:如何将输入数据保存到服务器或全局访问输入变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆