3

I'm trying to take my Shiny apps and break them into smaller files to make collaborating via git with coworkers much easier. This question helped me figure out how to source() in files to my server.r by using source(...,local=T). Now I'm trying to do the same thing with my UI layer.

Consider this toy Shiny app:

library(shiny)

ui <- bootstrapPage(
  plotOutput("test"),
  numericInput("n","Number of points",value=100,min=1)
)

server <- function(input, output, session) {
  output$test = renderPlot({
    x = rnorm(input$n)
    y = rnorm(input$n)
    plot(y~x)
  })
}

shinyApp(ui, server)

This app does what you would expect, one overly-wide graph of 100 random data points. Now, what if I want to move just the plotOutput to a separate file (the real use case is in moving whole tabs of UI to separate files). I make a new file called tmp.R and it has:

column(12,plotOutput("test"),numericInput("n","Number of points",value=100,min=1))

The reason for wrapping it in the column statement is because the comma's can't just be hanging out. Now I update my UI to:

library(shiny)

ui <- bootstrapPage(
  source("tmp.R",local=T)
)

server <- function(input, output, session) {
  output$test = renderPlot({
    x = rnorm(input$n)
    y = rnorm(input$n)
    plot(y~x)
  })
}

shinyApp(ui, server)

Now, the word "TRUE" is just hanging out at the bottom of the page.Snippet example of problem

How do I eliminate this word from showing up? Why is it there?

Community
  • 1
  • 1
Mark
  • 4,387
  • 2
  • 28
  • 48

1 Answers1

11

Try source("tmp.R",local = TRUE)$value maybe

DeanAttali
  • 25,268
  • 10
  • 92
  • 118
  • 1
    I would give you a thousand upvotes if I could. Thanks! – Mark Jan 27 '16 at 21:38
  • Hi @DeanAttali, appreciate if you could help with this https://stackoverflow.com/questions/59129562/in-r-shiny-how-to-fix-error-in-sourcing-app-as-nothing-changes-when-clicking-on – John Smith Dec 01 '19 at 20:30