Open sunaynagoel opened 4 years ago
Place your functions into an .R file (I have already done this for you).
The create_package() function builds the directory for you and creates some master files to describe the package.
Next step is to copy your R file into the R folder, then you call the document function. This will parse your roxygen comments and turn them into R package documentation.
You type roxygen comments directly into the R file. Once it’s in the new R directory the documentation function can find it.
Place your functions into an .R file (I have already done this for you).
The create_package() function builds the directory for you and creates some master files to describe the package.
Next step is to copy your R file into the R folder, then you call the document function. This will parse your roxygen comments and turn them into R package documentation.
You type roxygen comments directly into the R file. Once it’s in the new R directory the documentation function can find it.
@lecy Thank you. I was able to install package successfully. But I cannot locate any function from the package. When I try to run
help(create_game)
It gives me the following error. No documentation for ‘create_game’ in specified packages and libraries: you could try ‘??create_game’
https://www.dropbox.com/s/u8k1co2bb7eldzj/monty-hall-problem.R?dl=1
The file is called "monty-hall-problem.R". After downloading it, move it into the new "R" folder created when you built the package skeleton. That is where the program is looking when you type:
setwd( "montyhall" )
devtools::document()
You can see in the file that the first function has been documented for you, but you need to complete the rest:
#' @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 )
}
#' @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
}
If you want to run the code before you create your package, you can load all of the functions by navigating to the directory with the code, then type:
source( "monty-hall-problem.R" )
Sourcing reads a script from top to bottom, executing any R code that it finds.
I suspect your problem is that you were trying to install the package before adding the R code to the directory. So it would install an empty package without any functions. Were you able to load the package without errors? You were probably trying this and not finding the function:
setwd( ".." )
devtools::install( "montyhall" )
library( montyhall )
create_game()
After you move the code to the directory and document the code, try installing the package again.
devtools::document()
devtools::install( "montyhall" )
library( montyhall )
create_game()
@lecy
setwd( ".." )
devtools::install( "montyhall" )
library( montyhall )
create_game()
devtools::document()
I get the following error. Error in read.dcf(path_desc) : Line starting 'of the montyhall pro ...' is malformed!
@lecy also my working directory is set to
getwd() [1] "/Users/sunaynaagarwal/montyhall" I am not able to change it. Do you think that might be a problem ?
Can you share the code (function plus roxygen) for this line please:
Line starting 'of the montyhall pro ...' is malformed!
Try:
setwd( ".." )
getwd()
That should move your directory up one level to "/Users/sunaynaagarwal"
@sunaynagoel Can you try removing the extra line between section headers and text?
#' @title
#' Runs a simulation of the Monty Hall Problem game to
#' determine the probability of win or lose.
#'
#' @description
#' The function is a compliation of all the steps in playing
#' the game. It allows the user to run the simluation for any number
#' of times to determine probability of win or lose.
#'
#' @details
#' This function can run the simulation any number of times. It stores
#' the result in a data frame. After running the simulation for supplied
#' number of times it calculates the probabilities of win and lose for
#' both scenarios (stay and switch).
#'
#' @param
#' The Function takes a number for which the simulation needs to be
#' run as an argument.
#'
#' @return
#' The function returns a data frame with probabilities of win and
#' lose for both scenarios (stay and switch).
#'
#' @examples
#' play_n_games ( n=100 )
#'
#' @export
play_n_games <- function( n=100 )
{
library( dplyr )
results.list <- list() # collector
loop.count <- 1
for( i in 1:n ) # iterator
{
game.outcome <- play_game()
results.list[[ loop.count ]] <- game.outcome
loop.count <- loop.count + 1
}
results.df <- dplyr::bind_rows( results.list )
table( results.df ) %>%
prop.table( margin=1 ) %>% # row proportions
round( 2 ) %>%
print()
return( results.df )
}
See here: http://r-pkgs.had.co.nz/description.html
DESCRIPTION uses a simple file format called DCF, the Debian control format. You can see most of the structure in the simple example below. Each line consists of a field name and a value, separated by a colon. When values span multiple lines, they need to be indented:
Description: The description of a package is usually long,
spanning multiple lines. The second and subsequent lines
should be indented, usually with four spaces.
@sunaynagoel For the working directory do you have a Mac or PC?
@lecy I removed the extra lines. But I also figured that the line which shows error is in my description file . R did not like the spacing in my paragraph of description. After fixing it everything is running like a charm. Apparently there are many people like making this mistake. I am so relieved. Thank you for your help. Now I will try to upload it.
@sunaynagoel For the working directory do you have a Mac or PC?
I have a Mac. I had to restart my R a few times. Now I able to reset my working directory following your instruction. I have faced this problem in last semester as well, not sure what the permanent fix is.
Spaces are the worst in programming! Thankfully R for the most part ignores them except in YAML headers in RMD files.
See here: http://r-pkgs.had.co.nz/description.html
DESCRIPTION uses a simple file format called DCF, the Debian control format. You can see most of the structure in the simple example below. Each line consists of a field name and a value, separated by a colon. When values span multiple lines, they need to be indented:
Description: The description of a package is usually long, spanning multiple lines. The second and subsequent lines should be indented, usually with four spaces.
The indentation was the reason my code was not running. Thanks
I don't know what to say about the working directory. I am not as familiar with Macs. Problems with working directories can arise from permissions to access root directories (operating systems prevent this so you can't install viruses). It's strange to me, though, that after restarting R it worked. I have no good ideas on why that might be.
I don't know what to say about the working directory. I am not as familiar with Macs. Problems with working directories can arise from permissions to access root directories (operating systems prevent this so you can't install viruses). It's strange to me, though, that after restarting R it worked. I have no good ideas on why that might be.
I will try to read up more on working directory issue on Mac after the final project because once this happens it is very frustrating and time-consuming.
I think I got my package uploaded correctly into Github. I went to submit the assignment but couldn't find it in the assignments section of Canvas. How would you like us to submit this assignment? Or is it too soon?
Hello all! I am guessing this is where we submit the URL for our package.
I cannot get the program to work with the packages that we have to install for some reason it is making me install more programs that are not in R
I cannot get the program to work with the packages that we have to install for some reason it is making me install more programs that are not in R
Nevermind I got it to work
I am not understanding what I did wrong
@Vodkard @RickyDuran I appreciate the patience. Assignment links are open.
@jmacost5 What do you get when you type:
getwd()
You can set the directory directly using the full path of the folder. For example (yours would be different):
setwd( "/Users/icps86/Documents/R Projects/montyhall" )
You can find the path by clicking on the browser window of a file explorer bar just like you find the URL of a website.
In R Studio you can select: Session >> Change Working Directory. And after words try getwd()
and copy that path and save it.
If you are already in the folder "montyhall" then you can run the document function.
@jmacost5 If you are using R Studio I would use the full path version because the working directory can be reset between chunks of code.
@lecy I appreciate the patience. Assignment links are open.
Good morning, I tried the assignment link, but there is no submission button available.
@jrcook15 Strange, try now.
@jrcook15 Strange, try now.
Yes, it is working now. Thank you!
@jmacost5 If you are using R Studio I would use the full path version because the working directory can be reset between chunks of code.
I got everything to work today for some reason, I am got all the right messages for the programing and for some reason when I try to test library( montyhall ) create_game() I get the error "Error in create_game() : could not find function "create_game""
Hello @lecy ,
I'm getting the following error after running usethis::create_package( "montyhall" )
New project 'montyhall' is nested inside an existing project './', which is rarely a good idea.
Do you want to create anyway?
1: Absolutely
2: Not now
3: Negative
Does this mean I should pick another location to save my package? Or should I ignore this?
@lecy I tried to go through with it anyway, but it couldn't create an active directory. I've tried moving to the home section of my Mac and it's still not working. I'm not quite sure what to do here?
For reference, here is the exact output that I'm getting:
✔ Creating 'montyhall/'
✔ Setting active project to '/Users/mac/Documents/ASU/Spring 2020/CPP 527-Data Sci 2/Monty Hall Package/montyhall'
✔ Creating 'R/'
✔ Writing 'DESCRIPTION'
Package: montyhall
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R (parsed):
* First Last <first.last@example.com> [aut, cre] (<https://orcid.org/YOUR-ORCID-ID>)
Description: What the package does (one paragraph).
License: What license it uses
Encoding: UTF-8
LazyData: true
✔ Writing 'NAMESPACE'
✔ Changing working directory to 'montyhall/'
Error: Directory 'montyhall' does not exist.
✔ Setting active project to '<no active project>'
There is a montyhall folder created, but the error says it doesn't exist? There is also no rproj file in the folder.
Finally, I got it to work! I'm not sure exactly what I did. I did find an article that explained that if you type in 'ls()' it should return 'character(0)' and mine wasn't doing that. I entered rm(list = ls()) to clear it and I also uninstalled and re-installed devtools. So maybe one of those things was my problem?
I got the error again and chose to bypass it and now it's working. So maybe if someone else gets that error, try those two things and then bypass the warning.
Okay, I feel like I'm just taking babysteps.
Now that I'm trying to install the package, I'm getting the following error:
Error in Rd_info(db[[i]]) :
missing/empty \title field in '/private/var/folders/xq/pbc8kv2n6_nbnhwjhy0483zh0000gn/T/Rtmp4N6UOJ/R.INSTALL109370f3c5c8/montyhall/man/change_door.Rd'
Rd files must have a non-empty \title.
I thought these details came from the monty-hall-problem.R folder. Am I doing something wrong?
Okay, I feel like I'm just taking babysteps.
Now that I'm trying to install the package, I'm getting the following error:
Error in Rd_info(db[[i]]) : missing/empty \title field in '/private/var/folders/xq/pbc8kv2n6_nbnhwjhy0483zh0000gn/T/Rtmp4N6UOJ/R.INSTALL109370f3c5c8/montyhall/man/change_door.Rd' Rd files must have a non-empty \title.
I thought these details came from the monty-hall-problem.R folder. Am I doing something wrong?
Ah, I'm over thinking! Nevermind, I figured out that I have to fill out the fields for each function. I misunderstood the instructions! Got it now!
@jmacost5 Have you already moved the code script into the R folder and documented the functions?
@castower I have not seen this error before:
New project 'montyhall' is nested inside an existing project './', which is rarely a good idea.
Do you want to create anyway?
Do you think it was related to this?
I did find an article that explained that if you type in 'ls()' it should return 'character(0)' and mine wasn't doing that. I entered rm(list = ls()) to clear it and I also uninstalled and re-installed devtools.
@lecy I am not sure because after I cleared that step, the error still appeared, but I chose to proceed and it worked.
I did some googling, but I couldn't find much information about the error at all.
@castower I wonder if it has something to do with spaces in the directory path:
Users/mac/Documents/ASU/Spring 2020/CPP 527-Data Sci 2/Monty Hall Package/montyhall
You might have noted that I never use spaces in folder or file names. A lot of programs have problems with them. Most will just replace the space with something (have you ever wondered about all of the %20's in URLs?), but sometimes it can break the code.
Good practice is to use dash instead of space. my-folder/my-file-name.html
Underscores work as well, but they can be harder to read.
I keep receiving this error when trying to load my package through github using "devtools::install_github( "cjbecerr/montyhall" )". Anyone have any ideas?
Error: Failed to install 'montyhall' from GitHub: (converted from warning) installation of package ‘C:/Users/crist/AppData/Local/Temp/Rtmpopb0QE/file6af43eca4380/montyhall_1.0.0.9000.tar.gz’ had non-zero exit status
@cjbecerr When I run that code it works fine. I wonder if you have permission on your computer to access the folder where the download occurs (usually a temporary folder in the root C: directory somewhere, or if you have an R session currently open with the montyhall package attached?
devtools::install_github( "cjbecerr/montyhall" )
Downloading GitHub repo cjbecerr/montyhall@master
These packages have more recent versions available.
Which would you like to update?
1: All
2: CRAN packages only
3: None
4: dplyr (0.8.3 -> 0.8.4 ) [CRAN]
5: R6 (2.4.0 -> 2.4.1 ) [CRAN]
6: Rcpp (1.0.2 -> 1.0.3 ) [CRAN]
7: rlang (0.4.1 -> 0.4.4 ) [CRAN]
8: tidyselect (0.2.5 -> 1.0.0 ) [CRAN]
9: BH (1.69.0-1 -> 1.72.0-3) [CRAN]
10: cli (1.1.0 -> 2.0.2 ) [CRAN]
11: fansi (0.4.0 -> 0.4.1 ) [CRAN]
12: pillar (1.4.2 -> 1.4.3 ) [CRAN]
13: vctrs (0.2.0 -> 0.2.3 ) [CRAN]
14: digest (0.6.22 -> 0.6.25 ) [CRAN]
Enter one or more numbers, or an empty line to skip updates:
checking for file 'C:\Users\jdlecy\AppData\Local\Temp\Rtmpw9h4z1\remotes25287c935bab\cjbecerr-montyhall-c5ad06f/DESCRIPTION' ...
checking for file 'C:\Users\jdlecy\AppData\Local\Temp\Rtmpw9h4z1\remotes25287c935bab\cjbecerr-montyhall-c5ad06f/DESCRIPTION' ...
√ checking for file 'C:\Users\jdlecy\AppData\Local\Temp\Rtmpw9h4z1\remotes25287c935bab\cjbecerr-montyhall-c5ad06f/DESCRIPTION' (571ms)
- preparing 'montyhall':
checking DESCRIPTION meta-information ...
checking DESCRIPTION meta-information ...
√ checking DESCRIPTION meta-information
- checking for LF line-endings in source and make files and shell scripts
- checking for empty or unneeded directories
- building 'montyhall_1.0.0.9000.tar.gz'
Installing package into ‘C:/Users/jdlecy/Documents/R/win-library/3.5’
(as ‘lib’ is unspecified)
* installing *source* package 'montyhall' ...
** R
** byte-compile and prepare package for lazy loading
Warning: package 'dplyr' was built under R version 3.5.3
** help
*** installing help indices
converting help for package 'montyhall'
finding HTML links ... done
change_door html
create_game html
determine_winner html
open_goat_door html
play_game html
play_n_games html
select_door html
** building package indices
** testing if installed package can be loaded
*** arch - i386
Warning: package 'dplyr' was built under R version 3.5.3
*** arch - x64
Warning: package 'dplyr' was built under R version 3.5.3
* DONE (montyhall)
In R CMD INSTALL
> library( montyhall )
Loading required package: dplyr
Attaching package: ‘dplyr’
The following objects are masked from ‘package:stats’:
filter, lag
The following objects are masked from ‘package:base’:
intersect, setdiff, setequal, union
Warning message:
package ‘dplyr’ was built under R version 3.5.3
> create_game()
[1] "goat" "car" "goat"
I believe so, I saved the R file under monty hall.
@lecy glad to hear it is working after all. If it were to be the permissions problem, do you know how I would go about troubleshooting it? I terminated the R session and reran the code, but still received the error. I suppose it could be something with my permissions.
@castower I wonder if it has something to do with spaces in the directory path: Users/mac/Documents/ASU/Spring 2020/CPP 527-Data Sci 2/Monty Hall Package/montyhall You might have noted that I never use spaces in folder or file names. A lot of programs have problems with them. Most will just replace the space with something (have you ever wondered about all of the %20's in URLs?), but sometimes it can break the code. Good practice is to use dash instead of space. my-folder/my-file-name.html Underscores work as well, but they can be harder to read.
@lecy I suspected that might be a problem (in the future I'm going to exclude the spaces in my file names), but I tried running it in another file that was Users/Mac/Documents/MontyHall and I still got the same error.
I'm really not sure what caused it!
@cjbecerr I'm not sure. You could try uninstalling the package first:
https://stat.ethz.ch/R-manual/R-patched/library/utils/html/remove.packages.html
Permissions might be related to a couple of folders. One is the location of TEMP folders, which is often the default for program downloads. This is particular to installing packages from GitHub because it downloads a compressed file with the code before installation. Windows sticks mine here:
checking for file 'C:\Users\jdlecy\AppData\Local\Temp\Rtmpw9h4z1\remotes25287c935bab\cjbecerr-montyhall-c5ad06f/DESCRIPTION'
Sometimes firewalls or virus protection programs prevent anything from installing from these temporary folders to prevent viruses from being installed when downloading files. I am not sure which operation system / platform / and firewalls you are using, so you would have to google around for solutions.
The second is the default settings for installing packages:
Installing package into ‘C:/Users/jdlecy/Documents/R/win-library/3.5’
(as ‘lib’ is unspecified)
I would think this would not be the cause of the problem, another thing to check. There are a bunch of options used during installation:
https://stat.ethz.ch/R-manual/R-patched/library/utils/html/install.packages.html
The best way to check whether it is a permissions problem is to open a basic R Console (not R Studio), and select the Packages >> Install from local file options.
You can download the zipped folder from GitHub on the main repo page, and then choose that compressed directory as the file to install. If you can install it locally, but not using the install.github() function then it is likely a permissions issue.
@jmacost5 In your package skeleton directory, go into the R folder. Is your montyhall.R script there?
After moving it there, did you document functions? This is what allows the package to map the functions in that folder.
After that you can install the package through R, load it using library(), and the functions should be available.
Has anyone else had an issue installing the roxygen2 package? Every time I try to install it, the pop up urging to restart R prior to installing pops up. However, no matter how many times I click "yes" restart R, the same pop up keeps popping up. Even after manually restarting R several time I am still getting this pop up. Any suggestions?
@JaesaR yes, I got the same error. Since I knew that R had restarted, I just clicked the option to proceed anyway.
@JaesaR yes, I got the same error. Since I knew that R had restarted, I just clicked the option to proceed anyway.
@castower I tried this too but I can't get the functions after installing the packages to work. Like the first function:
library(devtools)
has_devel()
Triggers this response: Error: package or namespace load failed for ‘devtools’: .onLoad failed in loadNamespace() for 'pkgload', details: call: loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) error: namespace ‘rlang’ 0.4.0 is already loaded, but >= 0.4.1 is required
@JaesaR did you try to install the github version of the devtools
@lecy When I run this code
It opens up a new R console. The console shows all the file and folders we need. I am a little confused on where to add roxygen comments? In the new R console or to my original r document I was working with? This is my 5th try and I am not able to make it work.
Thanks