How to let the readline() function run correctly within r markdown document, and keep messaging on screen functiona?

37 views Asked by At

I'm making a user friendly pipeline to analyze flow cytometry data, which means we have a user friendly main document, and a depenencies file in which all the horror of a code is standing. Because people without prior knowledge to R should use it, which means as few lines of code for them if possible, it sometimes makes writing codes not the most streamlined.

I have a problem I keep encountering, and I don't know how to deal with it. I wanted to make sure that when people run chunks in an R markdown document, that it checks whether or not there is a folder/file when they've previously run it. I want then to ask for input of them like "hey i see that you have already a folder with the name you want to create now, do you want to override it, or not run in?" or "hey, do you want to continue with these markers? Or for this part of analysis do you want less?", I'm currently doing it by making a function (like checkInputMarkers I will put below), which then asks for input whether or not to continui.

  checkInputMarkers = function(){
    
      # remove side/foward scatter variables
      if(sum(channels.of.interest %in% c("SSC", "FSC", "SSC-A", "FSC-A", "SSC-H", "FSC-H", "SSC-W", "FSC-W")) > 0 ) {
        
        message("One of the following markers was detected and will be removed: SSC, FSC, SSC-A, FSC-A, SSC-H, FSC-H, SSC-W, FSC-W.\n")
        channels.of.interest <- markers_of_interest[!markers_of_interest %in% c("SSC", "FSC", "SSC-A", "FSC-A", "SSC-H", "FSC-H", "SSC-W", "FSC-W")]
        
        assign("channels.of.interest", channels.of.interest, envir = .GlobalEnv)
      }
      
        # Display markers of interest
        cat("These markers were noted as interesting before:\n", channels.of.interest, "\n")
        
        # Ask user if they want to continue with these markers

        continue <- readline("Do you want to continue with these markers? (y/n): ")
        
        if (tolower(continue) == "n") {
          stop("Please manually alter the 'markers_of_interest' variable on top of the chunk and run the chunk again.")
        }
    }

I wanted to put this in a more big funciton, however, if i use readline() within a chunk within a function within an if statment it wont wait for the user to input it, so some of these checks are within their own function, as I then force r to wait fo rinput.

Okay, so my first question is is there a way to make the code stop and wait for input even when you have it in an if statement? Like the lines of code work perfectly when i just run them one by one or select them and then run it, but within a chunk in an r markdown document...

Second question: when i use the readline() within its own function it works, it waits for input, however, any function that will be run after it in the chunk when there is messenging it won't turn up in the r markdown output place, but it does show in the console output place (dont know how to call it, hope you understand).

I myself wouldn't mind if this happened, as I can see where the output goes and what it measn, but people without experience in my lab will panic too many times.

Does anyone know how to circuimvent this?

(as en example, this is the output in the r markdown: One of the following markers was detected and will be removed: SSC, FSC, SSC-A, FSC-A, SSC-H, FSC-H, SSC-W, FSC-W.

These markers were noted as interesting before: CD8_IgG CD4 CD3_IgD PD1 CD14_CD24 CD127 CXCR5 CXCR3 CD19 CD56 CD95 CCR6 HLA_DR CD27 CCR4 CD16 CCR10 CD38 CD25 CD45RA

(and this is the output on the console:

checkInputMarkers() One of the following markers was detected and will be removed: SSC, FSC, SSC-A, FSC-A, SSC-H, FSC-H, SSC-W, FSC-W.

These markers were noted as interesting before: CD8_IgG CD4 CD3_IgD PD1 CD14_CD24 CD127 CXCR5 CXCR3 CD19 CD56 CD95 CCR6 HLA_DR CD27 CCR4 CD16 CCR10 CD38 CD25 CD45RA Do you want to continue with these markers? (y/n): y

prepCE(filesCE, md_column) There is already a folder for CE with this input, do you want to overwrite? Note, you will remove the previous files! (y/n): y Folder structure created. Directories correctly linked. Files copied to CE folder.

(mainly i want the last 3 lines to also be in the output of the markdown).

Hope this isn't too much of a vague question. Let me kwow if you have tips or other functions i can maybe better use!

0

There are 0 answers