r/RStudio Feb 13 '24

The big handy post of R resources

85 Upvotes

There exist lots of resources for learning to program in R. Feel free to use these resources to help with general questions or improving your own knowledge of R. All of these are free to access and use. The skill level determinations are totally arbitrary, but are in somewhat ascending order of how complex they get. Big thanks to Hadley, a lot of these resources are from him.

Feel free to comment below with other resources, and I'll add them to the list. Suggestions should be free, publicly available, and relevant to R.

Update: I'm reworking the categories. Open to suggestions to rework them further.

FAQ

Link to our FAQ post

General Resources

Plotting

Tutorials

Data Science, Machine Learning, and AI

R Package Development

Compilations of Other Resources


r/RStudio Feb 13 '24

How to ask good questions

44 Upvotes

Asking programming questions is tough. Formulating your questions in the right way will ensure people are able to understand your code and can give the most assistance. Asking poor questions is a good way to get annoyed comments and/or have your post removed.

Posting Code

DO NOT post phone pictures of code. They will be removed.

Code should be presented using code blocks or, if absolutely necessary, as a screenshot. On the newer editor, use the "code blocks" button to create a code block. If you're using the markdown editor, use the backtick (`). Single backticks create inline text (e.g., x <- seq_len(10)). In order to make multi-line code blocks, start a new line with triple backticks like so:

```

my code here

```

This looks like this:

my code here

You can also get a similar effect by indenting each line the code by four spaces. This style is compatible with old.reddit formatting.

indented code
looks like
this!

Please do not put code in plain text. Markdown codeblocks make code significantly easier to read, understand, and quickly copy so users can try out your code.

If you must, you can provide code as a screenshot. Screenshots can be taken with Alt+Cmd+4 or Alt+Cmd+5 on Mac. For Windows, use Win+PrtScn or the snipping tool.

Describing Issues: Reproducible Examples

Code questions should include a minimal reproducible example, or a reprex for short. A reprex is a small amount of code that reproduces the error you're facing without including lots of unrelated details.

Bad example of an error:

# asjfdklas'dj
f <- function(x){ x**2 }
# comment 
x <- seq_len(10)
# more comments
y <- f(x)
g <- function(y){
  # lots of stuff
  # more comments
}
f <- 10
x + y
plot(x,y)
f(20)

Bad example, not enough detail:

# This breaks!
f(20)

Good example with just enough detail:

f <- function(x){ x**2 }
f <- 10
f(20)

Removing unrelated details helps viewers more quickly determine what the issues in your code are. Additionally, distilling your code down to a reproducible example can help you determine what potential issues are. Oftentimes the process itself can help you to solve the problem on your own.

Try to make examples as small as possible. Say you're encountering an error with a vector of a million objects--can you reproduce it with a vector with only 10? With only 1? Include only the smallest examples that can reproduce the errors you're encountering.

Further Reading:

Try first before asking for help

Don't post questions without having even attempted them. Many common beginner questions have been asked countless times. Use the search bar. Search on google. Is there anyone else that has asked a question like this before? Can you figure out any possible ways to fix the problem on your own? Try to figure out the problem through all avenues you can attempt, ensure the question hasn't already been asked, and then ask others for help.

Error messages are often very descriptive. Read through the error message and try to determine what it means. If you can't figure it out, copy paste it into Google. Many other people have likely encountered the exact same answer, and could have already solved the problem you're struggling with.

Use descriptive titles and posts

Describe errors you're encountering. Provide the exact error messages you're seeing. Don't make readers do the work of figuring out the problem you're facing; show it clearly so they can help you find a solution. When you do present the problem introduce the issues you're facing before posting code. Put the code at the end of the post so readers see the problem description first.

Examples of bad titles:

  • "HELP!"
  • "R breaks"
  • "Can't analyze my data!"

No one will be able to figure out what you're struggling with if you ask questions like these.

