brittAnderson / Intro2Computing4Psychology

A guided introduction to computing tools useful for research in psychology - targeted to complete beginners
46 stars 159 forks source link

Printing the names of other datasets available in RStudio #69

Closed lchalupk closed 2 years ago

lchalupk commented 2 years ago

Hello,

I've found a command that lists all the available datasets in RStudio, but when I try to print the data output by this command RStudio generates a separate file and outputs the text into the file instead of the console. Additionally, when I knit the file the function I am using does not output anything into the HTML file.

I was wondering if this is okay for the assignment, or if we should have a function that produces this list of datasets also output to the HTML file.

Any input is appreciated! Thanks.

anhl07 commented 2 years ago

would you be able to post a sc of what you're seeing? If you use the library(help="datasets") then it'll open up the doc for the 'datasets' package. For me, after I had chosen a dataset (e.g., trees), and used names(trees) it did print the column names in the console

> names(trees)
[1] "Girth"  "Height" "Volume"

But if you're talking making R Markdown evaluate R codes, you'll have to use R chunks which begins like ```{r} and ends with 3 backticks ``` , it evaluates everything in that chunk, like if I did

image

the output in the console would look like

> names(trees)
[1] "Girth"  "Height" "Volume"
> summary(trees)
     Girth           Height       Volume     
 Min.   : 8.30   Min.   :63   Min.   :10.20  
 1st Qu.:11.05   1st Qu.:72   1st Qu.:19.40  
 Median :12.90   Median :76   Median :24.20  
 Mean   :13.25   Mean   :76   Mean   :30.17  
 3rd Qu.:15.25   3rd Qu.:80   3rd Qu.:37.30  
 Max.   :20.60   Max.   :87   Max.   :77.00 

and when knit to HTML it'll look like this

Capture

brittAnderson commented 2 years ago

Screenshots (sc) and minimal code examples (you can typeset those here. Look at the gray bar above where you type a comment in github to see the tool for including a code link). Give the smallest possible example that reproduces your error, and then we can try and help.

lchalupk commented 2 years ago

The code providing the error is:

data()

When run in RStudio, it returns a new text file containing all of the available packages, however after knitting the file, it simply comes up for me as echo'd code in the HTML file with no output below. the same thing happens when I use data() as a parameter for print().

Is there a method to get data() to output to HTML in order to print all the names of the built-in datasets within R?

brittAnderson commented 2 years ago

Okay this is going to risk looking like magic, but here goes:

---
title: "Showing Data Sets"
---

# How to show data sets?

```{r data, echo = TRUE, include = TRUE}
data()$results[,4]
```

I don't know if it was necessary, but I added header arguments to tell it to include the output.

R often returns quite complicated data structures. If you run str(data()) in the console of RStudio you will see that it describes the structure it is returning. That is how I guessed I wanted the results column. Then I used str again to see that the title was in the fourth column. So I said give me all the rows (why there is no number before the comma), and column 4, That is in brackets. R has data frames, matrices, structures, lists, ... You will learn how to parse these a little as we proceed in the course, but it is a deep topic. Often the data is easier to figure out than built in commands like data() that are meant to be read. But this works. Maybe there are easier ways, but it worked for me.