source() in ui.R

3,025 views
Skip to first unread message

Martin Loos

unread,
Jan 12, 2014, 8:01:20 AM1/12/14
to shiny-...@googlegroups.com
hi!
As my ui.R contains a lot of code, I split it up into a few scripts and use source(file="",...) just as recommended in the shiny tutorial.
Everything works fine except that a TRUE is printed in the web interface (late version of google chrome) for each sourcing. looks stupid!
Tried different argument settings for source(file="",...) - without success. 
any ideas? thnxs in advance...

Patrick Toche

unread,
Jan 12, 2014, 10:14:09 AM1/12/14
to shiny-...@googlegroups.com
Funny you should say this, I observed the same thing just a moment ago. Great minds ...

I also couldn't make it disappear despite trying several options to source that seemed promising.

My workaround was to create a function in global.R (global.R gets read on startup), let's say:

# global.R

gTags <- function() {
  tags$head(
    tags$style(
      type='text/css'
      , "body {font-size: 80%; color: rgb(0,0,100);}"
      , "#mainPanelId {min-width: 40%; max-width: 70%;  float: right;}"
      , "#sidebarPanelId {min-width: 20%; max-width: 40%; float: left;}"
    )
  )
}

# ui.R

    mainPanel = wellPanel(
      id = "mainPanelId"
      ,
      # pass customized display settings stored in function gTags() in global.R
      gTags()
      , etc.

Does this work for you?

Patrick Toche

unread,
Jan 12, 2014, 11:14:05 AM1/12/14
to shiny-...@googlegroups.com
A demo of sorts:

library("shiny")

Martin Loos

unread,
Jan 12, 2014, 12:10:26 PM1/12/14
to shiny-...@googlegroups.com
Hi Patrick, thanks for your fast reply!
No, that is not really a promising workaround - I really want to source code from DIFFERENT files to ui.R. With the global.R-approach proposed I indeed source from another file than ui.R - but just ONE, namely global.R. Not?
Although I wonder what happens if sourcing is wrapped into the global.R functions ... but doubt that even works.
hm...

Vincent Nijs

unread,
Jan 12, 2014, 12:48:49 PM1/12/14
to shiny-...@googlegroups.com
You can source any number of files in server.R

source('file1.R', local = TRUE)

Assuming you put your code in the file in renderUI's you can include it in ui.R as:

uiOutput('code_in_file1')

If you want to source all files in a directory you can use the R.utils package and 

sourceDirectory('mycode', recursive = TRUE)

Martin Loos

unread,
Jan 12, 2014, 1:42:01 PM1/12/14
to shiny-...@googlegroups.com
Hi Vincent

Thanks for your reply, too. Do you suggest a workaround from sourcing in server.R using renderUI() plus uiOutput() instead of sourcing in ui.R directly? Otherwise - I am confused. The question was how to source in ui.R without printing TRUE into the browser.

Vincent Nijs

unread,
Jan 12, 2014, 5:50:49 PM1/12/14
to shiny-...@googlegroups.com
You are pretty limited in terms of what you can do directly in ui.R. I am not sure you can avoid the printed message. So yes, I suggest you source from server.R and use renderUI / uiOutput

Martin Loos

unread,
Jan 13, 2014, 3:27:49 AM1/13/14
to shiny-...@googlegroups.com
...you are right, its limited. So I guess your answer was not bad at all. Lazily, I found that adding a HTML tag at the end of each source file defining the text color and another breaking it just after the source() in ui.R at least renders those TRUEs invisible, i.e., making the text color equal to that of the background.
Still, that is no real solution; shiny says you can use source(...) in ui.R, without TRUEs to appear.
Message has been deleted

Huidong TIAN

unread,
Feb 6, 2014, 8:11:51 AM2/6/14
to shiny-...@googlegroups.com
You may try include the following JavaScript code in you app:


$(document).ready(function(){
  var x = $("div.input").children();
  $("div.input").empty().append(x);
})

$(document).on("change", "div.input", function(){
  var x = $("div.input").children();
  $("div.input").empty().append(x);
})

Joe Cheng

unread,
Feb 6, 2014, 7:13:56 PM2/6/14
to Huidong TIAN, shiny-...@googlegroups.com
How about replacing:

source(...)

with:

{source(...); NULL}


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

Huidong TIAN

unread,
Feb 7, 2014, 1:50:24 AM2/7/14
to shiny-...@googlegroups.com, Huidong TIAN
Sounds promising!

Could you give an example to show how to include it in a bootstrapPage ui.R?

Martin Loos

unread,
Feb 7, 2014, 3:45:56 AM2/7/14
to shiny-...@googlegroups.com, Huidong TIAN
{source(...); NULL} does not work in ui.R?

Huidong TIAN

unread,
Feb 7, 2014, 3:53:52 AM2/7/14
to shiny-...@googlegroups.com, Huidong TIAN
Not work in my app. Does it works in yours? If yes, would you like to share your code? 

Martin Loos

unread,
Feb 7, 2014, 3:56:30 AM2/7/14
to shiny-...@googlegroups.com, Huidong TIAN
No - it also doesnt work in my app...

Yihui Xie

unread,
Feb 19, 2014, 11:31:17 PM2/19/14
to Martin Loos, shiny-discuss, Huidong TIAN
You may try the current development version on Github. I think the
{source(); NULL} trick should work.

Regards,
Yihui

Jan Stanstrup

unread,
Jun 4, 2014, 5:16:50 AM6/4/14
to shiny-...@googlegroups.com
Any news on this? I cannot make it work either. If I do a tabPanel for example and put the content in a separate file the tabs show up as lists.
It is the same with the github version.

Karolin Wiedemann

unread,
Sep 1, 2014, 5:23:42 AM9/1/14
to shiny-...@googlegroups.com
Hey,

if anyone is still interested...

I had the same problem, but non of these comments helped.
However, I found a really easy solution by a happy coincidence.
Just use

 source("file.R",local=TRUE)[1]

and the TRUE is gone.





Alex Hasan

unread,
Dec 10, 2014, 8:06:49 AM12/10/14
to shiny-...@googlegroups.com
Hello,

i had this problem since today morning, and some one in http://forums.cirad.fr/logiciel-R/   told me something very important about the source command,
the result of this command is two parameters: value and visible

the solution that i tried and it works, at the first line of my ui.R file
first =source('file.R')

and when i want to include it i use
first$value

and the TRUE is gone

cheng chen

unread,
Mar 9, 2015, 2:10:42 PM3/9/15
to shiny-...@googlegroups.com
Apparently, it has not been well addressed by the shiny team (big respect and thanks for all their great efforts, though).
And I encountered the same issue today. Mar 9/ 2015.

Thanks to Karolin and Alex, a simple solution is to source in the following way.

source('ui_whatever.r')[['value']]
 

Joe Cheng

unread,
Mar 10, 2015, 1:45:21 PM3/10/15
to cheng chen, shiny-...@googlegroups.com
Yeah sorry, I didn't know the ins and outs of source() well enough back then. This should now be considered the canonical way you do this:

source("ui_whatever.r", local=TRUE)$value

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

Jan Stanstrup

unread,
Jan 30, 2017, 8:02:05 AM1/30/17
to Shiny - Web Framework for R, chen.che...@gmail.com
Hi.

I am trying to build a modular app. So my idea was to find the enabled "modules" (server.R and ui.R files that add a tab to a navbarPage) and loop through them.

This works in my main server.R:


 module_names
<- "../Modules/conf.ini" %>% read.ini %>% list.filter(enabled == TRUE) %>% names
 
 
 
for(i in seq_along(module_names)){
 paste0
("../Modules/",module_names[i],"/shiny_server.R") %>%
 source
(local=TRUE)
 
}



But when I try the same in the ui.R:

module_names <- "../Modules/conf.ini" %>% read.ini %>% list.filter(enabled == TRUE) %>% names

for(i in seq_along(module_names)){
 source
(paste0("../Modules/",module_names[i],"/shiny_ui.R"), local=TRUE)$value
 
}



I get:

Stack trace (innermost first):
 
47: markSelected
 
46: FUN
 
45: lapply
 
44: findAndMarkSelected
 
43: buildTabset
 
42: navbarPage
 
41: shinyUI
 
1: shiny::runApp
Error : attempt to set an attribute on NULL


Any solution to this? Or another similar way to make modules?
Thanks.

Joe Cheng

unread,
Jan 30, 2017, 3:14:50 PM1/30/17
to Jan Stanstrup, Shiny - Web Framework for R, cheng chen
This would work if you use lapply instead of a for-loop. You need to collection the return values from those ui.R files as a list and include them in the UI.

That said, consider using Shiny modules: https://shiny.rstudio.com/articles/modules.html

To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/dadc157e-764c-4d03-89c8-c77b8904005b%40googlegroups.com.

Jan Stanstrup

unread,
Feb 1, 2017, 4:27:54 AM2/1/17
to Shiny - Web Framework for R, stan...@gmail.com, chen.che...@gmail.com
Thanks!
I think this was unavailable last time I tried this but after a day of hair pulling I got it working. Thanks!

Jan Stanstrup

unread,
Feb 1, 2017, 8:02:14 AM2/1/17
to Shiny - Web Framework for R, stan...@gmail.com, chen.che...@gmail.com
Spoke too soon. I managed to make my code into modules but I cannot find the right way to insert them dynamically.

I global.R I have a variable module_names that has the names of the modules.
In server.R I then have:

lapply(seq_along(module_names),
       
function(i){ callModule(get(module_names[i]), paste0("name",i))
                 
}
           
)

That works. As before my problem is the ui.R. Right now my attempt is this:

lapply(seq_along(module_names),
       
function(i){ do.call(paste0(module_names[i],"UI"),list(id = paste0("name",i)))
                 
}
       
)

The app loads and there are no errors but I have "tab-7636-1 tab-pane active" displayed instead of the tab I am inserting.

Any hint would be highly appreciated. Also if there is a bit more elegant way to do this.

Joe Cheng

unread,
Feb 1, 2017, 10:13:02 AM2/1/17
to Jan Stanstrup, Shiny - Web Framework for R, chen.che...@gmail.com
Instead of do.call(paste0(...), ...) you need do.call(get(paste0(...)), ...). Or just get(paste0(...))(id = ...).

Jan Stanstrup

unread,
Feb 1, 2017, 10:42:07 AM2/1/17
to Shiny - Web Framework for R, stan...@gmail.com, chen.che...@gmail.com
Thanks. Just tried that but I get the same result. do.call can take a character string as first argument so I don't think that is the problem.

Joe Cheng

unread,
Feb 1, 2017, 10:43:21 AM2/1/17
to Jan Stanstrup, Shiny - Web Framework for R, chen.che...@gmail.com
Are you sourcing the module files into the global environment, or another environment?

Joe Cheng

unread,
Feb 1, 2017, 10:46:37 AM2/1/17
to Jan Stanstrup, Shiny - Web Framework for R, chen.che...@gmail.com
Oh wait. I think the problem is that tabsetPanel doesn't take a list of tabPanel as an argument. But instead takes each tabPanel as an individual, unnamed argument. So if you have a list of tabPanel then you need to do.call(tabsetPanel, tabs).

Jan Stanstrup

unread,
Feb 1, 2017, 11:08:18 AM2/1/17
to Joe Cheng, Shiny - Web Framework for R, chen.che...@gmail.com
Thanks again.
You lost me a bit though. Where would I put that?
The main UI has a navbarPage not a tabsetPanel (I assume that is what you assumed?). So do I need to do something special in the main ui?

I realise it is not a nice simple and reproducible example but if you wan't to see what I am doing it is here:






To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discuss+unsubscribe@googlegroups.com.

Joe Cheng

unread,
Feb 1, 2017, 11:11:18 AM2/1/17
to Jan Stanstrup, Shiny - Web Framework for R, chen.che...@gmail.com
The main UI should become

do.call(navbarPage, c(list("Navigation bar"), lapply(...)))

I think (sorry I'm on my phone so I can't try right now)

Jan Stanstrup

unread,
Feb 1, 2017, 11:15:03 AM2/1/17
to Joe Cheng, Shiny - Web Framework for R, chen.che...@gmail.com
Thanks a million! That was indeed it. Worked.

To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discuss+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages