To save a script and data together in R, you can use the save() function to create an .Rdata file [1][2][5]. This file can store several variables, including data frames, lists, and other R objects. To save a single variable, you can use the saveRDS() function to create an .Rds file [1].
Here's an example of how to do it:

  1. Save your script and data:
    • To save multiple R objects (e.g., data frames, lists) together in an .Rdata file:
      ​​​​​​​​# Example: Saving a data frame
      ​​​​​​​​my_data <- data.frame(x = 1:5, y = c("a", "b", "c", "d", "e"))
      ​​​​​​​​save(my_data, file = "my_data.Rdata")
      
    • To save a single R object (e.g., a vector) in an .Rds file:
      ​​​​​​​​# Example: Saving a vector
      ​​​​​​​​my_vector <- c(1, 2, 3, 4, 5)
      ​​​​​​​​saveRDS(my_vector, file = "my_vector.Rds")
      
  2. Package the files together:
    You can package the script file, .Rdata file, and/or .Rds file into a compressed folder or archive (e.g., .zip or .tar.gz) using external tools or R packages like zip if needed.
  3. Sending the package:
    You can then send this compressed folder or archive to others via email, file-sharing services, or any other preferred method.

There are also other methods to save and share R scripts and data together. Here are a couple of alternative methods:

  1. RMarkdown Documents:
    Description: RMarkdown documents combine R code, narrative text, and results into a single document. They are rendered to various formats (HTML, PDF, etc.) for easy sharing.
    Example:
    ​​​​---
    ​​​​title: "My RMarkdown Report"
    ​​​​output: html_document
    ​​​​---
    
    ​​​​This is a sample RMarkdown document.
    
    ​​​​```{r}
    ​​​​# R code here
    ​​​​x <- 1:5
    ​​​​y <- x * 2
    
    The mean of y is r mean(y).
  2. R Shiny Applications:
    Description: R Shiny allows you to create interactive web apps with R. Share your script, data, and an interactive interface via a web URL.
    Example: Create a simple Shiny app to visualize data.
    ​​​​library(shiny)
    ​​​​ui <- fluidPage(
    ​​​​  titlePanel("Simple Shiny App"),
    ​​​​  sidebarLayout(
    ​​​​    sidebarPanel(
    ​​​​      selectInput("variable", "Select a variable:",
    ​​​​                  choices = colnames(mtcars)),
    ​​​​    ),
    ​​​​    mainPanel(
    ​​​​      plotOutput("histogram")
    ​​​​    )
    ​​​​  )
    ​​​​)
    ​​​​server <- function(input, output) {
    ​​​​  output$histogram <- renderPlot({
    ​​​​    hist(mtcars[, input$variable], main = "Histogram")
    ​​​​  })
    ​​​​}
    ​​​​shinyApp(ui, server)
    
  3. Version Control Systems (e.g., Git):
    Description: Use Git to manage and share your code, data, and documentation in a version-controlled repository.
    Example: Create a GitHub repository to collaborate and share code.
    ​​​​git init
    ​​​​git add .
    ​​​​git commit -m "Initial commit"
    ​​​​git branch -M main
    ​​​​git remote add origin https://github.com/yourusername/your-repo.git
    ​​​​git push -u origin main
    
  4. Docker Containers:
    Description: Package your R script, data, and dependencies within a Docker container for consistent execution.
    Example: Create a Dockerfile to package your R script.
    ​​​​
    ​​​​# Use an official R runtime as a parent image
    ​​​​FROM r-base:latest
    
    ​​​​# Install any needed packages
    ​​​​RUN R -e "install.packages('ggplot2')"
    
    ​​​​# Copy your script and data into the container
    ​​​​COPY my_script.R /usr/src/app/
    ​​​​COPY my_data.csv /usr/src/app/
    
    ​​​​# Run your script
    ​​​​CMD ["Rscript", "/usr/src/app/my_script.R"]
    
    Build and run the Docker container:
    ​​​​docker build -t my-r-script .
    ​​​​docker run -v $(pwd):/usr/src/app my-r-script