Skip to content
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

Add function to report the State of the Project and Working Directory #426

Merged
merged 9 commits into from Aug 8, 2018

Conversation

jennybc
Copy link
Member

@jennybc jennybc commented Aug 5, 2018

Objective: create a reporter or doctor function that reveals

  • working directory
  • active usethis project
  • active RStudio project

The print() method goes on to make observations about the state of things and to suggest ways to get back to the ideal situation: working directory == active usethis project == active RStudio project.

Intended for use in remote debugging / reprexing, live workshops, writing, when people get into weird situations due to awkward workflow (or a usethis bug).

Questions I have:

  • Overall 👍 or 👎?
  • Is this a good name? Recall, there are vague plans to add some other reporter/doctor functions.
  • Should I only offer advice on what to do or should there be functions to facilitate?
    • For example, setwd(rstudioapi::getActiveProject()) might deserve nicer packaging. This is equivalent to IDE workflows: Session > Set Working Directory > To Project Directory or Files pane > click on RStudio icon to focus file browser on Project > More ... > Set As Working Directory.

@jennybc
Copy link
Member Author

jennybc commented Aug 6, 2018

Situation normal (no RStudio, because reprex/callr):

library(usethis)

proj_sitrep()
#>       working_directory:     '/Users/jenny/rrr/usethis'
#>     active_usethis_proj:     NULL
#>     active_rstudio_proj:     NULL
#> 
#> ● There is currently no active usethis project.
#>   usethis attempts to activate a project upon first need.
#>   Call `proj_get()` to explicitly initiate project activation.

proj_get()
#> ✔ Setting active project to '/Users/jenny/rrr/usethis'
#> /Users/jenny/rrr/usethis

proj_sitrep()
#>       working_directory:     '/Users/jenny/rrr/usethis'
#>     active_usethis_proj:     '/Users/jenny/rrr/usethis'
#>     active_rstudio_proj:     NULL

Created on 2018-08-05 by the reprex package (v0.2.0.9000).


What this looks like in RStudio

library(usethis)

proj_sitrep()
#>       working_directory:     '/Users/jenny/rrr/usethis'
#>     active_usethis_proj:     NULL
#>     active_rstudio_proj:     '/Users/jenny/rrr/usethis'
#> 
#> ● There is currently no active usethis project.
#>   usethis attempts to activate a project upon first need.
#>   Call `proj_get()` to explicitly initiate project activation.

proj_get()
#> ✔ Setting active project to '/Users/jenny/rrr/usethis'
#> /Users/jenny/rrr/usethis

proj_sitrep()
#>       working_directory:     '/Users/jenny/rrr/usethis'
#>     active_usethis_proj:     '/Users/jenny/rrr/usethis'
#>     active_rstudio_proj:     '/Users/jenny/rrr/usethis'

Creating a much more disordered state, in RStudio:

> devtools::load_all("~/rrr/usethis")
Loading usethis
✔ Setting active project to '/Users/jenny/rrr/usethis'

> ## set working to directory to NOT this RStudio Project
> setwd("~")

> ## set usethis project to something below tempdir
> usethis:::scoped_temporary_project()
✔ Switching to a temporary project!
● Restore current project with: `proj_set("/Users/jenny/rrr/usethis")`

> proj_sitrep()
      working_directory:     '/Users/jenny'
    active_usethis_proj:     '/private/var/folders/vr/gzrbtprx6ybg85y5pvwm1ct40000gn/T/Rtmp88agSz/aaa335b7f4aee01'
    active_rstudio_proj:     '/Users/jenny/rrr/usethis'

● Your working directory is not the same as the active usethis project.
  To set working directory to the project: `setwd(proj_get())`
  To activate project in working directory: `proj_set(getwd())`
● Your working directory is not the same as the active RStudio Project.
  To set working directory to the Project: `setwd(rstudioapi::getActiveProject())`
● Your active RStudio Project is not the same as the active usethis project.
  To set usethis project to RStudio Project: `proj_set(rstudioapi::getActiveProject())`

@jennybc jennybc requested a review from hadley August 6, 2018 05:12
NEWS.md Outdated
@@ -1,5 +1,7 @@
# usethis 1.3.0.9000

* `proj_sitrep()` is a new function that reports current working directory, the active usethis project, and the active RStudio Project. The print method offers advice in situations that seem suboptimal (#426).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call this function if something seems wacky but you don't know what.

R/sitrep.R Outdated
format.sitrep <- function(x, ...) {
max_width <- max(nchar(names(x)))
format_string <- glue::glue("%{max_width}s")
padded_names <- sprintf(format_string, names(x))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could just be format(names(x))

R/sitrep.R Outdated
active_usethis_proj = if (proj_active()) proj_get() else NULL,
active_rstudio_proj = if (rstudioapi::hasFun("getActiveProject")) {
rstudioapi::getActiveProject()
} else NULL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can drop the else NULL; that's the default value of the else block.

@jennybc jennybc merged commit c86aaa2 into master Aug 8, 2018
@jennybc jennybc deleted the proj-sitrep branch August 8, 2018 23:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants