Watts-College / cpp-527-fall-2021

A course shell for CPP 527 Foundations of Data Science II
https://watts-college.github.io/cpp-527-fall-2021/
2 stars 6 forks source link

R Package #21

Open WSKQ23 opened 2 years ago

WSKQ23 commented 2 years ago

Hello @Lecy Do we have instructions for the R Package like we do have for the other labs? Thanks

lecy commented 2 years ago

https://watts-college.github.io/cpp-527-fall-2021/schedule/#project-build-your-own-r-package

AhmedRashwanASU commented 2 years ago

Last Step when complie below code :

setwd( ".." ) getwd() # back to documents devtools::install( "montyhall" )

I'm getting the below error, any idea what is the root of this issue? Note that Rd. files are created at the man file for each of the 7 functions + the description file is modified as well.

image

image

@lecy Prof below is the link to GitHub are you able to install the same from your side?

devtools::install_github( "AhmedRashwanASU/montyhall" )

https://github.com/AhmedRashwanASU/montyhall

ctmccull commented 2 years ago

I'm getting the same error. Does it have to do with documenting the functions?

ctmccull commented 2 years ago

@lecy

lecy commented 2 years ago

If the prior steps were successful then it's likely documentation.

Did you update the function documentation in the provided file?

#' @title
#' @description
#' @details
#' @param 
#' @return 
#' @examples
#' @export
select_door <- function( )
{
  doors <- c(1,2,3) 
  a.pick <- sample( doors, size=1 )
  return( a.pick )  # number between 1 and 3
}

Note that R comments are very flexible and R mostly ignores spaces in your file.

Roxygen is NOT forgiving. It is very sensitive to extra spaces and improper indentation.

So make sure you retain the formatting examples provided.

#' @title
#'   Create a new Monty Hall Problem game.
#'
#' @description
#'   `create_game()` generates a new game that consists of two doors 
#'   with goats behind them, and one with a car.
#'
#' @details
#'   The game setup replicates the game on the TV show "Let's
#'   Make a Deal" where there are three doors for a contestant
#'   to choose from, one of which has a car behind it and two 
#'   have goats. The contestant selects a door, then the host
#'   opens a door to reveal a goat, and then the contestant is
#'   given an opportunity to stay with their original selection
#'   or switch to the other unopened door. There was a famous 
#'   debate about whether it was optimal to stay or switch when
#'   given the option to switch, so this simulation was created
#'   to test both strategies. 
#'
#' @param ... no arguments are used by the function.
#' 
#' @return The function returns a length 3 character vector
#'   indicating the positions of goats and the car.
#'
#' @examples
#'   create_game()
#'
#' @export
create_game <- function()
{
    a.game <- sample( x=c("goat","goat","car"), size=3, replace=F )
    return( a.game )
} 

For example, do not change indentation size or add a line between documentation and functions:

### GOOD

#' @details
#'   The game setup replicates the game on the TV show "Let's
#'   Make a Deal" where there are three doors for a contestant

### BAD

#' @details
#' The game setup replicates the game on the TV show "Let's
#' Make a Deal" where there are three doors for a contestant

### GOOD 

#' @export
create_game <- function()
{
    a.game <- sample( x=c("goat","goat","car"), size=3, replace=F )
    return( a.game )
} 

### BAD

#' @export

create_game <- function()
{
    a.game <- sample( x=c("goat","goat","car"), size=3, replace=F )
    return( a.game )
} 
lecy commented 2 years ago

@AhmedRashwanASU I see that you need to update your ellipsis package.

You might try refreshing your packages (under options there is one for updating all packages).

I don't know why you would be getting the dplyr error unless it's tied to the ellipsis error? One of the reasons I suggest installing in R instead of R Studio is that it minimizes conflicts that occur when multiple packages are opened.

It is recommended to complete this lab in a regular R console, NOT in R Studio. You can do it using RMD docs as well, but some of the steps will be different and it complicates things.

lecy commented 2 years ago

Also double-checking that you have added the DPLYR dependency to the description file.

This line tells R to load DPLYR any time you load the montyhall package so that the dplyr functions are available since we use them in the code:

Depends: dplyr

Package: montyhall
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R: 
    person(given = "First",
           family = "Last",
           role = c("aut", "cre"),
           email = "first.last@example.com",
           comment = c(ORCID = "YOUR-ORCID-ID"))
Description: What the package does (one paragraph).
Depends:
    dplyr
License: What license it uses
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.1.1
Ma112120 commented 2 years ago

@lecy

Although I wrote the full documentation for all the 7 functions whenever I press on the man folder, I don't see my documentation codes there and it only includes "hello.Rd", so could you please advise on that?

lecy commented 2 years ago

@Ma112120 That means something is failing at the documentation step:

