How to save data from user input for later use?

4,665 views
Skip to first unread message

Matthew Holden

unread,
Mar 11, 2013, 2:08:46 PM3/11/13
to shiny-...@googlegroups.com

I am writing an app that will asks the user to submit a numeric value via a slidebar, will then click a push button to continue, a plot is generated with that numeric value, call it y1, as a pont (1, y1) in an x,y plot. the app will then ask the user to do it again, he enters a new value, y2, and the new plot is of (1, y1) and (2, y2).

I would like to have access to the data the user inputted to analyze later. If I could some how get it so that the user input got stored globally such the Rscript could access it I could just have the data emailed to me via sendmailR. How do I get access to the user input? I also am having problems with the re-plotting after each time step. I've looked over all the tutorial material, but am still confused on these issues.

Joe Cheng

unread,
Mar 11, 2013, 6:32:32 PM3/11/13
to shiny-...@googlegroups.com
To keep a running list of the values the user has inputted, declare a variable at a per-session level, and simply add new values to it as the user gives more inputs. Whenever you add to it, you could also write.csv the data to a local file--just make sure to generate a unique file name when the session begins, i.e.:

shinyServer(function(input, output) {
  logfile <- paste(
    './userlogs/',
    format(Sys.time(), "%Y-%m-%d_%H-%M-%S"),
    round(runif(1, min=10000, max=99999)),
    '.csv',
    sep='')
  values <- data.frame(x=numeric(0), y=numeric(0))

  observe({
    # Assuming your saveButton is an actionButton, from the shiny-incubator package
    if (input$saveButton == 0)
      return()
    isolate({
      values <- rbind(values, c(x=input$x, y=input$y))
      write.csv(values, logfile)
    })
  })
})


On Mon, Mar 11, 2013 at 11:08 AM, Matthew Holden <matthe...@gmail.com> wrote:

I am writing an app that will asks the user to submit a numeric value via a slidebar, will then click a push button to continue, a plot is generated with that numeric value, call it y1, as a pont (1, y1) in an x,y plot. the app will then ask the user to do it again, he enters a new value, y2, and the new plot is of (1, y1) and (2, y2).

I would like to have access to the data the user inputted to analyze later. If I could some how get it so that the user input got stored globally such the Rscript could access it I could just have the data emailed to me via sendmailR. How do I get access to the user input? I also am having problems with the re-plotting after each time step. I've looked over all the tutorial material, but am still confused on these issues.

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Vincent

unread,
Mar 12, 2013, 3:53:06 AM3/12/13
to shiny-...@googlegroups.com
Is it likely Shiny will have a feature where you can (1) save the input-object directly to a file and (2) load it back-in with a button click? The next time you use your shiny app, input you could replaced it with the previous state.

Joe Cheng

unread,
Mar 13, 2013, 5:45:15 PM3/13/13
to shiny-...@googlegroups.com
Yeah, it's on our list. We are getting close to the point where you'd be able to hack it in yourself (need access to the URL and the ability to set values on inputs) but we also would like to have this as a first-class feature.

Matthew Holden

unread,
Mar 18, 2013, 2:12:02 PM3/18/13
to shiny-...@googlegroups.com
This is great the <<- does work, also as the first part of the post. I want the user to basicallly enter input that renders a graph, but then I want the page to clear and ask the user to do something similar, clear page, and repeat. I looked through the Dynamic UI tutorial but don't think I can do it via what was mentioned on that page. Is there a clear page function in Shiny?

Joe Cheng

unread,
Mar 18, 2013, 2:20:39 PM3/18/13
to shiny-...@googlegroups.com
There is not, but calling "window.location.reload()" in JavaScript might achieve that effect (though it will also start a new session). So you could do something like this:

In ui.R:
uiOutput("reload")

In server.R:
output$reload <- renderUI({
  if (input$saveButton > 0) {
    tags$script("window.location.reload();")
  }
})

So that JS would be pushed to the client only after saveButton was pressed.

Matthew Holden

