Integrating R code with Shiny app

I am creating a shiny app gui that interacts with an R program. The program defines and initializes variables and funcions. Where is the app will this code be insterted? function(input, output)?

Should all the function definitions and functions be written in the sever function so that they are local to each individual session? For example
server <- function(input, output){
glycoPipe<- function(PARAMSfullFile=NULL, OUTpath=NULL, printFDRsummary=TRUE, useOldCalcFDR=FALSE, saveModelDetails=FALSE, deleteOMSfolder=TRUE) {

Set initial environmental options

Sys.setenv("OPENMS_DISABLE_USAGE_STATISTICS"="ON") # prevents OpenMS from repeatedly contacting server to send usage info
options(stringsAsFactors = FALSE)
override_openMS=FALSE
interact=FALSE
target<- "GLYCOPEPTIDES"

}

It is not clear to me what to write globally in the fluid page, what to write in the server function

If you want the program you are referring to tobe run every time that the app is opened, and it does not depend on any user inputs then you can source the program either at the top of your app.R file outside of both your server and ui functions or in a global.R script if you are using the split ui.R/server.R file setup. You can then use the results of this program inside of your server code. Now, the results can be modified if you call them in a reactive() expression and assign the results to a new value. Doing it in this manner will still run the program each time the app is run.

If the R program depends on user input in the shiny app then you can just run the code inside of a reactive() or eventReactive() function that will rerun the code every time one of the dependent inputs is changed or a specific action is perform (i.e. a button click), respectively.

If you do not want the program to be run every time the app is started, then I would recommend saving the results as a .rda or .rds file and then loading the file at the top of the app. The issue with this is you would have to schedule the program to run at set times or manually rerun the program and save the results every time the program data changed (assuming the results will be changing).

I hope this helps, it would be easier to give specific answers if you posted a reproducible example

Thank you. This is what I would like to do: "If the R program depends on user input in the shiny app then you can just run the code inside of a reactive() or eventReactive() function that will rerun the code every time one of the dependent inputs is changed or a specific action is perform (i.e. a button click), respectively". Will I write the reactive or eventReactive() functions inside the server function or outside? in either the leftside panel or mainPanel?

This is the code I have written so far:
library(shiny)
#source("glycoPipe/glycoPipe.R")

Define ui

ui <- fluidPage(

tags$head(
tags$style(HTML("
@import url('//fonts.googleapis.com/css?family='Helvetica', 'Arial', sans-serif:400,700');
"))
),
fluidRow(
column(width = 3, offset = 1),
headerPanel(
h1("glycoPipe - Identification and Analysis Pipeline R Package",
style = "font-family: 'Lobster', 'cursive';
font-weight: 500; line-height: 1.1;
color: #394779;"))
),

sidebarLayout(
sidebarPanel(

fluidRow(
  column(width = 3, offset = 0,
         img(src="logo.jpeg", height = 50, width = 200, align = "middle"))
),

fluidRow(
  column(width = 3, offset = 0,div(style = "height:25px;background-color: #99CCFF", strong("Urology"))
  )
),

fluidRow(column(width = 10, helpText(div(style = "height:100px;background-color: white;"),
        h4("Do you have a parameter file ready to use (Y/N)?")))
  
),

fluidRow(
  textInput(",",h4("Please enter Y/N"), 
            value = "Enter text...")
)

),
mainPanel(
p("glycoPipe first identifies and annotates glycan compositions
from peaks in non-SID/ MS/MS data, which is optimized for
fragmentation of the glycan portion of glycopeptides."),

p("These glycan identifications are next grouped by shared
annotated glycan peaks, shared probable peptide mass,
and found within the same RT regions."),

p("glycoPipe then processes, annotates and incororates peptide
search engine results from SID/HCD data, which is optimized
for fragmentation of the peptide portion."),

p("Finally the glycan groups are matched to these peptide
identifications by mass and RT adjacency in order to derive
full, annotated glycoconjugate identifications."),

p("glycoPipe outputs numerous plots and tabular results.")
)
)
)

Define server logic

server <- function(input, output){
}

Run the app

shinyApp(ui = ui, server = server)

What I need to write now is text that asks the user for the file that needs to be uploaded to the server, answer will be Y/N and the user will have the ability to browse the PC to find and select the file to be uploaded

All reactive() and eventReactive() functions have to go in your server code. I recommend that you check out the Shiny tutorials on the Rstudio shiny site. These tutorials will walk you through how you can use reactive expressions better than I will be able to explain it here.

In your case, I assume that the source(“glycoPipe/glycoPipe.R”) file contains some sort of functions you wish to use? If that is the case, then you can source that file and then use the functions inside the reactive expression in your server code.

Thank you for your help!

I am writing a text input in the server code. I ask the user to tell me if he/she has a file ready for import for processing by the R code. I would like for the user to be able to browse the PC file system and select the required file. Is there a way to browse?

have you looked at ?fileInput

1 Like

I will thanks. On the mean time I am trying to write userInput <- reactive({
output$myValue <- renderText(input$value)
}) instead of just "output$myValue <- renderText(input$value)" in the server function, but it does not work. The ui and the mainPanel have the correct statement:
" fluidRow(
textInput("value",h4("Please enter Y/N"),
"") in the ui and

  textOutput("myValue")) in the mainPanel()

I want to right a conditional statement inside the server function and it seems that it has to ve done inside a reactive function.

What am I doing wrong?

This is the server function I am referring to:

server <- function(input, output){

output$myValue <- renderText(input$value)
condition <- reactive({if(input$value == 'Y'){
print(input$value)
}})
}

Solved Issue: the code below works. Thanks!

server <- function(input, output){

output$myValue <- renderText(input$value)
condition <- reactive({if(input$value == 'Y'){
renderPrint({input$value})
}})
}

I am having a hard time using fileInput if the user choses "Y" through the select input widget. Could yo help me with this?

The conditional panel does not work. I would like to display the the browser box and check boxes only if the user inputs Y. Could you please explain a little beat "conditonalPanel()? I am having trouble finding information in the tutorials. Thanks

conditionalPanel(

fluidRow(
  column(width = 10,fileInput("file", "upload the file" ),
         helpText("Default max file size 5 MB"),
         tags$hr(),
         h5(helpText("select the parameters below")),
         checkboxInput(inputId = "Header","Header?"),
         br(),
         radioButtons(inputId = "sep", label = "separator", choices = c(Comma = ",", Semicolon = ";", Tab = "\t", Space = " "))
  )
  
)

)

),



mainPanel(
  textOutput("value"),
  tableOutput("inputFile")
)

)
)

server <- function(input, output){

output$value <- renderText({
paste("You chose", input$value)
})
output$inputFile <- renderTable({
fileToRead = input$inputFile
if(is.null(fileToRead)){
return()
}

read.table(fileToRead$datapath, sep = input$sep, header = input$header)

})

}

It looks like you haven't actually given conditionalPanel() a condition. You need to add a javascript expression before the ui code in the conditionalPanel function. You can see more (and an example) in the function's documentation.

Additionally, can you format your code by putting three backticks ("```") before and after your code chunks in the future. It will make reading your code much easier and make it much more likely that you will receive help.

Thank you. We will do.

Thank you. This works as expected:

'''conditionalPanel(
condition = "input.value == 'Y'",
fluidRow(
column(width = 10,fileInput("file", "upload the file" ),
helpText("Default max file size 5 MB"),
tags$hr(),
h5(helpText("select the parameters below")),
checkboxInput(inputId = "Header","Header?"),
br(),
radioButtons(inputId = "sep", label = "separator", choices = c(Comma = ",", Semicolon = ";", Tab = "\t", Space = " "))
)

)'''

Great. As for the three backticks, they need to go on their own lines in order for the code to be formatted properly.

OK. I am getting this error
"cannot coerce class "c("shiny.render.function", "function")" to a data.frame"

when I run the code:
'''
output$tb <- renderUI({
if(is.null(data())){
return()
}else{

  tabsetPanel(tabPanel("Data", tableOutput("table")))
}

})
'''

The main panel contains
'''
uiOutput("tb")
'''
Do you know where this comes from, usually?

data is referring to this:
'''
data <- reactive({
output$inputFile <- renderTable({
fileToRead = input$inputFile
if(is.null(fileToRead)){
return()
}

read.table(fileToRead$datapath, sep = input$sep, header = input$header, stringsAsFactors = input$stringsAsfactors)

})
})
'''