setwd( "montyhall" )
getwd()   # something like 'C:/Users/username/Documents/montyhall'
devtools::document()

You placed your documented R scripts into the R folder before trying that step?

documents  
├─ montyhall    # should be inside here now
│  ├─ R         # your R scripts should be in here  

Did you get errors, or a print-out like this?

# Updating montyhall documentation
# Updating roxygen version in C:\Users\jdlecy\Documents\montyhall/DESCRIPTION
# Writing NAMESPACE
# Loading montyhall
# Writing create_game.Rd
Ma112120 commented 2 years ago

@lecy Yes, I placed the R scripts into R before doing that step

lecy commented 2 years ago

@Ma112120 where is your directory set before running the document step?

getwd()

And what messages do you get when you run the document() step?

Ma112120 commented 2 years ago

I got this message

[1] "C:/Users/clt/Desktop/ASU/CPP 527 Data Analytics#2/Week#3/R Package Project/Montyhall/montyhall"

Ma112120 commented 2 years ago

I also got this when I ran

setwd( "montyhall" ) Error in setwd("montyhall") : cannot change working directory getwd() [1] "C:/Users/clt/Desktop/ASU/CPP 527 Data Analytics#2/Week#3/R Package Project/Montyhall/montyhall" devtools::document() i Updating Montyhall documentation i Loading Montyhall

lecy commented 2 years ago

As long as you are in the montyhall folder you are ok.

doesn't matter whats here   .../montyhall

If you try setting the working directory again after you are there you will get that error because you are already in the montyhall folder.

If you did not get errors when running the document() step it is processing your R file fine. If it does not create the Rd files for each function, though, it means there is a problem with your Roxygen comments in the script.

Just to double-check, you are using the script template that was provided in the lab?

https://www.dropbox.com/s/u8k1co2bb7eldzj/monty-hall-problem.R?dl=1

AhmedRashwanASU commented 2 years ago

Good day Prof, @lecy

I did edit my scripts again as you suggested and I did include dplyr in the description folder as well as updated ellipsis package, please note that I have deleted all the files and started from zero again, however, I'm still getting this error, Are there any points I should take care of?

image

lecy commented 2 years ago

@AhmedRashwanASU Did you try updating the ellipsis package first? Probably rlang and glue as well - they are likely used by devtools to build the packages.

lecy commented 2 years ago

@AhmedRashwanASU also see the comments above about Roxygen comments. https://github.com/Watts-College/cpp-527-fall-2021/issues/21#issuecomment-916371743

You are definitely changing the format inside of your R scripts.

#' @details
#'   The game setup replicates the game on the TV show "Let's
#'   Make a Deal" where there are three doors for a contestant
#'   to choose from, one of which has a car behind it and two

#### VERSUS 

#' @details
#' After a goat door is revealed the only the door initially selected and the other
#' closed door remain. The contestant will have the opportunity to keep their
#' initial door or change their door of choice .
aawoods97 commented 2 years ago

@lecy I have a two-part question.

  1. Could you specify what you mean by updating the documentation? Should I add descriptive information to the title - examples sections for each of the functions?
  2. Additionally, I am receiving an error when I attempt to install the package. Is this connected to my first question or a separate problem?

Thank you for your help!

Screen Shot 2021-09-09 at 6 10 28 PM
ctmccull commented 2 years ago

For anyone else who is struggling, what I did is I went into the provided roxygen script and added titles to each function. I was sure not to add any extra spaces. Once I did this and saved it into montyhall/R, it seemed to work. The roxygen script gives an example of a title for create_game() for reference.

@lecy Is it expected that we do this? Should we also be filling out the descriptions, parameters, etc. for this assignment?

lecy commented 2 years ago

@aawoods97 that is correct. If you look at the script I provided it has placeholder fields for the Roxygen documentation:

#' @title
#' @description
#' @details
#' @param 
#' @return 
#' @examples
#' @export
select_door <- function( )
{
  doors <- c(1,2,3) 
  a.pick <- sample( doors, size=1 )
  return( a.pick )  # number between 1 and 3
}

And correct - you are getting an error because the fields are blank. @ctmccull is correct that you can get the script to compile if you simply add a title to all of the functions.

documentation expectations

Should we also be filling out the descriptions, parameters, etc. for this assignment?

Correct, you need to document each function.

Don't go crazy - I don't care that they are perfect. I just want to see that you understand (1) minimum information you would need to provide to use a function - for example argument details, and (2) that you see how documentation is created inside R packages so you have done it once and it's no longer a black box or dark magic.

You are perfectly capable of creating your own R package.

example formatting

The first function is completed for you in the sample script - make sure you follow the same formatting style in the remaining function roxygen comments.

