Assignment #10: Building Your Own R Package


 







The plot_summary() function is used to automatically generate visualizations for a dataset. It begins by loading the ggplot2 package, which is used for creating graphs. The function then separates the dataset into numeric and categorical variables using sapply() with is.numeric and is.factor. This allows the function to treat each type of data appropriately. For numeric variables, a loop is used to create histograms that display the distribution of values. For categorical variables, another loop creates bar charts to show the frequency of each category. Each plot is printed using print(p), which ensures that the graphs appear immediately in the RStudio Plots pane without needing additional commands.





The interactive_chart() function creates an interactive scatter plot between two selected variables. It loads both ggplot2 and plotly, where ggplot2 is used to build the initial plot and plotly adds interactivity. The function takes a dataset and two column names as inputs, then uses aes_string() so the column names can be passed as text. A scatter plot is created using geom_point(), and a title is added for clarity. The plot is then converted into an interactive format using ggplotly() and printed, allowing the user to zoom, hover, and explore the data visually.






The tidy_summary() function provides a simple numerical summary of the dataset. It first selects only the numeric variables using sapply() and is.numeric. It then creates a data frame that includes the variable names along with their mean, median, and standard deviation. The use of na.rm = TRUE ensures that missing values do not interfere with the calculations. The summary table is printed so the user can immediately view the results, and it is also returned so it can be saved or used later in analysis.






























Finally, the example function calls using the mtcars dataset demonstrate how each function works in practice. The plot_summary(mtcars) call generates multiple histograms for numeric variables, interactive_chart(mtcars, "mpg", "hp") creates an interactive scatter plot comparing miles per gallon and horsepower, and tidy_summary(mtcars) prints a summary table of the dataset. These examples confirm that the functions are working correctly and provide a simple way to test the package.

GitHub: r-programming-assignments/Assignment 10 Build your own R package.R at main · nbrown022/r-programming-assignments

Friedman/DESCRIPTION: r-programming-assignments/Friedman at main · nbrown022/r-programming-assignments

NAMESPACE: r-programming-assignments/NAMESPACE at main · nbrown022/r-programming-assignments

Comments

Popular posts from this blog

Module 5

Module 3 Assignment

Assignment #1: Getting Started with R Programming