The following code produces a GUI where an user can input files paths. The code pre-processes those file paths and safes the results. The idea is that those inputs are passed to a render function for Rmarkdown and, on the first action button press, sends those values to the render function such that another Rmarkdown file is rendered that uses those values. That is how it works in theory, in practice the first button press causes a render to happen, but non of the params are actually sent along with the first button press. However, the second button press on, everything works fine. Can anyone see the flaw in my code, that is preventing the first button press to behave the way I want it to?
I have tried looking around to see if someone else has figured this out and honestly all I think i figured out is maybe I use isolate()? sorry i am very new to R shiny.
GUIforRmarkdowngeneration
title: "Generate Rmarkdown Report for data" runtime: shiny
output: html_notebook
#setwd("/home/alan/Desktop/wts/filestosaveandcarryover/tiamatfiles")
knitr::opts_chunk$set(echo=TRUE)
library(reticulate) # <- only needed to run python
#use_python("/Users/dyarmosh/anaconda3/bin/python")
library(shinyFiles)
library(glue)
library(stringr)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
shinyFilesButton("Btn_GetFile", "Choose forward fastq" ,
title = "Please select a file:", multiple = FALSE,
buttonType = "default"),
br(),
selectInput("Selected_Panel", "Select Panel", choices = c("bacillus","burkholderia","yersinia")),
br(),
# #submitButton("Begin Analysis") - fix this no select item from file system
actionButton("Start", "Begin Analysis")
),
mainPanel(
br(),
textOutput("fwdftext"),
br(),
textOutput("revftext"),
br(),br(),
textOutput("paneltext"),
br(),br(),
textOutput("completion")
))
)
rvs <- reactiveValues() #renamed to rvs
server <- function(input,output,session){
volumes = getVolumes()
observe({
shinyFileChoose(input, "Btn_GetFile", roots = volumes, session = session)
req(input$Btn_GetFile)
req(input$Selected_Panel)
req(input$Start)
fwd_selected <- parseFilePaths(volumes, input$Btn_GetFile)
rvs$fwdfastq <- as.character(fwd_selected$datapath)
if (str_detect(rvs$fwdfastq,"R1")){rvs$revfastq <- str_replace(rvs$fwdfastq,"R1","R2")}
if (str_detect(rvs$fwdfastq,"R2")){rvs$revfastq <- str_replace(rvs$fwdfastq,"R2","R1")}
class_selected <- parseFilePaths(volumes, input$ClassifierFile)
kmer_selected <- parseFilePaths(volumes, input$StoredKmerFile)
rvs$panopt <- as.character(input$Selected_Panel)
rvs$start <- input$Start
output$fwdftext <- renderText({paste("Entered Forward File:", rvs$fwdfastq)})
output$revftext <- renderText({paste("Matched Reverse File:", rvs$revfastq)})
output$paneltext <- renderText({paste("Entered Panel:", rvs$panopt)})
})
observeEvent(input$Start, {
print(paste(rvs$panopt,rvs$fwdfastq,rvs$revfastq, rvs$class,rvs$kmer))
rmarkdown::render("testinput.Rmd",
params = list(
panelname = rvs$panopt,
r1filename = rvs$fwdfastq,
r2filename = rvs$revfastq,
classifier = rvs$class,
kmer = rvs$kmer
),
output_file = paste0("AmpSeqClass_Report_",strsplit(basename(toString(rvs$fwdfastq)),"[.]")[[1]][1],"_",str_replace(c(Sys.time())," ","_"),".html"))
output$completion <- renderText({paste("Rmarkdown Document render is complete at ",toString(Sys.time()))})
})
}
observe({
req(rvs$fwdfastq)
req(rvs$revfastq)
req(rvs$panopt)
req(rvs$start)
})
shinyApp(ui = ui, server = server)
testinputfile.Rmd
title: Perform stuff output: html_document: params: r1filename: label: "Forward:" value: BurkP-130611_S12_100_L001_R1_001.fastq r2filename: label: "Reverse:" value: BurkP-130611_S12_100_L001_R2_001.fastq panelname: label: "AP:" value: bacillus input: select choices: [bacillus,burkholderia,yersinia]
setwd("/home/alan/Desktop/wts/filestosaveandcarryover/tiamatfiles")
knitr::opts_chunk$set(echo=TRUE)
library(reticulate) # <- only needed to run python
#use_python("/Users/dyarmosh/anaconda3/bin/python")
print(params)
basically if you can get the rmarkdown to generate properly on the first action button click, with non of the passed params being NULL, that is the solution