#' @title
#'   Create a new Monty Hall Problem game.
#'
#' @description
#'   `create_game()` generates a new game that consists of two doors 
#'   with goats behind them, and one with a car.
#'
#' @details
#'   The game setup replicates the game on the TV show "Let's
#'   Make a Deal" where there are three doors for a contestant
#'   to choose from, one of which has a car behind it and two 
#'   have goats. The contestant selects a door, then the host
#'   opens a door to reveal a goat, and then the contestant is
#'   given an opportunity to stay with their original selection
#'   or switch to the other unopened door. There was a famous 
#'   debate about whether it was optimal to stay or switch when
#'   given the option to switch, so this simulation was created
#'   to test both strategies. 
#'
#' @param ... no arguments are used by the function.
#' 
#' @return The function returns a length 3 character vector
#'   indicating the positions of goats and the car.
#'
#' @examples
#'   create_game()
#'
#' @export
create_game <- function()
{
    a.game <- sample( x=c("goat","goat","car"), size=3, replace=F )
    return( a.game )
} 
AhmedRashwanASU commented 2 years ago

@lecy here is my last version of one of the functions files, all the files is having the same structure, also note that I have updated the packages but it's still saying that need to update again.

image

lecy commented 2 years ago

@AhmedRashwanASU

I have updated the packages but it's still saying that need to update again.

Did you update the packages SUCCESSFULLY? I can't say more if you don't report messages you received.

Also, are you using the template script or re-creating all on your own?

https://www.dropbox.com/s/u8k1co2bb7eldzj/monty-hall-problem.R?dl=1

AhmedRashwanASU commented 2 years ago

image

AhmedRashwanASU commented 2 years ago

i will use the file shared in dropbox and start again and share the results . one more point which is , should i create one file only for all functions OR 7 different Functions and upload the same to R file ?

lecy commented 2 years ago

@AhmedRashwanASU you keep getting errors trying to update the packages.

I suspect it is because you have them loaded while you are trying to update them. When you load devtools those packages will be loaded as well (they are dependencies). Thus locking them in your computer, which is why you can't remove the prior installations and you get the errors.

Try closing everything and and starting with a fresh environment, then installing from there.

I also find that you have fewer conflicts when you update packages using regular R and not R Studio. All of the messaging above looks like its from R Studio?

lecy commented 2 years ago

should i create one file only for all functions OR 7 different Functions and upload the same to R file ?

This is a matter of preference, not necessity.

Some people prefer to have all functions in a single script. For this assignment it's perfectly fine.

Others find it easier to have a separate file for each function in the package, typically with file names mirroring function names. If you have a lot of functions that you need to update then it's certainly easier to find a specific chunk of code that way.

R will build the package the same either way.

AhmedRashwanASU commented 2 years ago

setwd( "montyhall" ) getwd() 'C:/Users/user/Documents/montyhall' devtools::document()

image

lecy commented 2 years ago

It's the same issue, Ahmed.

You need to make sure the devtools package and its dependencies are updated.

When a package install fails it can result in some of the dependencies being deleted.

Try installing devtools and the other packages from this R console. It doesn't matter which mirror you select when prompted.

lecy commented 2 years ago

You can also refresh all of your packages at once:

image

Because of dependencies you might need to do this once, close it down, then try again. Most will update fine but you might get an error on some because it needs a more recent version of another package, so that one needs to be updated first.

I find that the more packages I try to update at the same time the more likely I am to create conflicts. After repeating the step a couple of times the conflicts are usually resolved.

aawoods97 commented 2 years ago

@aawoods97 that is correct.

Thank you for your help!

aawoods97 commented 2 years ago

@ctmccull Thank you for your response! :)

AhmedRashwanASU commented 2 years ago

Thanks, Prof, I did start it fresh again, and it worked now

here is my link

devtools::install_github( "AhmedRashwanASU/montyhallgame" )

image

lecy commented 2 years ago

Great!

aawoods97 commented 2 years ago

I am receiving an error after running the 'devtools::install("montyhall") line' that says there is no file nor directory called 'montyhall.' Am I missing a file? I have included pictures of my console and the files that I have thus far.

Screen Shot 2021-09-10 at 5 16 00 PM Screen Shot 2021-09-10 at 5 16 43 PM
lecy commented 2 years ago

You need to be one directory level above "montyhall" because the install command will look for the folder with that name in your current directory.

Check back over instructions - it has you move one level up from the montyhall folder, back into documents in my example:

setwd( ".." )
getwd()   # back to documents 
devtools::install( "montyhall" )

documents     # should be back here 
├─ montyhall  
│  ├─ R
│  ├─ man     # new *.rd files will be here
lecy commented 2 years ago

The double-periods in setwd( ".." ) moves up one level in the directory structure.