Best practice to join R scripts

People,

I want to join different R Scripts just to reduce the size of the code.

For example -> If i have 3 tasks to do:
1- Extract data from a DB.
2- Create a datatable from this data
3- Plot graphs

I want to create 3 differents R scripts, each one doing one of these tasks.

Would be the "source()" the best way to do this? Whats is the "best practice" to do this?

Yes, source() is the way to do it. You could have a script purely consisting of source() statements to the task-based scripts you mention.

If i have an "observeEvent" in another R script, my "source()" just does not work.

For example:

UI:

library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(
  
  # Application title
  titlePanel("Old Faithful Geyser Data"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
       sliderInput("bins",
                   "Number of bins:",
                   min = 1,
                   max = 50,
                   value = 30),
       
       actionButton("plot", "Calculate!")
       
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
       plotOutput("distPlot")
    )
  )
))

Server:

library(shiny)

source("hahaha.R")

# Define server logic required to draw a histogram
shinyServer(function(input, output) {
  
  observeEvent(input$plot,{ 
    
      # Create a Progress object
      progress <- Progress$new()
      progress$set(message = "Buscando dados no banco...", value = 0)
      # Close the progress when this reactive exits (even if there's an error)
      on.exit(progress$close())
      progress$set(value = 0.3)
    
  })
  
  })

hahaha.R:

observeEvent(input$plot,{ 
  
  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')
    
  })
})

When i run it, just appear:

Listening on http://127.0.0.1:5350
Warning: Error in observeEventExpr: object 'input' not found
58: observeEventExpr
1: runApp

I didn't notice you were referring to shiny. I don't know if or how that changes matters.

No problem. I'm using Shiny Web.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.