Getting results out of R code

I'm new to R, so would like some help please.

I have some code for a PoC that produces and output shown in "Scenario 3" here at Creating event logs , but how would I save the results to a file from this code please?

Code is below :slightly_smiling_face:

    data %>%
        # recode lifecycle variable appropriately
        dplyr::mutate(registration_type = forcats::fct_recode(registration_type, 
                                                              "start" = "started",
                                                              "complete" = "completed")) %>%
        convert_timestamps(columns = "time", format = ymd_hms) %>%
        eventlog(case_id = "patient",
                    activity_id = "handling",
                    activity_instance_id = "handling_id",
                    lifecycle_id = "registration_type",
                    timestamp = "time",
                    resource_id = "employee") 

You get an out put

      ## # Log of 12 events consisting of:
      ## 1 trace 
      ## 1 case 
      ## 6 instances of 6 activities 
      ## 6 resources 
      ## Events occurred from 2017-07-03 05:21:36 until 2017-07-09 19:29:29 
      ##  
      ## # Variables were mapped as follows:
      ## Case identifier:     patient 
      ## Activity identifier:     handling 
      ## Resource identifier:     employee 
      ## Activity instance identifier:    handling_id 
      ## Timestamp:           time 
      ## Lifecycle transition:        registration_type 
      ## 
      ## # A tibble: 12 x 7
      ##    patient handling    employee handling_id registration_ty~ time               
      ##    <chr>   <fct>       <fct>    <chr>       <fct>            <dttm>             
      ##  1 187     Registrati~ r1       187         start            2017-07-03 05:21:36
      ##  2 187     Triage and~ r6       687         start            2017-07-03 08:57:23
      ##  3 187     Registrati~ r3       187         complete         2017-07-03 08:57:23
      ##  4 187     Triage and~ r4       687         complete         2017-07-03 16:49:13
      ##  5 187     Blood test  r2       1091        start            2017-07-08 13:30:11
      ##  6 187     Blood test  r2       1091        complete         2017-07-08 19:25:15
      ##  7 187     MRI SCAN    r7       1328        start            2017-07-09 00:55:50
      ##  8 187     MRI SCAN    r1       1328        complete         2017-07-09 04:54:05
      ##  9 187     Discuss Re~ r3       1921        start            2017-07-09 09:57:59
      ## 10 187     Discuss Re~ r7       1921        complete         2017-07-09 12:17:58
      ## 11 187     Check-out   r4       2416        start            2017-07-09 16:27:02
      ## 12 187     Check-out   r6       2416        complete         2017-07-09 19:29:29
      ## # ... with 1 more variable: .order <int>
      ```

I've been following "Scenario 3" here at Creating event logs

I can generate the output shown on the page above, my question is how do I actually get the data out to disk or work with it in memory please?

What the code sample does is to pass the data object to a chain of functions to produce the output shown. (BTW: don't use data, df or any other builtin; it can produce problems in some situations, use Data, DF, dat or similar.)

To keep the created object in memory, it has to be assigned a name

`event_log <- Data %>% dplyr::mutate( ...`

Then typing event_log will show the same output and is available to be further processed in the R session.

To save it between sessions, saveRDS is available

saveRDS(event_log, file = "event_log.Rds")

which will save a copy in the current directory. Then, to read it back in from that directory in another session

event_log <- readRDS("event_log")

The object can also be exported to CSV , Excel or loaded to a SQL database, if needed.

Hi

This is my solution, and it seems to work OK, thanks technocrat.

patient_data <- read_csv("H:/My Documents/test_data.txt",show_col_types = FALSE)
patient_data %>%
        
 # recode lifecycle variable appropriately
      dplyr::mutate(registration_type = forcats::fct_recode(registration_type,
                                                    "start" = "started",
                                                    "complete" = "completed")) %>%
    
          convert_timestamps(columns = "time", format = ymd_hms) %>%
    
          eventlog(case_id = "who",
                      activity_id = "what",
                      activity_instance_id = "handling_id",
                      lifecycle_id = "registration_type",
                      timestamp = "time",
                      resource_id = "employee")   ->  myoutput    
    
 # view the contents of the myoutput variable on the console  -
    
print(myoutput)

If an object like yours has a print method (vectors, lists, matrices, data frames, tibbles, data.tables, and many others, print is unnecessary. Just

myoutput

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.