# Install RVM (R Version Manager)curl-sSLhttps://get.rvm.io|bash-sstable
# Install latest Rrvminstalllatest
rvmuselatest
# Or use renv for project-specific R versionsR-e"install.packages('renv')"
# Create new packageusethis::create_package("my-r-package")# Set up development environmentusethis::use_rstudio()usethis::use_testthat()usethis::use_mit_license()usethis::use_readme_rmd()usethis::use_pkgdown()# Add dependenciesusethis::use_package("dplyr")usethis::use_package("ggplot2",type="Suggests")# Create functionsusethis::use_r("data_processing")usethis::use_r("visualization")# Add testsusethis::use_test("data_processing")usethis::use_test("visualization")
# .Rprofile# Project-specific R configuration# Set optionsoptions(repos=c(CRAN="https://cran.rstudio.com/"),warn=1,error=utils::recover,max.print=1000,scipen=999,digits=4)# Load development packagesif(interactive()){suppressMessages({library(devtools)library(testthat)library(usethis)library(styler)library(lintr)})}# Set up renv for package managementif(file.exists("renv.lock")){renv::activate()}
# tests/testthat/test-data_processing.Rlibrary(testthat)library(myrpackage)test_that("data_processing works correctly",{# Test datatest_data<-data.frame(x=c(1,2,3,4,5),y=c(2,4,6,8,10))# Test functionresult<-process_data(test_data)# Expectationsexpect_s3_class(result,"data.frame")expect_equal(nrow(result),5)expect_true(all(c("x","y","processed")%in%names(result)))})test_that("data_processing handles edge cases",{# Test with empty dataempty_data<-data.frame()expect_error(process_data(empty_data),"Data cannot be empty")# Test with missing valuesna_data<-data.frame(x=c(1,NA,3),y=c(2,4,NA))result<-process_data(na_data)expect_false(any(is.na(result$processed)))})
# Profiling with profvislibrary(profvis)# Profile a functionprofvis({# Your code hereresult<-expensive_function(data)})# Profile memory usagelibrary(pryr)mem_used()mem_change({result<-expensive_function(data)})
# Benchmarking with benchlibrary(bench)# Compare different approachesbench::mark(base=sum(x),dplyr=dplyr::summarise(data.frame(x=x),sum(x)),data.table=data.table::data.table(x=x)[,sum(x)],iterations=1000)
# Parallel processing with futurelibrary(future)library(future.apply)# Set up parallel backendplan(multisession,workers=4)# Parallel applyresult<-future_lapply(data_list,process_function)# Parallel for loopresult<-future_map(data_list,process_function)
#' Process data with advanced filtering#'#' This function processes data with various filtering options and#' returns a cleaned dataset ready for analysis.#'#' @param data A data.frame containing the raw data#' @param filter_cols Character vector of column names to filter on#' @param filter_values List of values to filter by (same order as filter_cols)#' @param na_handling How to handle missing values: "remove", "impute", or "keep"#' @param verbose Logical, whether to print progress messages#'#' @return A processed data.frame with the same structure as input#'#' @examples#' \dontrun{#' data <- data.frame(x = 1:10, y = rnorm(10))#' result <- process_data(data, "x", 5, "remove", verbose = TRUE)#' }#'#' @export#' @importFrom dplyr filter#' @importFrom stats na.omitprocess_data<-function(data,filter_cols=NULL,filter_values=NULL,na_handling="remove",verbose=FALSE){# Function implementation}
# Create vignetteusethis::use_vignette("getting-started")# Vignette content# ---# title: "Getting Started with myrpackage"# output: rmarkdown::html_vignette# vignette: ># %\VignetteIndexEntry{Getting Started}# %\VignetteEngine{knitr::rmarkdown}# ---# ```{r setup, include = FALSE}# knitr::opts_chunk$set(# collapse = TRUE,# comment = "#>"# )# ```# ## Introduction# This vignette shows you how to get started with myrpackage.
# DockerfileFROMrocker/r-ver:4.3.0# Install system dependenciesRUNapt-getupdate&&apt-getinstall-y\libcurl4-openssl-dev\libssl-dev\libxml2-dev\libgdal-dev\libproj-dev\libgeos-dev\&&rm-rf/var/lib/apt/lists/*
# Set working directoryWORKDIR/app# Copy package filesCOPYDESCRIPTIONNAMESPACE./
COPYR/./R/
COPYtests/./tests/
# Install R dependenciesRUNR-e"install.packages(c('devtools', 'testthat', 'roxygen2'))"# Install the packageRUNRCMDINSTALL.
# Expose portEXPOSE3838# Run the applicationCMD["R","-e","shiny::runApp(port=3838, host='0.0.0.0')"]
# app.Rlibrary(shiny)library(dplyr)library(ggplot2)# UIui<-fluidPage(titlePanel("My R Application"),sidebarLayout(sidebarPanel(fileInput("file","Choose CSV File"),selectInput("column","Select Column",choices=NULL)),mainPanel(plotOutput("plot"),tableOutput("table"))))# Serverserver<-function(input,output,session){# Server logic}# Run the applicationshinyApp(ui=ui,server=server)