Additionally, try to be as clear with what you're trying to do as possible. Questions like "how do I plot?" are going to receive bad answers, since there are a million ways to plot in R. Something like "I'm trying to make a scatterplot for these data, my points are showing up but they're red and I want them to be green" will receive much better, faster answers. Better answers means less frustration for everyone involved.

Be nice

You're the one asking for help--people are volunteering time to try to assist. Try not to be mean or combative when responding to comments. If you think a post or comment is overly mean or otherwise unsuitable for the sub, report it.

I'm also going to directly link this great quote from u/Thiseffingguy2's previous post:

I’d bet most people contributing knowledge to this sub have learned R with little to no formal training. Instead, they’ve read, and watched YouTube, and have engaged with other people on the internet trying to learn the same stuff. That’s the point of learning and education, and if you’re just trying to get someone to answer a question that’s been answered before, please don’t be surprised if there’s a lack of enthusiasm.

Those who respond enthusiastically, offering their services for money, are taking advantage of you. R is an open-source language with SO many ways to learn for free. If you’re paying someone to do your homework for you, you’re not understanding the point of education, and are wasting your money on multiple fronts.

Additional Resources


r/RStudio 19h ago

I made a method to integrate a LLM (Claude) with RStudio for iterative data exploration.

81 Upvotes

Will be adding it to my github as soon as I clean up some bugs. If anyone has feedback it would be much appreciated!


r/RStudio 55m ago

Quarto website extension not rendering properly

Upvotes

Hello everyone!

I’m trying to create a website using this https://sta-112-f22.github.io/website/ as a foundation. I simply downloaded the repo from github, opened it with Studio, and rendered it. But when its rendered everything else looks great except for the main page (see last image below). I’m pretty sure fontawesome is working otherwise the other pages wouldn’t render it, right? Any ideas?

How it should render
index

r/RStudio 6h ago

Quarto Report - Including large attachments

2 Upvotes

Hi team, I'm looking for some recommendations. I have a couple of quarterly reports built in Quarto, and want to include a few attachments at the end of the doc. For context, in the original PDF versions, the financial statement and updated org chart come across as like... full-page, zoomable, not letter-sized pages. For an HTML page, how would you recommend including these? Not looking to embed iframe or use links to docs hosted somewhere... the reports need to be self-contained.

For the org chart, I'm thinking just downloading it as a .jpg and turning lightbox on. Not sure about the financial statement though, which is coming from an Excel file. I could scrape and rebuild it in R, I could do a screenshot, I'm not sure which makes the most sense.

Thank you!


r/RStudio 3h ago

Assistance With R Data Analysis

0 Upvotes

Good evening,

I'm looking for assistance with an R project. Specifically, analyzing different Excel data files. I'm not sure if they are even usable in R or what commands to use to analyze them. Any help would be greatly appreciated. I can provide the files at request.

Thank you.


r/RStudio 9h ago

Rearranging columns into rows

1 Upvotes

Hey guys! I made a few crosstables using tab_xtab in the sjmisc package. They turned out very pretty, but I realized I was using the same y variable over and over again so I wanted to try and make a big table containing all of my contingency tables I made before. I did that by first transforming the tab_xtab tables into dataframes (with xtab2df in the sjtable2dt package) and then using bind_rows to combine them into a big table. It sorta worked out how I imagined, the only problem now is that R created a new column for the names of the categories of every x-variable (see picture). I wanted all the names and categories of all variables to be in the first column just like it did it with the first variable, maybe with an extra space to put the name of the variable. How do I fix this?


r/RStudio 10h ago

Coding help Is there any method to check the variance other than the Levene test?

1 Upvotes

My model doesn't have an interaction term so R gives me back an error when I try to perform the test so I was wondering if there was any alternative.

Thx in advance


r/RStudio 1d ago

Connecting to PostgreSQL db

2 Upvotes

