Returning a plot from a function

shiny
rstudio

#1

Hello,

I am having trouble with creating a function that will return a plot. I am displaying accelerometer data live stream,my code will better explain what I am trying showing I believe.
Here is my code:
Server.R

library(shiny)
library(shinydashboard)
library(readr)

Shinyserver <- function(input, output, session) {
  # Output sidebarpanel detail to UI  -------------------------------------------

  
  
  
  # Output body detail to UI  -------------------------------------------    
  output$body <- renderUI({
    tags$style(type="text/css",
".recalculating {opacity: 1.0;}"
)
    div(width=12,
        
        # Info Boxes --------------------------------------------------------------


        
    fluidRow(tags$style(type="text/css", ".recalculating {opacity: 1.0;}"),
              photo_funct(1),
             box(plot_funct(1), height = 427, width = 9)),
	  fluidRow(tags$style(type="text/css", ".recalculating {opacity: 1.0;}"),
	           photo_funct(2),
	           box(plot_funct(2), height = 427, width = 9)),
	  fluidRow(tags$style(type="text/css", ".recalculating {opacity: 1.0;}"),
	           photo_funct(3),
	           box(plot_funct(3), height = 427, width = 9)),
	  fluidRow(tags$style(type="text/css", ".recalculating {opacity: 1.0;}"),
	           photo_funct(4),
	           box(plot_funct(4), height = 427, width = 9)),
	  fluidRow(tags$style(type="text/css", ".recalculating {opacity: 1.0;}"),
	           photo_funct(5),
	           box(plot_funct(5), height = 427, width = 9)),
	   fluidRow(tags$style(type="text/css", ".recalculating {opacity: 1.0;}"),
	            photo_funct(6),
	            box(plot_funct(6), height = 427, width = 9)),
	   fluidRow(tags$style(type="text/css", ".recalculating {opacity: 1.0;}"),
	            photo_funct(7),
	            box(plot_funct(7), height = 427, width = 9)),
	   fluidRow(tags$style(type="text/css", ".recalculating {opacity: 1.0;}"),
	            photo_funct(8),
	           box(plot_funct(8), height = 427, width = 9)))
    
     })
 
  
  autoInvalidate <- reactiveTimer(20)

# plots -------------------------------------------------------------------


plot_funct <- function(which){
  autoInvalidate()
  tryCatch(chuck <<- read.csv(paste0("logfile",which,".txt"),header=FALSE), error=function(e) NULL)
  if(exists("chuck")==FALSE){
    }else{
  chuck1 <- na.omit(chuck)
  if(!is.null(chuck) && nrow(chuck) != 0)
  {
    rownumber <- 1:nrow(chuck)
    rownumber2 <- cbind(rownumber, chuck[,2])

    result <<- plot(rownumber2[,1],rownumber2[,2], type="l", xlim = c(0,100), ylim = c(0,5), xaxt='n', yaxt='n', ylab = "", xlab = "", main = "RFID: 982 000121745488", cex.main = "3")
  return(result)
    }}
}


photo_funct <- function(which){ 
  autoInvalidate()
  tryCatch(activity_level_math <- read.csv(paste0("logfile",which,".txt"), header=FALSE), error=function(e) NULL)
  if(exists("activity_level_math")==FALSE){
    weight <- read.csv(paste0("weight",which,".txt"), header=FALSE)
    photo <- box(h1(paste("Weight: ", weight$V1), align = "center"), tags$img(src=paste0(which,".jpg"),height = 350, width = 350, style="display: block; margin-left: auto; margin-right: auto;"),background = "red", width = 3, height = 450)
    return(photo)
    }else{
  activity_level_math <-na.omit(activity_level_math)
  activity_level <<- mean(activity_level_math$V2)
  battery <- activity_level_math$V3[1]
  battery1 <<- na.omit(battery)
  if(battery1 < 3.4){
    weight <- read.csv(paste0("weight",which,".txt"), header=FALSE)
    photo <- box(h1(paste("Change Battery"), align = "center"), tags$img(src=paste0(which,".jpg"),height = 350, width = 350, style="display: block; margin-left: auto; margin-right: auto;"), background = "red" , width = 3, height = 450)
    return(photo)    
  }else {
 if(activity_level > 0.95 && activity_level < 1.05)
    { 
   weight <- read.csv(paste0("weight",which,".txt"), header=FALSE)
   photo <- box(h1(paste("Weight: ", weight$V1), align = "center"), tags$img(src=paste0(which,".jpg"),height = 350, width = 350, style="display: block; margin-left: auto; margin-right: auto;"),background = "yellow", width = 3, height = 450)
   return(photo)    }
 else if (activity_level > 1 && activity_level < 2)
  { 
   weight <- read.csv(paste0("weight",which,".txt"), header=FALSE)
   photo <- box(h1(paste("Weight: ", weight$V1), align = "center"), tags$img(src=paste0(which,".jpg"),height = 350, width = 350, style="display: block; margin-left: auto; margin-right: auto;"),background = "green", width = 3, height = 450)
   return(photo)  }
 else if (activity_level > 2)
  { 
   weight <- read.csv(paste0("weight",which,".txt"), header=FALSE)
   photo <- box(h1(paste("Weight: ", weight$V1), align = "center"), tags$img(src=paste0(which,".jpg"),height = 350, width = 350, style="display: block; margin-left: auto; margin-right: auto;"),background = "red", width = 3, height = 450)
   return(photo)  }}}
}

ur.R

library(shiny)
library(shinydashboard)

header <- dashboardHeader(dropdownMenuOutput("notificationsMenu"), title = tags$a(href='https://www.datamuster.net.au/', tags$img(src="DM1.png", height = 35, width = 110)), titleWidth = 250)
sidebar <- dashboardSidebar(disable=TRUE, uiOutput("sidebarpanel"))

body <- dashboardBody(uiOutput("body"))

ui <- dashboardPage(skin="black",header, sidebar, body)

Sample data:

4795.304,0.989987,3.4
4795.51,0.9932784,3.5
4795.716,0.9951183,3.5
4795.92,0.9937792,3.5
4796.126,0.9941151,3.5
4796.333,0.9942585,3.5
4796.539,0.9962009,3.5
4796.745,0.9934099,3.5
4796.951,0.9943963,3.5
4797.157,0.9964195,3.5
4797.363,0.9945069,3.5
4797.569,0.9947223,3.5
4797.773,0.9935158,3.5
4797.979,0.9930292,3.5
4798.185,0.9948092,3.5
4798.391,0.9930816,3.5
4798.595,0.9877461,3.5
4798.801,0.993361,3.5
4799.005,0.9942733,3.5
4799.211,0.9977367,3.5
4799.417,0.9930295,3.5
4799.622,0.9922276,3.5
4799.827,0.9950614,3.5
4800.032,0.9960138,3.5
4800.237,0.9946167,3.5
4800.442,0.9965359,3.5
4800.647,0.9924312,3.5
4800.853,0.9919447,3.5
4801.059,0.9923678,3.5
4801.265,0.9889902,3.5
4801.471,0.9885372,3.5
4801.676,0.9943115,3.5
4801.882,0.9956486,3.5
4802.086,0.9920061,3.5
4802.292,0.9952565,3.5
4802.497,0.9916264,3.5
4802.703,0.9933941,3.5
4802.907,0.9940879,3.5
4803.113,0.9962399,3.5
4803.318,0.9916958,3.5
4803.523,0.9935183,3.5
4803.729,0.9940549,3.5
4803.935,0.9971527,3.5
4804.141,0.9898958,3.5
4804.347,1.001381,3.5
4804.553,0.9931783,3.5
4804.759,0.9925856,3.5
4804.965,0.9917496,3.5
4805.171,0.996786,3.5
4805.377,0.9966578,3.5
4805.583,0.9955171,3.5
4805.789,0.9940113,3.5
4805.995,0.9936245,3.5
4806.201,0.9898512,3.5
4806.407,0.9942101,3.5
4806.613,0.9943449,3.5
4806.818,0.9926554,3.5
4807.024,0.9941435,3.5
4807.229,0.9880523,3.5
4807.434,0.9932559,3.5
4807.639,0.9934172,3.5
4807.845,0.9946555,3.5
4808.051,0.9921254,3.5
4808.256,1.000156,3.5
4808.461,0.9925559,3.5
4808.666,0.9950869,3.5
4808.873,0.9939869,3.5
4809.078,0.9975284,3.5
4809.283,0.993193,3.5
4809.49,0.9950306,3.5
4809.696,0.9963129,3.5
4809.9,0.9940285,3.5
4810.106,0.9918531,3.5
4810.312,1.00132,3.5
4810.518,0.9920932,3.5
4810.724,0.9942331,3.5
4810.93,0.994749,3.5
4811.135,0.9925523,3.5
4811.341,0.9951307,3.5
4811.547,0.994047,3.5
4811.754,0.9943303,3.5
4811.958,0.9926687,3.5
4812.164,0.9900825,3.5
4812.371,0.9922136,3.5
4812.577,0.9876211,3.5
4812.782,0.9935301,3.5
4812.988,0.9939828,3.5
4813.194,0.9966561,3.5
4813.399,0.9901903,3.5
4813.606,0.9948195,3.5
4813.812,0.9932545,3.5
4814.018,0.995314,3.5
4814.224,0.9926063,3.5
4814.429,0.9932002,3.5
4814.635,0.9971574,3.5
4814.841,0.9920959,3.5
4815.047,0.9954756,3.5
4815.254,0.9932692,3.5
4815.46,1.9975183,3.5
4815.666,1.994017,3.4


#2

Could we have a minimal reproducible example please?


#3

Sorry, I've added the ui.R in and a full logfile1.txt.


#4

It generally helps if you ask a question. There's no question here.


#5

Sorry, things have been such a rush here.
My question is how do I get a function to return a plot so that this line would return plot from the data in logfile1.txt:
plot_funct(1)
logfile2.txt:
plot_funct(2)