Hi, I need to show the text output of an equation from my code in server.R in the main panel. I am attaching the code below

..I just need the << CL = timeCL * actspeedinkmph * 5/18>> the CL to be shown somewhere on the mainpanel, its the last part of server.R. Could you help me make the necessary changes. Thank You :slight_smile:

library(peakPick)
library(shiny)
library(ggplot2)
library(signal)
ui <- fluidPage(
  titlePanel("Apollo Tyres CLC "),
  sidebarLayout(
    sidebarPanel( 
      fileInput("file1", "Choose CSV File",
                multiple = FALSE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv")),
      tags$hr(),
      splitLayout(numericInput("speed", "Speed of Vehicle",0,width = "200px"),
      numericInput("freq", "Sampling Frequency",200,width = "200px")),
      splitLayout(numericInput("start","Start Time",12.5,width = "100px"),
      numericInput("stop","Stop Time",15,width = "100px")),
      strong(h4("Ploting Data")),
      splitLayout(actionButton("rawdata","RawData_Plot"),
      actionButton("fftdata","FFTPlot")),
      numericInput("num", label = p(em("Cut-off Frequency from FFT Plot(0,100Hz)")),width = "350px",value = 25,min=0,max=100),
      actionButton("bfilter","ButterworthFilterPlot"),
      br(),
      tags$hr(),     
      # Input: Select number of rows to display ----
      radioButtons("disp", "Display",
                   choices = c(Head = "head",
                               All = "all"),
                   selected = "head")
      
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      # Output: Data file ----
      tableOutput("contents"),
      plotOutput("fftplot"),
      plotOutput("btrplot"),
      verbatimTextOutput("btrplotlc")
      
    )
    
  )
)

# Define server logic to read selected file ----
server <- function(input, output) {
 
  output$contents <- renderTable({
    
    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, head of that data file by default
    # or all rows if selected, will be shown.
    
    req(input$file1)
    
    # when reading semicolon separated files,
    # having a comma separator causes `read.csv` to error
    tryCatch(
      {
        df <- read.csv(input$file1$datapath,
                       header = FALSE)
      },
      error = function(e) {
        # return a safeError if a parsing error occurs
        stop(safeError(e))
      }
    )
  
    if(input$disp == "head") {
      return(head(df))
    }
    else {
      return(df)
    }
   
  })
#Plots out the FFT plot
  
  fftplot <- eventReactive(input$fftdata, {
    
    if (is.null(input$file1))
      return()
    infile <- input$file1
    data <- read.csv(infile$datapath,header = F)
    data$V3 = data$V3 - mean(data$V3) ##DC offset removal##
    fftx = fft(data$V3) ##takes FFT##
    magx = Mod(fftx[1:(length(fftx)/2)]) ##magnitude of FFT##
    frequency = seq(0,100,length.out=length(magx)) ##frequency Range##
    fftdf = data.frame(magx,frequency) ##making a data frame of freq and mag##
    aabc = ggplot(fftdf,aes(x=frequency,y=magx,color=frequency)) 
    aabc+ geom_line()
    })
  
  output$fftplot <- renderPlot({fftplot()})
  
  #Plots out the filtered output using Butterworth filter
  btrplot <- eventReactive(input$bfilter,{
    if (is.null(input$file1))
      return()
    infile <- input$file1
    data <- read.csv(infile$datapath,header = F)
    tstart=input$start
    tstop= input$stop
    sfreq = input$freq
    n=input$num
    filter = butter(8,n/100,type="low")
    fsig = signal::filtfilt(filter,data$V3)
    data$fsig = fsig
    plot(data$V1,data$fsig,xlim = c(tstart*sfreq,tstop*sfreq))
      #peak detection
    fsig = signal::filtfilt(filter,data$V3)
    data$fsig = fsig
    peaks = peakpick(matrix(fsig), neighlim=50, deriv.lim = 0.5, peak.min.sd = 0.1,peak.npos = 10L, mc.cores = 1)
    lines(peaks,col='red',xlim=c( tstart*sfreq,tstop*sfreq))
    posPeak = which(peaks == TRUE)
    rangepos = subset(posPeak,posPeak>tstart*sfreq & posPeak<tstop*sfreq)
    negfsig3 = -fsig
    negpeaks = peakpick(matrix(negfsig3), neighlim=50, deriv.lim = 0.5, peak.min.sd = 0.1,peak.npos = 10L, mc.cores = 1)
    lines(negpeaks, col = "blue",xlim=c( tstart*sfreq,tstop*sfreq))
    negPeak = which(negpeaks==TRUE)
    #Calculation of Contact Length
    rangeneg = subset(negPeak,negPeak>tstart*sfreq & negPeak<tstop*sfreq)
    if(rangeneg[1]>rangepos[1]){
      if(length(rangeneg)==length(rangepos)){
        index = rangeneg - rangepos	
      }
      else{
        rangepos2 = rangepos[1:(length(rangepos)-1)]
        index = rangeneg - rangepos2
      }
    }
    else{
      if(length(rangeneg)==length(rangepos)){
        rangeneg2 = rangeneg[2:length(rangeneg)]
        rangepos2 = rangepos[1:length(rangepos)-1]
        index = rangeneg2 - rangepos2
      }
      else{
        rangeneg2 = rangeneg[2:length(rangeneg)]
        index = rangeneg2 - rangepos
      }
    } 
    actspeedinkmph = 10
    timeCL = mean(index)/sfreq
    CL = timeCL * actspeedinkmph * 5/18
    
  })
  
  output$btrplot <- renderPlot({btrplot()})
  #shows the contact length as output text
  btrplotcl <- eventReactive(input$bfilter,{
    if (is.null(input$file1))
      return()
    infile <- input$file1
    data <- read.csv(infile$datapath,header = F)
    tstart=input$start
    tstop= input$stop
    sfreq = input$freq
    n=input$num
    filter = butter(8,n/100,type="low")
    fsig = signal::filtfilt(filter,data$V3)
    data$fsig = fsig
    peaks = peakpick(matrix(fsig), neighlim=50, deriv.lim = 0.5, peak.min.sd = 0.1,peak.npos = 10L, mc.cores = 1)
    posPeak = which(peaks == TRUE)
    rangepos = subset(posPeak,posPeak>tstart*sfreq & posPeak<tstop*sfreq)
    negfsig3 = -fsig
    negpeaks = peakpick(matrix(negfsig3), neighlim=50, deriv.lim = 0.5, peak.min.sd = 0.1,peak.npos = 10L, mc.cores = 1)
    negPeak = which(negpeaks==TRUE)
    #Calculation of Contact Length
    rangeneg = subset(negPeak,negPeak>tstart*sfreq & negPeak<tstop*sfreq)
    if(rangeneg[1]>rangepos[1]){
      if(length(rangeneg)==length(rangepos)){
        index = rangeneg - rangepos	
      }
      else{
        rangepos2 = rangepos[1:(length(rangepos)-1)]
        index = rangeneg - rangepos2
      }
    }
    else{
      if(length(rangeneg)==length(rangepos)){
        rangeneg2 = rangeneg[2:length(rangeneg)]
        rangepos2 = rangepos[1:length(rangepos)-1]
        index = rangeneg2 - rangepos2
      }
      else{
        rangeneg2 = rangeneg[2:length(rangeneg)]
        index = rangeneg2 - rangepos
      }
    } 
    actspeedinkmph = 10
    timeCL = mean(index)/sfreq
    CL = timeCL * actspeedinkmph * 5/18
    show("CL")
  })
  output$btrplotcl <- renderText({btrplotcl()})                       
}

# Create Shiny app ----
shinyApp(ui, server)

A quick solution is create a list output from btrplotcl();

   CL <- list(time = timeCL, speed = actspeedinkmph)
   return(CL)

then

renderText({ 
  output_text <- paste0("CL = ", btrplotcl()$time, " * ", btrplotcl()$speed, " * 5/18")
  return(output_text)
})

Thanks!
Heramb

HI heramb, thanks for replying, i substituted your code in mine, it still isnt showing the text(numeric) output

copied as such.

 #Calculation of Contact Length
    rangeneg = subset(negPeak,negPeak>tstart*sfreq & negPeak<tstop*sfreq)
    if(rangeneg[1]>rangepos[1]){
      if(length(rangeneg)==length(rangepos)){
        index = rangeneg - rangepos	
      }
      else{
        rangepos2 = rangepos[1:(length(rangepos)-1)]
        index = rangeneg - rangepos2
      }
    }
    else{
      if(length(rangeneg)==length(rangepos)){
        rangeneg2 = rangeneg[2:length(rangeneg)]
        rangepos2 = rangepos[1:length(rangepos)-1]
        index = rangeneg2 - rangepos2
      }
      else{
        rangeneg2 = rangeneg[2:length(rangeneg)]
        index = rangeneg2 - rangepos
      }
    } 
    actspeedinkmph = 10
    timeCL = mean(index)/sfreq
    CL = timeCL * actspeedinkmph * 5/18
    CL <- list(time = timeCL, speed = actspeedinkmph)
    return(CL)
  })
  output$btrplotcl <- renderText({
    output_text <- paste0("CL = ", btrplotcl()$time, " * ", btrplotcl()$speed, " * 5/18")
    return(output_text)
  })                     
}

Could you paste a snapshot of the output that you are getting? ideally this should substitute the values for time and speed and give out a equation string.

-Heramb

yes one second,

http://www.sharecsv.com/s/e8cf7cf357627511d94476c9b6c99e04/tire_data.csv

the graph is of btrplot

I am seeing a table and 1 plot. You have mentioned one table output, two plots and 1 text output in the main panel. Is there anything below this plot that you have taken screenshot of?

no,its the last plot and after that the equation is there which should be printed but we are unaavle to d oit

Okay... you have named your output var and a reactive element with the same string.. Try changing output$btrplotcl to output$test and verbatimTextOutput("test")

1 Like

Thanks a lot, it was rather a silly mistake , thanks for pointing it out and for your time

:slight_smile:
This is a good learning. So its my pleasure.. Have a good day!

-Heramb

1 Like