DS4PS / cpp-526-fall-2019

Course material for CPP 526 Foundations of Data Science I
http://ds4ps.org/cpp-526-fall-2019
4 stars 4 forks source link

Final #27

Open jmacost5 opened 4 years ago

jmacost5 commented 4 years ago

I was running the document to see what it looks like to get an idea of the tabs and it is giving me an error. http://127.0.0.1:5011/final-project-dashboard-template.rmd

lecy commented 4 years ago

That link does not work for me (it's local on your machine, not a public URL). What are the errors?

I just tried the template, and it works for me. I suspect you need to install the packages used in the document?

jmacost5 commented 4 years ago

Screen Shot 2019-10-06 at 12 54 20 PM Screen Shot 2019-10-06 at 12 54 16 PM

jmacost5 commented 4 years ago

I did check to make sure all of the packages are loaded and they are.

lecy commented 4 years ago

You might try creating a new folder in "My Documents" and name it anything ("dashboard"). Save the file there and try running.

R shiny creates a virtual server on your computer to host the shiny app, so it requires a few tools to play nicely with your local operating system.

I suspect it's a package issue, likely something with pandocs, htmltools, rmarkdown, knitr, or flexdashboard. Trying to diagnose now.

sunaynagoel commented 4 years ago

I am getting the same error as @jmacost5 even after making sure file is saved and all the packages are up to date. I even tried running code one line at a time, it does not give any error then.

lecy commented 4 years ago

Can you both tell me your session info:

sessionInfo()

R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_3.6.1
jmacost5 commented 4 years ago
> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] rsconnect_0.8.15      knitr_1.24            DT_0.9                shiny_1.3.2          
 [5] viridis_0.5.1         viridisLite_0.3.0     leaflet_2.0.2         ggmap_3.0.0          
 [9] forcats_0.4.0         stringr_1.4.0         purrr_0.3.2           readr_1.3.1          
[13] tidyr_1.0.0           tibble_2.1.3          ggplot2_3.2.1         tidyverse_1.2.1      
[17] flexdashboard_0.5.1.1 pander_0.6.3          dplyr_0.8.3          

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.2        lubridate_1.7.4   lattice_0.20-38   png_0.1-7        
 [5] assertthat_0.2.1  zeallot_0.1.0     digest_0.6.20     mime_0.7         
 [9] R6_2.4.0          cellranger_1.1.0  plyr_1.8.4        backports_1.1.4  
[13] evaluate_0.14     httr_1.4.1        pillar_1.4.2      RgoogleMaps_1.4.4
[17] rlang_0.4.0       lazyeval_0.2.2    readxl_1.3.1      rstudioapi_0.10  
[21] rmarkdown_1.15    htmlwidgets_1.3   munsell_0.5.0     broom_0.5.2      
[25] compiler_3.6.1    httpuv_1.5.2      modelr_0.1.5      xfun_0.9         
[29] pkgconfig_2.0.2   htmltools_0.3.6   tidyselect_0.2.5  gridExtra_2.3    
[33] crayon_1.3.4      withr_2.1.2       later_0.8.0       bitops_1.0-6     
[37] grid_3.6.1        nlme_3.1-140      jsonlite_1.6      xtable_1.8-4     
[41] gtable_0.3.0      lifecycle_0.1.0   magrittr_1.5      scales_1.0.0     
[45] cli_1.1.0         stringi_1.4.3     promises_1.0.1    xml2_1.2.2       
[49] generics_0.0.2    vctrs_0.2.0       rjson_0.2.20      tools_3.6.1      
[53] glue_1.3.1        hms_0.5.1         crosstalk_1.0.0   jpeg_0.1-8       
[57] yaml_2.2.0        colorspace_1.4-1  rvest_0.3.4       haven_2.1.1 
lecy commented 4 years ago

Ok, I think I have got it. If you are using the newest R version, 3.6.1, you need to re-install R shiny.

You can try the traditional version install.packages("shiny").

That did not work for me. I had to install the developer package from R Studio (which will be the most up-to-date code):

install.packages("devtools")
devtools::install_github("rstudio/shiny")

When you begin the install R will tell you that newer versions are available for a few packages, but they require compilation. It asks for your preference. I selected 1 for ALL. It took some time to update all of the code (because it compiles the packages directly on your machine), but that seemed to do the trick.