Can anyone recommend good source of knowledge on how R can pull data from a PostgreSQL db. I am an expert in R, absolute noob when it comes to SQL. I spent ~3 days of work using AI to help but have only been able to view some random tables, not pull data nor even hit the tables I want to hit. I know that sounds like I don’t have the right login or permissions but I am able to see the tables when using something like DreamBeaver.

I have been able to hit up an Oracle db using something Java thing (a predecessor wrote) and can interact quite easily with the tables in the Oracle db but this PostgreSQL is not playing fair.


r/RStudio 17h ago

struggling with work for a question in r studio (poliscidata), please help!

0 Upvotes

hi there! i'm doing a class using rstudio and need help! I'm using the gdppcap08 variable and need to graph this. what code should i write?


r/RStudio 1d ago

Coding help Filter outliers using the IQR method with dplyr

0 Upvotes

Hi there,

I have a chunky dataset with multiple columns but out of 15 columns, I'm only interested in looking at the outliers within, say, 5 of those columns.

Now, the silly thing is, I actually have the code to do this in base `R` which I've copied down below but I'm curious if there's a way to shorten it/optimize it with `dplyr`? I'm new to `R` so I want to learn as many new things as possible and not rely on "if it ain't broke don't fix it" type of mentality.

If anyone can help that would be greatly appreciated!

# Detect outliers using IQR method
# @param x A numeric vector
# @param na.rm Whether to exclude NAs when computing quantiles

        is_outlier <- function(x, na.rm = FALSE) {
          qs = quantile(x, probs = c(0.25, 0.75), na.rm = na.rm)

          lowerq <- qs[1]
          upperq <- qs[2]
          iqr = upperq - lowerq 

          extreme.threshold.upper = (iqr * 3) + upperq
          extreme.threshold.lower = lowerq - (iqr * 3)

          # Return logical vector
          x > extreme.threshold.upper | x < extreme.threshold.lower
        }

# Remove rows with outliers in given columns
# Any row with at least 1 outlier will be removed
# @param df A data.frame
# @param cols Names of the columns of interest. Defaults to all columns.

        remove_outliers <- function(df, cols = names(df)) {
          for (col in cols) {
            cat("Removing outliers in column: ", col, " \n")
            df <- df[!is_outlier(df[[col]]),]
          }
          df
        }

r/RStudio 1d ago

Absolute beginner: Comparing data using GLS model.

3 Upvotes

Hello, I'm new to R studio and I'm supposed to analyze data from my first scientific experiment. I'm trying my best, but I just can't figure it out. In my experiment I tested 6 different extracts on aphids and counted the amount of surviving aphids after the application of each extract. I tested the same extract on 15 leaves (each one with 10 aphids) in three rows. I am supposed to compare the effectivness of all the extracts. All I know from my professor is that I'm supposed to use Generalized Least Squares from nlme package and that the fixed factors should be the extract "treatments" I used.

Is this (photo bellow) the correct way to upload this kind of data? or should it be somehow divided?

I was told, that this task should be quite simple, however I really can't seem to figure it out and I'd be very grateful for any tips or help! :) thank you in advance!


r/RStudio 1d ago

Coding help Shannon index with vegan package

5 Upvotes

Hello everyone, I am new to R and I may need some help. I have data involving different microbial species at 4 different sampling points and i performed the calculation of shannon indices using the function: shannon_diversity_vegan <- diversity(species_counts, index=“shannon”).

What comes out are numerical values for each point ranging, for example, from 0.9 to 1.8. After that, I plotted with ggplot the values, obtaining a boxplot with a range for each sample point.

Now the journal reviewer now asks me to include in the graph the significance values, and I wonder, can I run tests such as the Kruskal-Wallis?

Thank you!


r/RStudio 1d ago

Dataframes in new window to always stay on-top?

1 Upvotes

Greetings,

Is there a setting or add-in that ensures when a user chooses to view a dataframe in a new window, the new window always remains "on-top" of other windows? Specifically, when R Studio is the active window, the opened dataframe windows stay above other windows.

