0

I was wondering whether it is possible to start a shiny app with an argument being passed to it.

For example, say I want to do a histogram of some data set. If I know the data set ahead of time then I can save it in the corresponding directory and source it in once the app is running. But what if I don't know what data set a user might want to use?

I have actually thought of 2 so-so solutions:

1) I can have a small wrapper routine in R that first writes the data to some temp directory, then within shiny I can use scan or read. The reason I don't like this is that I want the routine to run on other peoples computers, irrespective of their folders and operating system, and I am sure this won't work everywhere.

2) Similar to the first solution, except I use dump("data","clipboard) in R and then source("clipboard") in shiny. Again, this works fine on my computer but I am afraid it won't necessarily work on others.

So my question, is there a more elegant solution that can be relied upon to work on (almost) all computers?

Here is a simple example of the "copy to clipboard" idea: say i want to display a scatterplot in Shiny. First i write a routine in R:

shinyPlot <- function (x) 
  {
    dump("x","clipboard")
    runApp("c:/r/shiny/test")
  }

Now I have ui.R

shinyUI(fluidPage(
  sidebarLayout(
      sidebarPanel(),
      mainPanel(
        plotOutput("plot")
      )
  )    
))    

and server.R

shinyServer(function(input, output) {

  data <- reactive({
    source("clipboard")
    x
  })          
  output$plot <- renderPlot({
      plot(data())
  })
})

Now I can run in R

shinyPlot(faithful)

and this works fine on my computer. But as I said, i am not sure that it would work on all computers. At any rate, is there is a more elegant solution?

Wolfgang Rolke
  • 795
  • 4
  • 16
  • It might be more easy to help you if you can provide some example code. Based on your example solutions it seems like there may be more going on in your app than I would've thought based on the question above, so it's a little unclear what constraints you may be facing. Note that many applications create temp directories upon installation and you can define those directories based on OS by using control-flow constructs and determining the user's OS from `Sys.info()['sysname']`. Finally, I edited your post to improve grammar but would suggest capitalizing the word "I" and that sort of thing. – Hack-R Mar 19 '16 at 14:28
  • If you use Shiny with web browsers, see http://stackoverflow.com/questions/32872222/how-do-you-pass-parameters-to-a-shiny-app-via-url – Xiongbing Jin Mar 19 '16 at 15:18
  • Ok, here is some sample code. Say I want to display a scatterplot in a browser, so i have shinyUI(fluidPage( sidebarLayout( sidebarPanel(), mainPanel( plotOutput("plot") ) ) )) – Wolfgang Rolke Mar 20 '16 at 11:46
  • Sorry for the comment above, something weird is happening, when i use Enter i get thrown out of the editing panel. Maybe something wrong with my keyboard? – Wolfgang Rolke Mar 20 '16 at 11:55
  • @warmflow I checked the link but I don't know what they mean by Example URL to test: 127.0.0.1:5767/?symbol=BBB,AAA,CCC,DDD&date_start=2005-01-02&period_select=2&smaLen=153&usema=1 when I run runUrl("Example URL to test: 127.0.0.1:5767/?symbol=BBB,AAA,CCC,DDD&date_start=2005-01-02&period_select=2&smaLen=153&usema=1") i get an error message Unknown file extension and when i copy-paste the link into a browser it says Unable to connect. At any rate, i read the help on session$clientData and it is not obvious to me how i would use this to pass a whole data set to Shiny? – Wolfgang Rolke Mar 20 '16 at 12:30
  • I'll try to get a working example for you. – Xiongbing Jin Mar 20 '16 at 15:23
  • Please clarify: you said you want user to be able to choose which data they want to use. Will these datasets be available in the working directory (as different csv files)? Or you want user to be able to use any arbitrary data files stored anywhere? – Xiongbing Jin Mar 20 '16 at 15:41
  • the idea is that a user starts the routine (here shinyPlot) from within an R session and can use any arbitrary data set as the argument. Of course, if there is a finite set of predefined data sets this is much simpler, just put them somewhere on the web and use the URL in source. – Wolfgang Rolke Mar 21 '16 at 13:58
  • Here is why i am trying to do this: I am a Professor of Statistics. The population of some of my course is mildly math-phobic and without any computer programming experience. I want to use R but the learning curve is just to steep. So i have number of Shiny apps to make it easier. Say the student is asked to investigate data from a quantitative variable. So they should calculate summary statistics like mean or median, or maybe do a histogram. The routine eda brings up Shiny and now its just point and click from there. Of course the best way would be if they can just type eda(data) – Wolfgang Rolke Mar 21 '16 at 14:20

0 Answers0