That should be the only step you need (it was the step that made a different on my machine after upgrading to R 3.6.1). I also updated knitr and rmarkdown along the way, so if the step above does not fix the issue then you can also try these:

install.packages('knitr', repos = c('https://xran.yihui.name', 'https://cran.r-project.org'))
devtools::install_github('rstudio/rmarkdown')
lecy commented 4 years ago

@sunaynagoel @jmacost5 Can you please try that step and update us on whether it fixed the issue?

sunaynagoel commented 4 years ago

@lecy I tried both the step mentioned above. It made no difference. Here is my sessionInfo() after running the steps.

 sessionInfo () R version 3.6.1 (2019-07-05) Platform: x86_64-apple-darwin15.6.0 (64-bit) Running under: macOS Mojave 10.14.6

Matrix products: default BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages: [1] stats graphics grDevices utils datasets methods base

loaded via a namespace (and not attached): [1] Rcpp_1.0.2 rstudioapi_0.10 knitr_1.25.1 magrittr_1.5
[5] flexdashboard_0.5.1.1 tidyselect_0.2.5 munsell_0.5.0 colorspace_1.4-1
[9] lattice_0.20-38 R6_2.4.0 rlang_0.4.0 plyr_1.8.4
[13] dplyr_0.8.3 tools_3.6.1 grid_3.6.1 gtable_0.3.0
[17] lemon_0.4.3 xfun_0.10 htmltools_0.4.0.9000 yaml_2.2.0
[21] digest_0.6.21 lazyeval_0.2.2 assertthat_0.2.1 tibble_2.1.3
[25] crayon_1.3.4 gridExtra_2.3 purrr_0.3.2 ggplot2_3.2.1
[29] rsconnect_0.8.15 evaluate_0.14 glue_1.3.1 rmarkdown_1.16
[33] compiler_3.6.1 pillar_1.4.2 scales_1.0.0 jsonlite_1.6
[37] pkgconfig_2.0.3

lecy commented 4 years ago

If you open base R (not R Studio) and select packages --> update packages then select a mirror, which packages are listed? 

sunaynagoel commented 4 years ago

My Base R does not give me an option of select packages. It has the following screen. But when I go in packages list somehow Shiny does not show as loaded even though I installed it. Screen Shot 2019-10-06 at 6 13 20 PM

lecy commented 4 years ago

Try in R Studio then under Tools --> Check for package update.

If shiny will not load with library(shiny) it means the re-install failed. That is likely the culprit.

jmacost5 commented 4 years ago

So I need to put these packages in my base R and not my R studio.

lecy commented 4 years ago

@jmacost5 they are all the same packages, it's just easier to install them in base R sometimes because R Studio will automatically load some packages it needs, thus making it harder to update them.

Once they have installed in the basic R console they will be available in R Studio as well.

sunaynagoel commented 4 years ago

@lecy I tried a few things

  1. R Studio, under Tools --> Shows all my packages are up-to date.
  2. library(shiny) does not give any errors.
  3. Loaded the package in Base R as well. Restarted everything.

When I run the my .rmd file it does not give any errors, but it generates a file which opens up in new window (which is what it is supposed to do). Instead of showing widgets it shows only HTML type of codes in red.

I am not sure what am I doing wrong?

jmacost5 commented 4 years ago

I do not think it worked because I am still getting the same message.R version 3.6.1 (2019-07-05) Platform: x86_64-apple-darwin15.6.0 (64-bit) Running under: macOS Mojave 10.14.6

Matrix products: default BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] rsconnect_0.8.15 pander_0.6.3 DT_0.9 shiny_1.3.2.9001
[5] viridis_0.5.1 viridisLite_0.3.0 leaflet_2.0.2 ggmap_3.0.0
[9] forcats_0.4.0 stringr_1.4.0 dplyr_0.8.3 purrr_0.3.2
[13] readr_1.3.1 tidyr_1.0.0 tibble_2.1.3 ggplot2_3.2.1
[17] tidyverse_1.2.1 flexdashboard_0.5.1.1