unread,
Mar 20, 2013, 1:17:08 PM3/20/13
to shiny-...@googlegroups.com
Actually I was using a submit button I did not know about the incubator packages, sorry very new to Shiny. Since reloading the page starts a new session, I've decided to abandon that idea in the middle of the app. I think I am almost done with my problem now, hopefully my last question is how do you get the action button to behave like a submit button (+ the action feature)? Right now everything is working fine with a submit button. However after the user has chosen N numbers, the app as now written produces an error, I'd prefer if the next time he hits the button the app ended (perhaps by reloading the page, which works great by the way!). I figure the way to do this is to use an action button and reload the page if it is hit more than N times.

Here is my code:

###########################################
#####            ui.R   ###################
###########################################

library(shiny)
library(shinyIncubator)

#Define UI for app
shinyUI(pageWithSidebar(
  
  sidebarPanel( checkboxInput("consent", "I am 18 or older", FALSE)),  
  
  #title  
  headerPanel("Fishing Game"),
  
  mainPanel(    
    selectInput("major", "Survey Question1, provide info about yourself:",
                list("answer 1"="a1","answer 2" = "a2")),
    
    helpText("","","Now Lets Play the Game",""), 
    helpText("","","Directions: Choose a number between zero and one. 
             Hit the button, then repeat. Watch the graph 
             to see your history of chosen numbers",""), 
    
    sliderInput("harvest1", "Enter number between 0 and 1", 
                min = 0, max = 1, value = 0.5, step= 0.01),
    
#    actionButton("saveButton", "Enter, and continue"),
    
    submitButton("Enter, and continue"),
    
    plotOutput("yieldplot1")
    
  )#mainpanel
  
))#shinyUI


#####################################
###                  server.R                          ##
#####################################
library(shiny)
library(shinyIncubator)
n=6; 
Time=1:n;
harv = rep(0,n) #harvesting a messure of the % of fish to catch
k = 1; #counter
# define server logic

shinyServer(function(input, output){
  
  # generate a plot of the current harv vector vs. 1:n

  
  output$yieldplot1 <- renderPlot({
    
    harv[k] <<- input$harvest1
    k <<- (k+1)
    plot(Time, harv, type='p', ylim=c(0,1))
    write(harv, file="FishGameResultsShiny10.txt")
  })
  
#   output$reload <- renderUI({
#     if (input$saveButton > 0) {
#       tags$script("window.location.reload();")
#     }
#   })
   
}) #end ShinyServer

Matthew Holden

unread,
Mar 20, 2013, 3:50:32 PM3/20/13
to shiny-...@googlegroups.com
Also I want harv to get updated when the button is pressed and the slider did not move (i.e. if I move the slider to 0.6 hit the button then harv[k] =.6, then if I hit the button again without moving the slider I want harv[k+1]= 0.6 as well)

Matthew Holden

unread,
Apr 1, 2013, 7:12:43 PM4/1/13
to shiny-...@googlegroups.com
I figured out an answer to the question two posts above (just use a global counter variable that gets increased using "<<-" every time the plot function is called, and then make the reload script depend on the counter variable (so no need for an action button to solve this)

However the last question I am still very stuck on after more than a weeks worth of searching/trying things. I really need to figure out a way for the plot function in the server script to get called when I hit the submit button even if the slider input has not changed. Does anyone have any ideas. Here is a link to the app, 

Joe Cheng

unread,
Apr 1, 2013, 9:09:03 PM4/1/13
to shiny-...@googlegroups.com, shiny-...@googlegroups.com

The answer to your last question is that you should use actionButton and isolate together. I'm answering this from my phone or I would dig up a link for you, but I wrote a more or less detailed answer about actionButton and isolate to another user on this list earlier today. If you can't find it, let me know. 


Also I didn't see a link to your app in your previous email. If you need more help please send it along and I'll take a look. 

Thanks and sorry for the lack of replies to your previous two emails. It's been busy around here!

Vincent

unread,
Aug 4, 2015, 1:05:42 AM8/4/15
to Shiny - Web Framework for R
I'm curious why this approach doesn't work anymore. I know you can use 'onclick' in a button definition but I'm curious if there is still a way to run javascript in a similar manner. I would like to see if something like this could be used to reset the state of a DT table using "table.state.clear();" where 'table' is the id of the particular DT table

output$reload <- renderUI({
  if (input$saveButton > 0) {
    tags$script("window.location.reload();")
  }
})

Reply all
Reply to author
Forward
0 new messages