6

do you see a way to run a computation in R while waiting for a user input?

I'm writing a script that makes differents types of plots which are defined by user input, but in first lot of data has to be loaded and processed. But in fact, user could start defining what he wants already while the processing is running - that's what I would like to do!

I think package Rdsn might provide the functionality that I need, but I was not able to figure out how.

Thanks!

1 Answers1

0

You didn't give me much context, nor reproducible code, so I will just provide a simple example. I am not familiar with the Rdsn package, so I will use provide a solution I know.

# create a function to prompt the user for some input
readstuff = function(){
  stuff = readline(prompt = "Enter some stuff: ")
  # Here is where you set the condition for the parameter
  # Let's say you want it to be an integer
  stuff = as.integer(stuff)
  if(is.na(stuff)){
    return(readstuff())
  } else {
    return(stuff)
  }
}

parameter = readstuff()

print(parameter)
print(parameter + 10)

The key here is to "source" the script instead of "running" it. You can find the "source" button on the top right of RStudio. You can also use source(yourscript) to source it.

So for every parameter you want to prompt the user for input, just call readstuff(). You can also tweak it a little to make it more general. For example:

# create a function to prompt the user for some input
readstuff = function(promptMessage = "stuff", class = "integer"){
  stuff = readline(prompt = paste("Enter the", promptMessage, ": "))
  # Here is where you set the condition for the parameter
  # Let's say you want it to be an integer
  stuff = as(stuff, class)
  if(is.na(stuff)){
    return(readstuff(promptMessage, class))
  } else {
    return(stuff)
  }
}

plotColor = readstuff("plot color", "character")
size = readstuff("size parameter")
xvarName = readstuff("x axis name", "character")

df = data.frame(x = 1:100, y = 1:100)

library(ggplot2)
p = ggplot(df, aes(x = x, y = y, size = size, color = plotColor)) + 
  labs(x = xvarName) + geom_point()
print(p)

The if(is.na(stuff)) statements won't work if class is character, but I won't get into details on how to fix that, since this question is mainly about how to wait for user input. There are also ways to suppress the warning messages if the user entered something other than what is intended, but again, a bit off topic to talk about it here.

One important thing you have to watch out for is that anything you want R to print or plot, you need to wrap it with a print() function. Otherwise sourcing it won't print nor plot anything. Also, when typing in a parameter that is intended to be a string or character, don't add quotes. For example, for plotColor, type red instead of "red" in the prompt.

Most of the readline code are referenced from here:

acylam
  • 18,231
  • 5
  • 36
  • 45
  • Well this is not really what I wanted to know. I was thinking about a situation like this: `label<-readline("Tell me what do you want to be the tittle") #ask user question that requires lot of thinking data.raw<-read.table(...) #read massive file data.processed<-do.something(data.raw) #do time demanding processing plot(data.processed, main=label) #plot the data with parameter that user specified at the beginning ` So I want that the reading and processing starts immediately so that is not slowed down by the user deciding what he wants the plot to be labeled. – Michal Svoboda Oct 27 '16 at 20:44
  • @MichalSvoboda Well, does the user also have to provide their own data? or is the "massive file" already included? – acylam Oct 27 '16 at 23:27
  • The idea is that the program first loads some big file with all the data and then the user selects some subset of this from which he wants a report to be generated. The file is not very well structured and requires lot of processing, but that is what we have - I thought that the user might start specifying the subset before the processing is finished. The user does not need to specify path to the file or anything, because the file is the same, but is continually updated by a data acquisition system. – Michal Svoboda Oct 31 '16 at 20:09
  • @MichalSvoboda My understanding is that the processing, instead of reading in the massive data itself is taking the most time. If that's the case, it doesn't make sense to me why you would want to process the data _every time_ a user wants a report. Since the data is the same, and the information from the user is only needed _after_ the processing is done, the most logical thing to do is to only run the processing once, save the processed data, and only load the final data when a user wants to plot something. This way, no computing time is wasted in processing when it can be done beforehand. – acylam Nov 01 '16 at 03:03