loaded via a namespace (and not attached): [1] Rcpp_1.0.2 lubridate_1.7.4 lattice_0.20-38 png_0.1-7
[5] assertthat_0.2.1 zeallot_0.1.0 digest_0.6.21 mime_0.7
[9] R6_2.4.0 cellranger_1.1.0 plyr_1.8.4 backports_1.1.4
[13] evaluate_0.14 httr_1.4.1 pillar_1.4.2 RgoogleMaps_1.4.4
[17] rlang_0.4.0 lazyeval_0.2.2 readxl_1.3.1 rstudioapi_0.10
[21] rmarkdown_1.16.1 htmlwidgets_1.3 munsell_0.5.0 broom_0.5.2
[25] compiler_3.6.1 httpuv_1.5.2.9000 modelr_0.1.5 xfun_0.10
[29] pkgconfig_2.0.2 htmltools_0.4.0.9000 tidyselect_0.2.5 gridExtra_2.3
[33] crayon_1.3.4 withr_2.1.2 later_1.0.0 bitops_1.0-6
[37] grid_3.6.1 nlme_3.1-140 jsonlite_1.6 xtable_1.8-4
[41] gtable_0.3.0 lifecycle_0.1.0 magrittr_1.5 scales_1.0.0
[45] cli_1.1.0 stringi_1.4.3 promises_1.1.0 xml2_1.2.2
[49] generics_0.0.2 vctrs_0.2.0 rjson_0.2.20 tools_3.6.1
[53] glue_1.3.1 hms_0.5.1 crosstalk_1.0.0 jpeg_0.1-8
[57] fastmap_1.0.0 yaml_2.2.0 colorspace_1.4-1 rvest_0.3.4
[61] knitr_1.25.1 haven_2.1.1

sunaynagoel commented 4 years ago

@jmacost5 I am on the same boat as well. It did not work for me either. I hope we can figure it out sooner and then later so we can work on actual Lab. Good luck. @lecy do you think shiny and rconnect don't interact well each other. I do get this message in my base R Attaching package: ‘rsconnect’

The following object is masked from ‘package:shiny’:

serverInfo
lecy commented 4 years ago

@jmacost5 do you also have a Mac?

It should not be a conflict with shiny and rsconnect, though you can comment out packages to see if it works then.

Can you also re-install leaflet? The package conflict I had was leaflet and shiny.

We might have to schedule a Zoom call tomorrow to diagnose.

jmacost5 commented 4 years ago

ok, I will reinstall the package. Also I do have a Mac and if office hours are still the same I can meet at that time tomorrow. I am also getting an error when I re booted R studio that I did not get before. Error in value[3L] : Package ‘knitr’ version 1.25 cannot be unloaded: Error in unloadNamespace(package) : namespace ‘knitr’ is imported by ‘rmarkdown’ so cannot be unloaded

jrcook15 commented 4 years ago

Hi Professor Lecy, can you please activate the link to submit the final assignment? Only the Code Through Tutorial is showing on Canvas. Thank you.

sunaynagoel commented 4 years ago

@lecy - I pointed out 3 problems so far, when I run the document

  1. install.packages("rconnect") Warning in install.packages : package ‘rconnect’ is not available (for R version 3.6.1)

  2. library( pander )

Attaching package: ‘pander’

The following object is masked from ‘package:shiny’:

p
  1. library( DT )

Attaching package: ‘DT’

The following objects are masked from ‘package:shiny’:

dataTableOutput, renderDataTable
lecy commented 4 years ago

@jrcook15 the link should be working now

lecy commented 4 years ago

@sunaynagoel masking is not be a problem. That just means that two packages have a function of the same name, so one has been set as the default when they are called.

The other package is called "rsconnect" not "rconnect". See if install.packages("rsconnect") works.

castower commented 4 years ago

Hello all, I have also gotten the same errors. I first ran this code:

install.packages("devtools")
devtools::install_github("rstudio/shiny")

in RStudio, but it didn't make any changes.

I am now trying to install in base R, but I'm getting the following error:

devtools::install_github("rstudio/shiny")
Skipping install of 'shiny' from a github remote, the SHA1 (89bd7e90) has not changed since last install.
  Use `force = TRUE` to force installation

I am not sure how to proceed.

Thanks! Courtney

castower commented 4 years ago

Here's my systemInfo():

R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: OS X El Capitan 10.11.6

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

