Single-file Shiny

115 views
Skip to first unread message

Jean-François Moyen

unread,
Mar 31, 2015, 7:31:49 AM3/31/15
to shiny-...@googlegroups.com
Hi,

More experimenting with Shiny... I was willing to try the "single file" approach, as per
http://shiny.rstudio.com/articles/function.html

I have the following minimum function, borrowed from
https://github.com/rstudio/shiny-examples/tree/master/084-single-file

__________________________________________
shiny_test.r
------------------------
require("shiny")
# Global variables can go here
n
<- 200
# Define the UI
ui
<- bootstrapPage(
numericInput
('n', 'Number of obs', n),
plotOutput
('plot')
)
# Define the server code
server
<- function(input, output) {
output$plot
<- renderPlot({
hist
(runif(input$n))
})
}
# Return a Shiny app object
cat
("before\n")
shinyApp
(ui = ui, server = server)
cat
("After\n")


------------------------------------------------------------------------

When I source the file (shiny_test.r), nothing happens. To me more specific -- "before" and "after" are printed all right, but the app does not start and that's all there is to say.

However, if (after having sourced the file once obviously) I copy the "shinyApp().." line and paste it into the console, the app starts alright in Firefox.

Any explanation/solution ?

Thanks

(R 3.1.3, Rgui 32 bits for Windows on a win7 machine)

Joe Cheng

unread,
Mar 31, 2015, 10:14:31 AM3/31/15
to Jean-François Moyen, shiny-...@googlegroups.com

You have to have shinyApp as the last line; it needs to be the thing that's returned when Shiny calls source() on your file.

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/0809192d-2ca3-4cea-a7a8-5a871620658c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jean-François Moyen

unread,
Mar 31, 2015, 10:19:40 AM3/31/15
to shiny-...@googlegroups.com, jfm...@gmail.com
Joe, thanks for the hint -- alas, it does not make any difference...

Joe Cheng

unread,
Mar 31, 2015, 10:29:04 AM3/31/15
to Jean-François Moyen, shiny-...@googlegroups.com

Oh sorry, I didn't see that you were sourcing the file. Normally we intend that you name the file app.R and call shiny::runApp(), passing it the directory in which app.R lives. This gives you the advantage of editing the app.R and hitting Reload in your browser without restarting Shiny.

If you really want to source it this way, put a print() or runApp() call around your shinyApp() call. That should launch it.

Jean-François Moyen

unread,
Mar 31, 2015, 10:44:13 AM3/31/15
to shiny-...@googlegroups.com, jfm...@gmail.com
Indeed. runApp() does the trick. Thanks !

srijat...@mail.dcu.ie

unread,
Apr 18, 2018, 10:02:38 AM4/18/18
to Shiny - Web Framework for R
Hi Jean,

Any idea how to convert an user input to a global variable?

Regards,
Srijata
Reply all
Reply to author
Forward
0 new messages