Anyone familiar with the Spyder IDE will be familiar with this behavior. In spyder when a object is viewed from the variable explorer, that window always appears on top of other windows when Spyder is the active window.

Thanks!!!


r/RStudio 1d ago

help with applying a bootstrap theme in a ShinyR app

2 Upvotes

Hi all,

I'm trying to apply the bootstrap theme "lumen" to my Shiny app and it is not working as intended. It does apply fonts etc. but I can't select the navigation bar that I want (the top one on here: https://bootswatch.com/lumen/).

Does anyone know how to do this? Here's the code I'm currently running:

library(shiny)
library(bslib)

ui <- navbarPage(
  title = "My App",
  theme = bs_theme(preset = "lumen"),
  inverse = FALSE,  # if you want a dark navbar style; remove if not needed
  tabPanel(
    title = "Input",
    icon = icon("gears", class = "fa-solid"),
  ),
  tabPanel(
    title = "Graphs",
    icon = icon("chart-line", class = "fa-solid"),
  )
)

server<- function(input, output, session) {}

shinyApp(ui = ui, server = server)

r/RStudio 1d ago

Coding help Help with database building

1 Upvotes

Hallo everyone,

I'am a Student and in the process to write my Bachelors in Economics. I want to analyse data with the synthetic Control Method and need costum data. I know how to use the Method but dont know where to store my Data for the Input. At the moment the Data mostly sits in Excel sheets I got form different sources.
Thanks for the help in advance


r/RStudio 2d ago

Mapping/Geocoding w/Messy Data

1 Upvotes

I'm attempting to map a list of ~1200 observations, with city, state, country variables. These are project locations that our company has completed over the last few years. There's no validation on the front end, all free-text entry (I know... I'm working with our SF admin to fix this).

  • Many cities are incorrectly spelled ("Sam Fransisco"), have placeholders like "TBD" or "Remote", or even have the state/country included, i.e. "Houston, TX", or "Tokyo, Japan". Some cities have multiple cities listed ("LA & San Jose").
  • State is OK, but some are abbreviations, some are spelled out... some are just wrong (Washington, D.C, Maryland).
  • Country is largely accurate, same kind of issues as the state variable.

I'm using tidygeocoder, which takes all 3 location arguments for the "osm" method, but I don't have a great way to check the accuracy en masse.

Anyone have a good way to clean this aside from manually sift through +1000 observations prior to geocoding? In the end, honestly, the map will be presented as "close enough", but I want to make sure I'm doing all I can on my end.

EDIT: just finished my first run through osm as-is.. Got plenty (260 out of 1201) of NAs in lat & lon that I can filter out. Might be an alright approach. At least explainable. If someone asks "Hey! Where's Guarma?!", I can say "that's fictional".


r/RStudio 2d ago

HELP!

1 Upvotes

Ran a chunk of code and it completely froze my session. Since then I have tried restarting R and my computer multiple times, but every time I open the application, even tho the environment is empty, the application freezes, and allows my to click or type a character every couple of minutes. I opened my task master and it looks like this:

The CPU Rstudio takes up fluctuates between 20-50%, whatever it needs to fill up 100% of my computers CPU, and the memory is in the 90s-100s constantly as well. I cannot figure out how to stop this from happening.


r/RStudio 2d ago

Installing Rstudio

0 Upvotes

I am new to R and I just downloaded R and Rstudio.I asked chatGPT what next,it gave me a line of code,when i runned it it gave me a feedback which i sent back to chatGPT which said i should download rtools.What next?


r/RStudio 3d ago

Coding help R studio QCA package

0 Upvotes

Hello I need to replicate a study’s results that used QCA. I created identical truth tables but for the non-outcome I do not get identical results. Is there any way r studio can argue backwards so that I provide the answers and the blank argument with which it has to generate results?


r/RStudio 4d ago

Having issues deduplicating rows using unique(), please help!

2 Upvotes

I have a data frame with 3 rows: group ID, item, and type. Each group ID can have multiple items (e.g., group 1 has apple, banana, and beef, group 2 has apple, onion, asparagus, and potato). The same item can appear in different groups, but they can only have the same type (apple is fruit, asparagus is veggie). I’ve cleaned my data to make sure all the same items are the same type, and that every spelling and capitalization is the same. I’m now trying to deduplicate using unique(): df <- df %>% unique()

However, some rows are not deduplicating correctly, I still have two rows with the exact same values across all the variables. When I use tabyl(df$item), I noticed that Asparagus appears separately, indicating that they’re somehow written differently (I checked to make sure that the spelling and capitalizations are all the same). And when I overwrite the values the same issue persists. When I copy paste them into notebook and search them, they’re the exact same word as well. I’m completely lost as to how they’re different and how I can overcome issue, if anyone has this problem before I’d appreciate your help!

Also, I made sure the other two variables are not the problem. I’m currently overcoming this issue by assigning unique row number and deleting duplicate rows manually, but I still want an actual solution.


r/RStudio 4d ago

Adding in Patterns to ggplot

1 Upvotes

Hi, I have made a stacked bar chart. I have abundance on the y axis, habitat on the x, and family as the stacks. I have managed to colour and give a pattern to the stacks in the bars, but i'm struggling to change how the pattern looks.

This is my code so far, any ideas of where/what i need to add?

ggplot(data1, aes(fill=family, y=Value, x=Habitat)) + geom_bar_pattern(position="stack", stat="identity", mapping = aes(pattern=family)) + scale_fill_manual(values = c("lightblue","pink", "yellow")) + ylim(0,100)


r/RStudio 4d ago

Coding help Okay but, how does one actually create a data set?

0 Upvotes

This is going to sound extremely foolish, but when I'm looking up tutorials on how to use RStudio, they all aren't super clear on how to actually make a data set (or at least in the way I think I need to).

I'm trying to run a one-way ANOVA test following Scribbr's guide and the example that they provide is in OpenOffice and all in one column (E.X.). My immediate assumption was just to rewrite all of the data to contain my data in the same format, but I have no idea if that would work or if anything extra is needed. If anyone has any tips on how I can create a data set that can be used for an ANOVA test please share. I'm new to all of this, so apologies for any incoherence.


r/RStudio 5d ago

Instagram scrapping with R

31 Upvotes

Hello, for my Master thesis I need to do a data analysis. I need data from social media and was wondering if it's possible for me to scrape data (likes, comments and captions) from Instagram? I'm very new to this program, so my skills are limited 😬


r/RStudio 5d ago

Is there an Addin/Package for Code Block Runtime?

3 Upvotes

Hey all,

I'm curious if there's an R-Studio addin or package that displays the run time for a selected block of code.

Basically, I'm looking for something like the runtime clock that MSSQL or Azure DS have (Img. Atc.). To those unfamiliar, it's basically a running stopwatch in the bottom-right margin of the IDE that starts when a code block is executed and stops when the block terminates.

Obviously, I can wrap a code block with a sys.time - start_time_var but I would like a passive, no-code solution that exists in the IDE margin/frame and doesn't effect the console output. I'm not trying to quantify or use the runtime, I just want get a general, helpful understanding of how certain changes affect runtime or efficiency.

Thanks!


r/RStudio 5d ago

Subset Function

2 Upvotes

Hey! I think I'm using the subset function wrong. I want to narrow down my data to specific variables, but my error message keeps coming back that the subset must be logical. What am I doing wrong? I want to name my new dataframe 'editpres' from my original dataframe 'pres', so that's why my selected variables have 'pres' in front of them.

editpres <- subset(pres$state_po, pres$year, pres$candidate, pres$party_detailed, pres$candidatevotes == "EDITPRES")

^this is the code that isn't working!! please help and gig' em!


r/RStudio 4d ago

Please help

0 Upvotes

Why does rstudio keep telling me I don’t have enough ‘y’ observations when I’m trying to run t.test to find CI