Random number generation:
 RNG:     Mersenne-Twister 
 Normal:  Inversion 
 Sample:  Rounding 

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.2        ps_1.3.0          prettyunits_1.0.2 rprojroot_1.3-2   withr_2.1.2       digest_0.6.21    
 [7] crayon_1.3.4      assertthat_0.2.1  R6_2.4.0          backports_1.1.4   magrittr_1.5      rlang_0.4.0      
[13] cli_1.1.0         curl_4.0          fs_1.3.1          remotes_2.1.0     testthat_2.2.1    callr_3.3.2      
[19] ellipsis_0.3.0    desc_1.2.0        devtools_2.2.1    tools_3.6.1       glue_1.3.1        pkgload_1.0.2    
[25] compiler_3.6.1    processx_3.4.1    pkgbuild_1.0.5    sessioninfo_1.1.1 memoise_1.1.0     usethis_1.5.1    
lecy commented 4 years ago

Thanks to @sunaynagoel for helping me find this issue. I had downloaded the data dictionary from Tempe's open data site, and they managed to embed some special characters in their Excel file. Can you find it?

INCAPACITATING INJURY - Any   
                         injury, other than a fatal   
                         injury, which prevents the   
                        injured person from walking,  
                       driving or normally continuing 
                       the activities the person was  
                        capable of performing before  
                         the injury occurred. Often   
                        defined as needing help from  
                        the scene. Includes: severe  
                           lacerations, broken or     
                         distorted limbs, skull or    
                         chest injuries, abdominal    
                       injuries, unconsciousness when 
                        taken from the crash scene.  

So that was breaking the HTML code for some people and not others. It's an easy fix, just replace the embedded data dictionary chunks with this one chunk that reads in a clean version of the data dictionary:

URL.dd <- "https://raw.githubusercontent.com/DS4PS/cpp-526-fall-2019/master/labs/final-project/TempeTrafficAccidentsDataDictionary.csv"
data.dictionary <- read.csv( URL.dd, stringsAsFactors=F )
data.dictionary %>%
  select( column, description ) %>%
  pander( )

I've updated the template.

Let me know if you are having other issues getting the template to work!

castower commented 4 years ago

@lecy I re-downloaded the template, but it still had the old url.

I tried to just replace the data in the code, but I'm getting this error:

Error: object 'dat' not found

I tried to run the following code with dat instead of data.dictionary:

URL.dd <- "https://raw.githubusercontent.com/DS4PS/cpp-526-fall-2019/master/labs/final-project/TempeTrafficAccidentsDataDictionary.csv"
dat <- read.csv( URL.dd, stringsAsFactors=F )
dat %>%
  select( column, description ) %>%
  pander( )

and I got this error:

Error: replacement has 0 rows, data has 32
castower commented 4 years ago

I re-downloaded the template again and this time it worked! I'm still curious how I would've gotten the CSV file to work though, if you have any suggestions. Thanks!

lecy commented 4 years ago

Try refreshing your browser then downloading the template again:

As for the code, I just tested it an it runs fine. Do you have dplyr and pander loaded while trying the code?

URL.dd <- "https://raw.githubusercontent.com/DS4PS/cpp-526-fall-2019/master/labs/final-project/TempeTrafficAccidentsDataDictionary.csv"
dat <- read.csv( URL.dd, stringsAsFactors=F )
dat %>%
  select( column, description ) %>%
  pander( )
lecy commented 4 years ago

@castower Are you running the document, or just the code chunk? It might be because there are other files named "dat" in the document, so when the dashboard is created one gets overwritten? Try renaming it and see if that works?

jmacost5 commented 4 years ago

Mine is working once I redownloaded it, thanks.

lecy commented 4 years ago

@jmacost5 A lesson in good data governance. Don't embed weird data types in your open dataset you share with the world.

castower commented 4 years ago

@castower Are you running the document, or just the code chunk? It might be because there are other files named "dat" in the document, so when the dashboard is created one gets overwritten? Try renaming it and see if that works?

I tried renaming it and running just the code chunk, but it seems to get stuck at:

dat <- na.omit(dat) # omit any rows with NAs
dat$fatal <- dat$Totalfatalities > 0 

and produce this error:

Error in `$<-.data.frame`(`*tmp*`, fatal, value = logical(0)) : 
  replacement has 0 rows, data has 32
lecy commented 4 years ago

