-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
R Package #11
Comments
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
It gives me the following error. |
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() |
I get the following error. |
@lecy also my working directory is set to
|
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?
|
See here: http://r-pkgs.had.co.nz/description.html
|
@sunaynagoel For the working directory do you have a Mac or PC? |
@lecy I removed the extra lines. |
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. |
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 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 |
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 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. |
Good morning, I tried the assignment link, but there is no submission button available. |
@jrcook15 Strange, try now. |
Yes, it is working now. Thank you! |
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" )
Does this mean I should pick another location to save my package? Or should I ignore this? |
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:
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?
|
@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:
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: |
@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. |
@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. |
@castower I tried this too but I can't get the functions after installing the packages to work. Like the first function:
Triggers this response: |
@JaesaR did you try to install the github version of the devtools |
@castower I did! After installing build_github_devtools
returns the same error as before
returns: Error in library(build_github_devtools) : there is no package called ‘build_github_devtools’ |
@JaesaR try this one: devtools::install_github("r-lib/devtools") |
@castower Wow I really appreciate all your help, thanks Courtney! Unfortunately, when I run that it returns with the following error: Error: .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 I believe I got that error too. I uninstalled 'rlang' and then reinstalled it. |
@castower Thanks for your suggestion, but unfortunately after uninstalling and reinstalling 'rlang' I am still experiencing the same issues. |
@JaesaR hmm, my only other suggestion (but you've probably tried it already!) would be to re-start R, sometimes that seems to fix odd problems. |
@castower haha you're right I've restarted it multiple times and even tried restarting my computer but I just keep running into the same issues. Thanks for all your help though :) |
@JaesaR you're welcome! I ran into a few issues too so I know it can get frustrating, lol |
@JaesaR It definitely appears to be a problem with dependencies from the error:
When I google that error here is what comes up: Not the solution I expected, but worth a try! I find that when there are package conflicts it is easier to update them with a basic R console rather than R Studio. I'm not sure if R Studio loads some packages automatically, but I don't encounter as many problems You can try installing devtools directly, or uninstall and reinstall rlang. remove.packages( "rlang" )
# install.packages( "rlang" )
devtools::install_github( "r-lib/devtools" ) Sometimes it is not the package that is triggering the error, but another related package to which it is bundled. You can also try Packages >> Update packages. |
Thanks for your help @castower ! |
@JaesaR Were you installing roxygen from GitHub or from CRAN? Did you try: devtools::install_github( "r-lib/roxygen2" ) |
Thanks for the help, everyone! I ended up needing to update and restart R, because I was only restarting RStudio. |
@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
The text was updated successfully, but these errors were encountered: