0

I'm trying to load a script from a seminar on meta analysis, which supposedly gives you an app/tool to run meta analysis. The script looks like this:

if(!require(shiny)){install.packages('shiny')}
if(!require(shinythemes)){install.packages('shinythemes')}
if(!require(rstudioapi)){install.packages('rstudioapi')}

library(shiny)
library(shinythemes)
ui <- fluidPage(theme=shinytheme("flatly"),
                titlePanel("Mini meta-analysis of your own studies (correlations)"),
                sidebarLayout(
                  sidebarPanel(
                    h6("To create the mini meta-analysis, you need to upload a CSV file with the following columns:"),                
                    h6("Study: Name of the study/experiment [String]"),        
                    h6("N    : Sample size"),                    
                    h6("R    : Correlation between X and Y"),
                    fileInput("file1", "Choose CSV File",
                              accept = c(
                                "text/csv",
                                "text/comma-separated-values,text/plain",
                                ".csv"))
                  ),
                  mainPanel(h3("Loaded file:"), br(),
                            tableOutput("fileload"), br(),
                            h3("Meta analysis Results:"), br(),
                            h5(verbatimTextOutput("metaresult")), br(),
                            plotOutput("forestplot"),br(),
                            plotOutput("funnelplot"),br(),
                            h3("Test for asymmetry & trim-fill procedure"), br(),
                            h5(verbatimTextOutput("metaresult3")), br(),                            
                            h5(verbatimTextOutput("metaresult2")), br(),                            
                            plotOutput("funnelplottrim"))
                )
)

server <- function(input, output) {
  
  this.dir <- dirname(rstudioapi::getActiveDocumentContext()$path)
  setwd(this.dir)
  source("minimetacor.R")
  
  
  InFile <- reactive({
    
    validate(
      need(input$file1, "Choose a file to see meta-analysis output results!")
    )
    
    inFile <- input$file1
    
    if (is.null(inFile))
      return(NULL)
    
    idataset <- read.table(inFile$datapath, header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)
    
  })
  
  MetaFile <- reactive({
    idata <-InFile()
    odataset<-CorMeta(idata$R, idata$N,idata$Study)
  })
  
  
  
  output$forestplot <- renderPlot({
    
    PrintPlotMetaR(MetaFile())
  })
  
  output$metaresult <- renderPrint({ 
    summary(MetaFile())
  })
  
  output$metaresult2 <- renderPrint({ 
    taf <- trimfill(MetaFile())
    taf
  })
  
  output$metaresult3 <- renderPrint({ 
    regtest(MetaFile())
  })
  
  output$fileload <- renderTable({ 
    
    InFile()
    
  })
  
  output$funnelplot <- renderPlot({
    
    PrintFunnelMetaR(MetaFile())
  })
  
  output$funnelplottrim <- renderPlot({
    taf <- trimfill(MetaFile())
    taf
    PrintFunnelMetaR(taf)
  })
  
}
shinyApp(ui = ui, server = server)

However when I ran it, it briefly showed what I wanted, then gave me this error:

Listening on http://127.0.0.1:4427 Warning: Error in setwd: cannot change working directory 50: setwd 49: server [#4] Error in setwd(this.dir) : cannot change working directory

I'm not sure what this means. I assume its referring to this specific part of the code, however, I'm not sure if I should fiddle with this:

  this.dir <- dirname(rstudioapi::getActiveDocumentContext()$path)
  setwd(this.dir)
  source("minimetacor.R")

Can anybody offer some insight? I don't wanna edit the wrong thing and it becomes unworkable.

Shawn Hemelstrand
  • 2,676
  • 4
  • 17
  • 30
  • 1
    Are you running this from inside RStudio? Have you saved the script file in the same place that the `minimetacor.R` file exists? You can try commending out the first two of the three lines you highlighted and just keep the `source()` one. It's not really a good practice to put `setwd()` in a script anyway, – MrFlick Aug 18 '21 at 02:01
  • As far as I'm aware, I don't have such a file, I only copied this script and pasted it into R Studio. I deleted the first two parts you mentioned and it spit out this: Listening on http://127.0.0.1:6565 Warning in file(filename, "r", encoding = encoding) : cannot open file 'minimetacor.R': No such file or directory Warning: Error in file: cannot open the connection 51: file 50: source 49: server [#4] Error in file(filename, "r", encoding = encoding) : cannot open the connection – Shawn Hemelstrand Aug 18 '21 at 02:13
  • 2
    Well make sure to save the file after copy/pasting into RStudio. And you'll need to locate and download the `minimetacor.R` file as well. You'll have to ask whomever gave the seminar or created the tool where that file is. – MrFlick Aug 18 '21 at 02:22
  • I think I found that same file somewhere in his dropbox. Is there somewhere specific I should save each file? – Shawn Hemelstrand Aug 18 '21 at 02:43
  • 1
    Save both files in the same folder. Then open the non-"minimetacor" file in RStudio, but this time run the original code without the lines removed. – MrFlick Aug 18 '21 at 02:44
  • Not sure why my previous comment wasn't saved, but it worked! Thanks a bunch. I've included an answer to the problem below. – Shawn Hemelstrand Aug 18 '21 at 03:04

1 Answers1

0

It looks like I just needed to download another file, the "minimetacor" file shown in this directory code I mentioned:

  this.dir <- dirname(rstudioapi::getActiveDocumentContext()$path)
  setwd(this.dir)
  source("minimetacor.R")

Once I downloaded both the minimetacor file and downloaded the script I was using, I put them in the same folder and ran the script again. It worked right away.

Thanks to MrFlick for the help!

Shawn Hemelstrand
  • 2,676
  • 4
  • 17
  • 30