Gotcha. You are not replacing the original data load with the new chunk. You are replacing the part below - but if you downloaded the new template it should already be fixed for you:

Row
-------------------------------------

DATA DICTIONARY

```{r}
data.dictionary <- 
structure(list(column = structure(c(13L, 7L, 32L, 19L, 6L, 8L, 
15L, 22L, 21L, 14L, 5L, 17L, 31L, 20L, 27L, 1L, 11L, 23L, 25L, 
29L, 3L, 9L, 28L, 2L, 12L, 24L, 26L, 30L, 4L, 10L, 16L, 18L), .Label = c("Age_Drv1", 
"Age_Drv2", "AlcoholUse_Drv1", "AlcoholUse_Drv2", "Collisionmanner", 
"CrossStreet", "DateTime", "Distance", "DrugUse_Drv1", "DrugUse_Drv2", 
"Gender_Drv1", "Gender_Drv2", "Incidentid", "Injuryseverity", 
"JunctionRelation", "Latitude", "Lightcondition", "Longitude", 
"StreetName", "SurfaceCondition", "Totalfatalities", "Totalinjuries", 
"Traveldirection_One", "Traveldirection_Two", "Unitaction_One", 
"Unitaction_Two", "Unittype_One", "Unittype_Two", "Violation1_Drv1", 
"Violation1_Drv2", "Weather", "Year"), class = "factor"), type = structure(c(1L, 
3L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L), .Label = c("numeric", 
"text", "timestamp"), class = "factor"), label = structure(c(10L, 
6L, 29L, 16L, 5L, 7L, 12L, 19L, 18L, 11L, 4L, 14L, 28L, 17L, 
24L, 1L, 1L, 20L, 22L, 26L, 2L, 8L, 25L, 1L, 1L, 21L, 23L, 27L, 
3L, 9L, 13L, 15L), .Label = c("", "Alcohol Use 1 ", "Alcohol Use 2", 
"Collision Manner", "Cross-street", "Date Time", "Distance from Intersection", 
"Drug Use 1", "Drug Use 2", "Incident ID", "Injury Severity ", 
"Junction Relation", "Latitude", "Lighting Conditions", "Longitude", 
"Street Name", "Surface Condition", "Total Fatalities", "Total Injuries", 
"Travel Direction", "Travel Direction Two", "Unit Action One", 
"Unit Action Two", "Unit Type One", "Unit Type Two", "Violation  One", 
"Violation Two", "Weather", "Year"), class = "factor"), description = structure(c(21L, 
2L, 23L, 17L, 14L, 9L, 11L, 20L, 19L, 10L, 4L, 18L, 15L, 16L, 
3L, 1L, 1L, 7L, 13L, 12L, 5L, 6L, 3L, 1L, 1L, 8L, 13L, 12L, 5L, 
6L, 22L, 22L), .Label = c("", "Date and time that the crash occurred.", 
"Driver, Passenger, Pedestrian, Pedalcyclist or Driverless.", 
"Identifies the manner in which two vehicles initially came into contact.", 
"Indicates whether alcohol was a contributing factor in the crash or not.", 
"Indicates whether drug use was a contributing factor in the crash or not.", 
"The direction the unit was traveling before the incident occurred,", 
"The direction the unit was traveling before the incident occurred.", 
"The distance, in feet, that the crash occurred from the cross-street.  This distance is expressed in feet with positive numbers being either north or east of the intersection and negative numbers being either south or west of the intersection, based on the orientation of the street that the incident occurred on. (i.e. Baseline Rd runs in an E/W direction and so a negative number would be west of the intersection and a positive number would be east of the intersection).", 
"The highest severity of injury of all persons involved in the crash.\n\nNo Injury - No complaint or treatment was required by the person.\n\nPossible Injury - Complaint of pain without visible injury. Includes â\200“ momentary unconsciousness, claim of injuries not evident, limping, complaint of pain, nausea or hysteria.\n\nNon-Incapacitating Injury - Any injury, other than a fatal injury or an incapacitating injury, which is evident to observers at the scene of the crash in which the injury occurred.\nExamples: contusions (bruises), laceration, bloody nose, lump on head, or abrasions.\n\nIncapacitating Injury - Any injury, other than a fatal injury, which prevents the injured person from walking, driving or normally continuing the activities the person was capable of performing before the injury occurred. Often defined as â\200œneeding help from the scene.â\200\235 Includes: severe lacerations, broken or distorted limbs, skull or chest injuries, abdominal injuries, unconsciousness when taken from the crash scene.\n\nFatal Injury - Any injury that results in death within 30 days after the crash occurred.\n", 
"The location of the crash in relation to a junction, either an intersection or connection between a driveway and a roadway.", 
"The main violation/behavior of the unit that contributed to the crash.", 
"The maneuver, or last action, of the unit before the crash.", 
"The nearest intersecting street or road.", "The prevailing (most significant) atmospheric conditions that existed at the time of the crash.", 
"The roadway surface condition at the time and place of a crash.", 
"The street that the crash occurred on.", "The type/level of light that existed at the time of the crash.", 
"Total number of persons with fatal injuries involved in the crash.", 
"Total number of persons with non-fatal injuries involved in the crash.", 
"Unique incident ID number assigned by Arizona Department of Transportation (ADOT).", 
"Used to specify the precise location of the crash.", "Year that the crash occurred."
), class = "factor")), class = "data.frame", row.names = c(NA, 
-32L))
dd.simple <-
  data.dictionary %>%
  select( column, description )
#kable( dd.simple )  # markdown table, from knitr package
pander( dd.simple )  # html table, from pander package
jmacost5 commented 4 years ago

I am assuming I can not ask for any help on the final right?

jmacost5 commented 4 years ago

I mean for the coding project.

lecy commented 4 years ago

@jmacost5 The final dashboard project in 526? Yes you can ask for help.

jmacost5 commented 4 years ago

I wanted to make my first tab try to be similar to Traffic Accidents by Day and Time but I wanted to instead filter by the data from Day of the week, The Injury Severity (Injuryseverity), and the commute type (time.of.day). I do not know how to do the code because it is effecting the original code and when I added Injury Severity It did not even work

Tab #1 Traffic Accidents By Day and Commute =====================================

Inputs {.sidebar}

checkboxGroupInput("days", label = h5("Day of Week"), 
    choices = list("Monday"    = "Mon", 
                   "Tuesday"   = "Tue", 
                   "Wednesday" = "Wed", 
                   "Thursday"  = "Thu",
                   "Friday"    = "Fri",
                   "Saturday"  = "Sat",
                   "Sunday"    = "Sun" ),
    selected = c("Fri","Sat","Sun"))

radioButtons( label = h5("Commute Type"),
    choices = c("Work", "Morning Commute", "School Pickup", "Evening Commute", "Night", "Midnight to Dawn"), selected = c("Work"))

radioButtons(label = h5("Injury Severity 1-5"),
    choices = c("Fatal", "Incapacitating Injury", "No Injury", "No Incapacitating Injury", "Possible Injury"), selected = c("Fatal"))

# parameters

Outputs

Traffic Accidents By Day, Commute Type, and Injury Severity


#leaflet
renderLeaflet({

  days.of.week <- input$days    # vector will all checked values
  commute.type <- dat$time.of.day # type of commute being recorded
  injury.severity <- dat$Injuryseverity # how bad injuries are

  d2 <-
    dat %>%
    filter( day %in% input$days, commute.type, injury.severity )

  d2$col.vec <- ifelse( d2$nohurt, "gray20", ifelse(d2$inj, "steelblue", "darkorange") )              

  point.size <- d2$Totalinjuries + d2$Totalfatalities

  crash.details <- paste0( "Time: ", d2$DateTime, "<br>",
                     "Total Fatalities: ", d2$Totalfatalities, "<br>",
                     "Total Injuries: ", d2$Totalinjuries, "<br>",
                     "Collision type: ", d2$Collisionmanner,
                     "Injury Severity: " dat$Injuryseverity,
                     "Commute Type", dat$time.of.day)

  tempe <- leaflet( ) %>% 
              addProviderTiles( "CartoDB.Positron" )  %>%
              setView( lng=-111.9278, lat=33.39951, zoom=13 )

  addCircles( tempe, lng=d2$Longitude, lat=d2$Latitude,
              fillColor=d2$col.vec, fillOpacity=0.5, 
              stroke=F, radius=50*(1+0.33*point.size),
              popup=crash.details )

})
table(dat$Injuryseverity)
sunaynagoel commented 4 years ago

It is probably a silly question. But I am having hard time filtering data for driver 1 who have inputs Alcohol, Drugs and Alcohol and Drugs.

I have this from the template

dat <- 
  dat %>% 
  mutate( d1.substance = case_when( AlcoholUse_Drv1 == "Alcohol" & 
                                      DrugUse_Drv1 == "No Apparent Influence" ~ "Alcohol", 
                                   AlcoholUse_Drv1 == "No Apparent Influence" & 
                                     DrugUse_Drv1 == "Drugs" ~ "Drugs", 
                                   AlcoholUse_Drv1 == "Alcohol" & 
                                     DrugUse_Drv1 == "Drugs" ~ "Alcohol and Drugs", 
                                   AlcoholUse_Drv1 == "No Apparent Influence" & 
                                     DrugUse_Drv1 == "No Apparent Influence" ~ "No Apparent Influence"))

I was further trying to filter d1.substance for Alcohol, Drugs and Alcohol and Drugs. Since it is a character field I am struggling.

lecy commented 4 years ago

@jmacost5 You need to reference variables, not just criteria, in your filter function.

d2 <-
    dat %>%
    filter( day %in% input$days, commute.type, injury.severity )

For example:

d2 <- 
  data %>% 
  filter( color == input$color, size == input$size )

Where color and size are variables in your dataset, and the input$ options are filter criteria.

You only have criteria above, not the variables or logical operators for comparisons.

lecy commented 4 years ago

@sunaynagoel The mutate() function creates a new variable named d1.substance.

You can try a filter function with it, and if it is not working try to convert it to a character vector instead of a factor.

# preview values 
table( dat$d1.substance )
sunaynagoel commented 4 years ago

Not working for me. Here is my code. I am trying to filter out data related to accidents due to Substance use ( alcohol, drug or both) for driver one. Inputs are day, time and age (driver1). Output will in a map. leaflets on top will show total crashes, injuries and fatalities. I am stuck on first leaflet so far. It gives me an error that in correct filter option for character field.

data preset

dat <- 
  dat %>% 
  mutate( d1.DUI = case_when( AlcoholUse_Drv1 == "Alcohol" & 
                                      DrugUse_Drv1 == "No Apparent Influence" ~ "Alcohol", 
                                   AlcoholUse_Drv1 == "No Apparent Influence" & 
                                     DrugUse_Drv1 == "Drugs" ~ "Drugs", 
                                   AlcoholUse_Drv1 == "Alcohol" & 
                                     DrugUse_Drv1 == "Drugs" ~ "Alcohol and Drugs" 
                                   ))

Input

checkboxGroupInput("days", label = h3("Day of Week"), 
    choices = list("Monday"    = "Mon", 
                   "Tuesday"   = "Tue", 
                   "Wednesday" = "Wed", 
                   "Thursday"  = "Thu",
                   "Friday"    = "Fri",
                   "Saturday"  = "Sat",
                   "Sunday"    = "Sun" ),
    selected = c("Fri","Sat","Sun"))

sliderInput("hour", label = h3("Time of Day"), 
            min = 0, max = 23, value = c(6, 12))

sliderInput("d1age", label = h4("Driver 1 Age"), 
            min = 15, max = 100, value = c(18,36) )

First leaflet

Number of Crashes

renderValueBox({

  days.of.week <- input$days    # vector will all checked values
  start.time <- input$hour[1]   # sliderInput lower value
  end.time  <-  input$hour[2]   # sliderInput higher value
  d2 <-
    dat$d1.DUI %>%
    filter( Age_Drv1 >= input$d1age[1] & Age_Drv1 <= input$d1age[2],
            day %in% input$days, 
            hour >= start.time & hour <= end.time
            ) 
  crashes <- count( d2 )
  valueBox(crashes, 
           icon = "fa-ambulance",
           color = ifelse( crashes > 50, "danger", "primary") )

})
lecy commented 4 years ago

I don't have the data up right now so I can't test this to make sure, but I think it is because you are passing a vector to a dplyr function. You can do that in core R subset cases, but dplyr is made to work with data frames as inputs and outputs.

  d2 <-
    dat$d1.DUI %>%      # this is a vector, not a data frame
    filter( ... )

Try something like:

crashes <-
    dat %>%
    filter( Age_Drv1 >= input$d1age[1] & Age_Drv1 <= input$d1age[2],
            day %in% input$days,  hour >= start.time & hour <= end.time ) %>% 
    count( d1.DUI )
sunaynagoel commented 4 years ago

Thank you. I went the other way. I am letting the user choose what type of substance influence they want to choose for output. I was able to make that work.

sunaynagoel commented 4 years ago

I am running into new problem. So my input bar has three option a. Type of Influence - Check box option b. Day of the week - Check box option c. Time of the day - slider d. Age - slider ( could not get age group to work yet)

My output seems to only respond to Type of influence. It is not taking any other factor into account. i have checked my codes several times. I happy to share the code here but its too big. I can share the link of html file as well here but not sure how.

jmacost5 commented 4 years ago

So I do not understand how to fix getting the "commute" (time.of.day) to work. The only thing working currently is the day, not the commute part

Traffic Accidents By Day and Commute =====================================

Inputs {.sidebar}

checkboxGroupInput("days", label = h5("Day of Week"), 
    choices = list("Monday"    = "Mon", 
                   "Tuesday"   = "Tue", 
                   "Wednesday" = "Wed", 
                   "Thursday"  = "Thu",
                   "Friday"    = "Fri",
                   "Saturday"  = "Sat",
                   "Sunday"    = "Sun" ),
    selected = c("Sun"))

radioButtons("dcommute", label = h3("Commute Type"),
    choices = c("Work", "Morning Commute", "School Pickup", "Evening Commute", "Night", "Midnight to Dawn"), selected = c("Work"))

# parameters

Outputs

Traffic Accidents By Day and Commute


#leaflet
renderLeaflet({

  days.of.week <- input$days    # vector will all checked values
  commute.type <- dat$time.of.day

  d2 <-
    dat %>%
    filter( day %in% input$days, commute.type %in% dat$time.of.day)

  d2$col.vec <- ifelse( d2$nohurt, "gray20", ifelse(d2$inj, "steelblue", "darkorange") )              

  point.size <- d2$Totalinjuries + d2$Totalfatalities

  crash.details <- paste0( "Time: ", d2$DateTime, "<br>",
                     "Total Fatalities: ", d2$Totalfatalities, "<br>",
                     "Total Injuries: ", d2$Totalinjuries, "<br>",
                     "Collision type: ", d2$Collisionmanner)

  tempe <- leaflet( ) %>% 
              addProviderTiles( "CartoDB.Positron" )  %>%
              setView( lng=-111.9278, lat=33.39951, zoom=13 )

  addCircles( tempe, lng=d2$Longitude, lat=d2$Latitude,
              fillColor=d2$col.vec, fillOpacity=0.5, 
              stroke=F, radius=50*(1+0.33*point.size),
              popup=crash.details )

})
lecy commented 4 years ago

@jmacost5 You named your radiobutton widget "dcommute" and you are trying to reference the input values as dat$time.of.day.

First, widgets store input values on input$widget_name.

Second, your widget values would be at input$dcommute not input$time.of.day.

lecy commented 4 years ago

@jmacost5 The dplyr functions operate on data frames solely, so if you want to filter by specific vectors you need to make sure they are contained in the data frame. For example:

commute.type <- dat$time.of.day

dat %>%
    filter( day %in% input$days, commute.type %in% dat$time.of.day)

The filter function will look in dat for the variable commute.type. If you have not added it to the data frame, filter won't find it. Try instead:

dat$commute.type <- dat$time.of.day
# OR
dat <- mutate( dat, commute.type = time.of.day )
dat %>%
    filter( day %in% input$days, commute.type %in% input$some_widget_name )
sunaynagoel commented 4 years ago

@lecy After following all the steps in shinyapps.io, when I try to publish my document it gives me following error under deploy tab in RStudio. Also my account in shinyapps.io shows that my project status is undeployed.

Preparing to deploy document...DONE Uploading bundle for document: 1304895...DONE Deploying bundle: 2483118 for document: 1304895 ... Waiting for task: 656298676 building: Parsing manifest building: Building image: 2669839 building: Installing system dependencies building: Fetching packages building: Building package: knitr ################################ Begin Task Log ################################ [2019-10-09T07:20:16.458903402+0000] Execute script: packages/build/sys.sh