Home > Mobile >  Display verbatimTextOutput() outputs with alternative way in shiny app
Display verbatimTextOutput() outputs with alternative way in shiny app

Time:12-24

In the shiny app below I display as verbatimTextOutput() the outputs of a function. Is it possible to display this kind of outputs with a more aesthetic way like datatable or in a different context or whatever else. Even printed but in more aesthetic way.

## app.R ##
library(shiny)
library(shinydashboard)
library(bupaR)
library(daqapo)

hospital<-structure(list(patient_visit_nr = c(510, 512, 510, 512, 512, 
                                              510, 517, 518, 518, 518, 519, 519, 520, 519, 521, 521, 519, 522, 
                                              522, 523, 524, 525, 526, 523, 527, 527, 527, 528, 528, 523), 
                         activity = c("registration", "Registration", "Triage", "Triage", 
                                      "Clinical exam", "Clinical exam", "Triage", "Registration", 
                                      "Registration", "Registration", "Registration", "Triage", 
                                      "Trage", "Clinical exam", "Triage", "Registration", "Treatment", 
                                      "Registration", "Triaga", "Registration", "Triage", "Triage", 
                                      "Triage", "Triage", "Registration", "Triage", "Clinical exam", 
                                      "Registration", "Triage", "Clinical exam"), originator = c("Clerk 9", 
                                                                                                 "Clerk 12", "Nurse 27", "Nurse 27", "Doctor 7", "Doctor 7", 
                                                                                                 "Nurse 17", "Clerk 12", "Clerk 6", "Clerk 9", "Clerk 12", 
                                                                                                 "Nurse 17", "Nurse 17", "Doctor 4", "Nurse 17", "Clerk 3", 
                                                                                                 "Nurse 17", "Clerk 3", "Nurse 5", "Clerk 9", "Nurse 5", "Nurse 5", 
                                                                                                 "Nurse 5", "Nurse 27", "Clerk 6", "Nurse 5", "Doctor 4", 
                                                                                                 "Nurse 6", "Nurse 6", "Doctor 4"), start = structure(c(1511173097, 
                                                                                                                                                        1511173994, 1511174048, 1511174652, 1511177232, 1511177701, 
                                                                                                                                                        1511264116, 1511264716, 1511264716, 1511264716, 1511265581, 
                                                                                                                                                        1511270783, 1511271796, 1511272354, 1511272924, 1511273120, 
                                                                                                                                                        1511275132, 1511277137, 1511277325, 1511282528, 1511283843, 
                                                                                                                                                        1511283853, 1511283855, 1511285190, 1511287330, 1511287331, 
                                                                                                                                                        1511287333, 1511287817, 1511288105, 1511288209), tzone = "UTC", class = c("POSIXct", 
                                                                                                                                                                                                                                  "POSIXt")), complete = structure(c(1511173206, 1511174220, 
                                                                                                                                                                                                                                                                     1511174508, 1511175017, 1511177637, 1511177769, 1511264340, 
                                                                                                                                                                                                                                                                     1511263336, 1511263336, 1511263336, 1511265952, 1511271138, 
                                                                                                                                                                                                                                                                     1511271540, 1511272954, 1511273169, 1511290800, 1511275761, 
                                                                                                                                                                                                                                                                     1511277499, 1511277484, 1511283119, 1511283965, 1511283968, 
                                                                                                                                                                                                                                                                     1511283970, 1511287261, 1511287447, 1511287448, 1511287450, 
                                                                                                                                                                                                                                                                     1511288104, 1511288412, 1511288528), tzone = "UTC", class = c("POSIXct", 
                                                                                                                                                                                                                                                                                                                                   "POSIXt")), triagecode = c(3, 3, 3, 3, 3, NA, 3, 4, 4, 4, 
                                                                                                                                                                                                                                                                                                                                                              4, 4, 5, 4, 2, 2, 4, 2, 2, 3, 3, 3, 4, 3, 2, 2, 2, 3, 3, 
                                                                                                                                                                                                                                                                                                                                                              3), specialization = c("TRAU", "URG", "TRAU", "URG", "URG", 
                                                                                                                                                                                                                                                                                                                                                                                     "URG", "URG", "PED", "PED", "PED", "PED", "PED", "URG", "PED", 
                                                                                                                                                                                                                                                                                                                                                                                     "URG", "URG", "PED", "URG", "URG", "URG", "URG", "URG", "URG", 
                                                                                                                                                                                                                                                                                                                                                                                     "URG", "URG", "URG", "URG", "TRAU", "TRAU", "URG"), .order = 1:30), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                       -30L), class = c("activitylog", "log", "tbl_df", "tbl", "data.frame"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ), case_id = "patient_visit_nr", activity_id = "activity", resource_id = "originator", timestamps = c("start", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             "complete"))

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    verbatimTextOutput("print")
  )
)

server <- function(input, output) {
  
  output$print<-renderPrint({
    hospital %>%
      detect_time_anomalies()
  })
  
  
}

shinyApp(ui, server)

CodePudding user response:

The last part of daqapo::detect_time_anomalies shows that the function mostly prints and messages information and returns a dataframe anomalies only.

Access function's definition by entering the function's name without parentheses or, more "sophisticatedly" by printing its body:

detect_time_anomalies
detect_time_anomalies |> body()
## > daqapo:::detect_time_anomalies
## 
## function (activitylog, anomaly_type = c("both", "negative", "zero"), 
##     details = TRUE, filter_condition = NULL) 
## {
## ...
##     message("Selected anomaly type: ", anomaly_type, "\n")
##     message("*** OUTPUT ***")
##     message("For ", nrow(anomalies), " rows in the activity log (", 
## ...
##         message("The anomalies are spread over the activities as follows:")
## ...
##             print(anomalies %>% group_by(!!activity_id_(activitylog), 
## ...
##             print(anomalies %>% group_by(!!activity_id_(activitylog)) %>% 
## ...
## ...
##             message("Anomalies are found in the following rows:")
##             return(anomalies)
## ...
## }

You could rewrite this part of the function to make it return a list including, beyond the dataframe with anomalies, the various message/print strings. To illustrate:

## ...
## result = list()
## result$anomaly_type = # definition here
## result$percent_anomalies = # calculation here
## ...
## result$anomalies = # dataframe here
## result # return result list

Now you can pick items of the result list and format/display them with the desired output type each.

  • better don't overwrite the original function but create a new one, e. g. detect_time_anomalies2
  • remember to index the result of your new function: anomalies_table <- detect_time_anomalies(...) now becomes anomalies_table <- detect_time_anomalies2(...)$anomalies

edit

You can expose the body of an internal function by using the package name, followed by a triple colon ::: and hitting tab (or whichever key of your editor suggests autocompletions) to view possible function names.

In this case, daqapo offers only one class-specific method, namely: daqapo:::detect_value_range_violations.activitylog, which can be inspected as described (e. g. body(daqapo:::detect_value_range_violations.activitylog).

  • Related