dark-peak-analytics / assertHE

R package to assist in the verification of health economic decision models.
https://dark-peak-analytics.github.io/assertHE/
Other
4 stars 10 forks source link

Can't get file link to work ... any help appreciated. #30

Closed RobertASmith closed 7 months ago

RobertASmith commented 8 months ago

Hi @W-Mohammed @bitowaqr I am trying to add a hyperlink to the hover:

image

Do you know how to add in the HTML for the file so that the specific R file pops up in Rstudio, the HTML below doesn't work:

https://github.com/dark-peak-analytics/assertHE/blob/1291e087ed0c2cebf6fd5ec546248526fef3da20/R/project_visualiser.R#L303

This should be replicable by cloning and running the below (changing the project path to match yours).

rm(list = ls())

devtools::load_all(".")

# project_path <- testthat::test_path(paste0("example_project"))
 project_path <- "C:/Users/r_a_s/Documents/Projects/GSK/gskVEOutils"
#project_path <- "C:/Users/r_a_s/Documents/Projects/GSK/gsk_CLD_model"

visualise_project(project_path = project_path,
                  foo_path = "R",
                  test_path = "tests/testthat")
RobertASmith commented 7 months ago

Had some feedback, which I agree with, outlining how important this is. @W-Mohammed are you able to look into it further?

W-Mohammed commented 7 months ago

Hi @RobertASmith, I have spent some time working on this issue, and here is a quick update.

image image

image

https://github.com/dark-peak-analytics/assertHE/assets/58259938/7a69e31a-50ee-422c-83de-6d25169f7e7c

W-Mohammed commented 7 months ago

Had some feedback, which I agree with, outlining how important this is. @W-Mohammed are you able to look into it further?

Can you please elaborate on the feedback you received?

RobertASmith commented 7 months ago

Yeah I think we will have to use shiny, the benefit of being able to click through is substantial for reviewers - copy-pasting + searching would be very frustrating. Thanks for this, very useful guidance.

RobertASmith commented 7 months ago

Had some feedback, which I agree with, outlining how important this is. @W-Mohammed are you able to look into it further?

Can you please elaborate on the feedback you received?

I'll catch you up in our next meeting

nialldavison commented 7 months ago

The aforementioned feedback didn't come from me, but I agree would be great for this to function as intended.

It's difficult to see what code is actually being used in your tests above (via the screenshots and videos), but to avoid the shiny app refreshing, it's possible that you'd need to add target="_blank" to the a() html. For JS, it would be window.open(url , '_blank');.

W-Mohammed commented 7 months ago

Thanks, @nialldavison, for the feedback. The issue was with the a() HTML. This is now fixed, and the app works as I expected.

https://github.com/dark-peak-analytics/assertHE/assets/58259938/d6db43eb-f188-4618-81e0-8efda73f8d4d

Here are some more information about the code I was playing with.

' Extract function line in file path

'

' @param file_location Character scalar specifying the path of a file.

'

' @return A numeric scalar

' @export

'

' @examples

' \dontrun{

' cleaned_function_line <- clean_function_line(

' file_location = "tests/testthat/example_project/R/calculate_QALYs.R:L41"

' )

' cleaned_function_line <- clean_function_line(

' file_location = c(

' "tests/testthat/example_project/R/calculate_QALYs.R:L41",

' "tests/testthat/example_project/R/calculate_QALYs.R:L49"

' )

' )

' }

clean_function_line <- function(file_location) {

function_line <- gsub(".*:L", "", file_location)

function_line <- ifelse( test = is.na(function_line), yes = -1L, no = as.numeric(function_line) )

return(function_line) }


- Build and test the shiny app.

ui <- shiny::fluidPage( shiny::tags$head( shiny::tags$script(" function openInRStudio(file_location) { console.log('Executing JavaScript function openInRStudio'); console.log('File location: ' + file_location); // For debugging

 // Send message to Shiny server
 Shiny.setInputValue('openRStudio', file_location);

} ") ), shiny::fluidRow( shiny::column(12, visNetwork::visNetworkOutput("networkPlot") ) ) )

server <- function(input, output, session) { output$networkPlot <- visNetwork::renderVisNetwork({

Create your visNetwork plot here

visualise_project(
  project_path = "tests/testthat/example_project",
  foo_path = "R",
  test_path = "tests/testthat",
  run_coverage = TRUE
)

})

Observer to handle opening files in RStudio

shiny::observeEvent(input$openRStudio, {

Open the file in RStudio

file_location <- input$openRStudio
file_path <- assertHE::clean_file_path(
  file_location = file_location
)
function_line <- assertHE::clean_function_line(
  file_location = file_location
)

if (file.exists(file_path)) {
  rstudioapi::navigateToFile(
    file = file_path,
    line = function_line
  )
} else {
  shiny::showNotification(
    paste("File not found:", file_path),
    type = "error"
  )
}

}) }

shiny::shinyApp(ui, server)

Smit-tay commented 7 months ago

Was the secret to this the use of "preventDefault()" ?

RobertASmith commented 7 months ago

Very nice @W-Mohammed, this works locally for me too.

Could you wrap the app into a function and submit a pull request? The function would need to be able to take the same arguments as visualise_model.

While it's not ideal to be launching a shiny app, its also likely better than any other solution we can come up with.

Just finalizing the abstract and draft paper for submission to R-HTA.

W-Mohammed commented 7 months ago

Was the secret to this the use of "preventDefault()" ?

Yes, @Smit-tay!

Let's take the following chunk as an example.

   "<br>Foo Location: <a href=\"#\" onclick=\"openInRStudio('",
    foo_paths, "'); event.preventDefault();\">",
    df_node_info$foo_location, "</a>",

When a user clicks on a link (<a href="#">), the default behaviour of the browser is to navigate to the URL specified in the href attribute. In this case, the href attribute is set to "#", which would normally cause the browser to navigate to the top of the current page.

By calling event.preventDefault() in the onclick event handler, you are instructing the browser to prevent this default navigation behaviour from occurring. Instead, only the specified JavaScript function (openInRStudio('foo_paths') in this case) will be executed, and the browser will not perform the usual action associated with clicking a link (i.e., navigating to the URL in the href attribute).

W-Mohammed commented 7 months ago

A shiny app was built to facilitate interactive code reviewing. This new functionality was integrated into the visualise_project function, which now gives the options of an HTML output with limited interactivity or a shiny app with more flexibility.

Check PR #51, branch WM_shiny_app, if you want to test or review the app.

Example code:

# Visualize project dependencies in shiny
visualise_project(
    project_path = "tests/testthat/example_project",
    foo_path = "R",
    test_path = "tests/testthat",
    run_coverage = TRUE,
    show_in_shiny = TRUE
)

# Visualize project dependencies in HTML
visualise_project(
    project_path = "tests/testthat/example_project",
    foo_path = "R",
    test_path = "tests/testthat",
    run_coverage = TRUE,
    show_in_shiny = FALSE
)
image image image image image

https://github.com/dark-peak-analytics/assertHE/assets/58259938/3240505a-d6a7-410f-b433-fdeac713d31e