Dynamic Data

191 views
Skip to first unread message

Steve Bistline

unread,
Jun 5, 2013, 2:40:29 PM6/5/13
to shiny-...@googlegroups.com

I just found Shiny and have a use case that I am wondering about. Looking at the Stock example on the website, the stocks displayed are hard coded. Is there a way to make this list dynamic, such that I can pass in or read a list of stocks that would be added to the UI and passed on for analysis?

Thanks,

Steve

Joe Cheng

unread,
Jun 6, 2013, 3:45:01 AM6/6/13
to shiny-...@googlegroups.com
Yes. This way is complicated but it allows the user to add new symbols from the web browser. Let's say you can have a textInput called "symbol" and an actionButton called "addSymbol". Then you could have something like this in server.R:

shinyServer(function(input, output) {

  # Include the make_chart code, etc...

  values <- reactiveValues()
  values$stocks <- character(0)

  observe({
    if (input$addSymbol == 0)
      return()
    isolate({
      if (!(input$symbol %in% values$stocks)) {
        values$stocks <- c(values$stocks, input$symbol)
        output[[paste0('plot_', input$symbol)]] <- renderPlot({ make_chart(input$symbol) })
      }
    })
  })

  output$plots <- renderUI({
    lapply(values$stocks, function(symbol) {
      list(
        br(),
        div(plotOutput(outputId = paste0('plot_', symbol)))
      )
    })
  })

})

Then in ui.R you'd have to replace the contents of mainPanel with "uiOutput('plots')".

The observe/check-for-zero/isolate sequence probably looks strange, but it's a Shiny idiom that is used for responding to a button click. (We should wrap it up as a friendlier function, like handleButtonClick)

There are also examples of passing datasets or whatever in from the R console, here's an example:

If you execute that code, then run explore(df) where df is a dataframe of your choice, you'll see what I mean.


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

halldor bjornsson

unread,
Jun 6, 2013, 11:22:17 AM6/6/13
to shiny-...@googlegroups.com
Joe

When I run explore on the diamonds dataset I get a complaint from runApp.
It interprets the app argument in runApp(app,....) as the startup directory and
complains that it did not get a text string.


Is there some tweak that is needed to get this to work?

Sincerely
Halldór

Joe Cheng

unread,
Jun 6, 2013, 8:28:14 PM6/6/13
to shiny-...@googlegroups.com
You need the very latest version of Shiny that's on CRAN (0.6.0). This is a very recent version and the CRAN Windows binary has not been updated yet. If you're on Mac or Linux then reinstalling Shiny from CRAN will do it, if not and you're on R 3.0.0 or later then you can use this instead:

if (!require('devtools'))
  install.packages('devtools')
devtools::install_github('shiny', 'rstudio')


--

Zhuo Jia Dai

unread,
Jun 7, 2013, 6:06:55 AM6/7/13
to shiny-...@googlegroups.com
Hi all,

A follow on question. It sounds like it's technically possible to develop an application where the user can upload a file (e.g. csv could be up to 100G in size) to the server, and design the Shiny app to point to whichever file the user has uploaded and then perform the analysis?

Joe Cheng

unread,
Jun 7, 2013, 12:14:40 PM6/7/13
to shiny-...@googlegroups.com

Zhuo Jia Dai

unread,
Jun 9, 2013, 10:26:50 AM6/9/13
to shiny-...@googlegroups.com
options(shiny.maxRequestSize, 30*1024^2)

I tried to follow the tutorial and put the above on "top" of the server.R. No matter where I tried it errors with

Error in options(shiny.MaxRequestSize, 30 * 1024^2) : 
  object 'shiny.MaxRequestSize' not found

The following is my code


options(shiny.MaxRequestSize, 30*1024^2) #limit to 1Gigabyte    
shinyServer(
function(input, output) {

  output$contents <- renderTable({
    # input$file1 will be NULL initially. After the user selects and uploads a 
    # file, it will be a data frame with 'name', 'size', 'type', and 'datapath' 
    # columns. The 'datapath' column will contain the local filenames where the 
    # data can be found.

    inFile <- input$file1

    if (is.null(inFile))
      return(NULL)
    
    read.csv(inFile$datapath, header=input$header, sep=input$sep, quote=input$quote)
  })
})

Zhuo Jia Dai

unread,
Jun 9, 2013, 10:53:45 PM6/9/13
to shiny-...@googlegroups.com
Could it be that i am running this locally on my Windows system? I am testing the program by copying the code into ui.R and server.R and running it using runApp("c:/shiny/app1")

BTW Thanks for such a promising looking piece of software in Shiny. Really getting excited about it.

Zhuo Jia Dai

unread,
Jun 10, 2013, 12:45:17 AM6/10/13
to shiny-...@googlegroups.com

options(shiny.maxRequestSize= 30*1024^2)

Seems to have worked. I am noob to R so didn't spot that typo. Please correct in the tutorial.

Joe Cheng

unread,
Jun 10, 2013, 2:18:03 PM6/10/13
to shiny-...@googlegroups.com
Doh! Sorry for the typo, thanks sgibb for the fix.
Reply all
Reply to author
Forward
0 new messages