Saturday, 8 October 2016

Read R code from a file (i.e. source) in a shiny App

I want to modularise my shiny app code, by moving parts of the code in separate files. I then include the content of the file with a call to the source function: source("./www/some_code.R", local = TRUE)



It works well except for an undesired effect: the word TRUE is added just below the insert.



Could you help me understand why this happen and how I can remove this undesired text?




For a reproducible example,



create app.R:



#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#


library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

# Application title
titlePanel("Old Faithful Geyser Data"),

# Sidebar with a slider input for number of bins
sidebarLayout(

sidebarPanel(
source("./www/slider.R", local = TRUE)
),

# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)


# Define server logic required to draw a histogram
server <- function(input, output) {

output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)

# draw the histogram with the specified number of bins

hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}

# Run the application
shinyApp(ui = ui, server = server)


and in the www folder the slider.R:




sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)


enter image description here

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...