--- title: "R Notebook" output: html_notebook --- This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code. Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Cmd+Shift+Enter*. ```{r} plot(cars) ``` ```{r} iris # note there are many stored dataframes in R # we first set up a matrix (but it could just be # a dataframe) bb <- as.matrix(iris[1:5,1:4]); bb # but keeps cols and indices # input and output with csv files write.csv(bb,file='/Users/richardson/Documents/2022S_na/R/bb.csv') write.csv(bb,file='/Users/richardson/Documents/2022S_na/R/bb_noIndices.csv', row.names = FALSE) # can't delete the column names ``` ```{r} # can't write to xlsx files directly, but you can use a package library(openxlsx) # need to use dataframes, not matrices write.xlsx(iris, '/Users/richardson/Documents/2022S_na/R/irisDF.xlsx',sheetName="FirstOne", overwrite=TRUE) write.xlsx(iris, '/Users/richardson/Documents/2022S_na/R/irisDF2.xlsx',sheetName="FirstOne", rowNames=FALSE, colNames=FALSE, overwrite=TRUE) #deletes headers and indices snorkle <- read.csv(file='/Users/richardson/Documents/2022S_na/R/bb_noIndices.csv');snorkle snorkle2 <- read.xlsx('/Users/richardson/Documents/2022S_na/R/irisDF.xlsx');snorkle2 ``` ```{r} # here is a cool package, ggplot2 library(ggplot2) # load the ggplot2 package every time afterwards ggplot(iris) + aes(x = Petal.Length, y = Petal.Width) + # define space geom_point( aes(color = Species, shape = Species) ) + # add points geom_smooth(method =lm) + # add trend line annotate("text", x = 5, y = 0.5, label = "R=0.96") + # annotate with text xlab("Petal length (cm)") + # x-axis labels ylab("Petal width (cm)") + # y-axis labels ggtitle( "Correlation between petal length and width") # title ``` ```{r} #For GUIs - Shiny is an R package that does that library(shiny) # two parts - user interface + server function library(shiny) # Define UI for app that draws a histogram ---- ui <- fluidPage( # App title ---- titlePanel("Example of Shiny"), # Sidebar layout with input and output definitions ---- sidebarLayout( # Sidebar panel for inputs ---- sidebarPanel( #Put widgets here #Textbox input to change title of histogram textInput("titleHist", "Give title that you want", "Type here"), # Input: Slider for the number of bins ---- sliderInput(inputId = "bins", label = "Number of bins:", min = 1, max = 50, value = 30) ), # Main panel for displaying outputs ---- mainPanel( # Put other widgets here # Output: Histogram ---- plotOutput(outputId = "distPlot") ) ) ) # Define server logic required to draw a histogram ---- server <- function(input, output) { # Histogram of the Old Faithful Geyser Data ---- # with requested number of bins # This expression that generates a histogram is wrapped in a call # to renderPlot to indicate that: # # 1. It is "reactive" and therefore should be automatically # re-executed when inputs (input$bins) change # 2. Its output type is a plot #y <- renderText({input$titleHist}) output$distPlot <- renderPlot({ x <- faithful$waiting bins <- seq(min(x), max(x), length.out = input$bins + 1) hist(x, breaks = bins, col = "#4D1979", border = "white", xlab = "Waiting time to next eruption (in mins)", main = input$titleHist) }) } # Create Shiny app --- shinyApp(ui = ui, server = server) # run the shiny shinyApp(ui, server) ``` Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Cmd+Option+I*. When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Cmd+Shift+K* to preview the HTML file). The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike *Knit*, *Preview* does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.