-1

I'm trying to upload an R Script as a file so that when it opens it runs automatically so that the output can be seen, however when I try it opens this and no sign of any output. I don't know if it's significant but the source function doesn't execute the code and just prints the file path? However source with echo does execute the code

I've tried ticking the source on save option but that doesn't seem to work. When I save the link and send it to myself via email to test it, it doesn't run. and when I specifically locate the path through 1. File 2. Source File it says 'path' does not exist. Even though its the exact one I could run and execute through source with echo on R Studio.

#activating package required to read excel dataset
library(readxl)

options(warn = -1)
#irrelevant cols skipped, remaining are classified as numeric or      text 
soccer_data <- read_excel("Downloads/soccer data.xlsx", 
                          col_types = c("numeric", "text", "skip", 
                                        "skip", "skip", "skip",     "numeric", 
                                        "numeric", "numeric",     "numeric", 
                                        "numeric", "numeric",     "numeric", 
                                        "numeric"))

#print statistical summary of data, and first and last 10 rows
summary(soccer_data)
head(soccer_data,10)
tail(soccer_data,10)
sum(complete.cases(soccer_data))
Nathan
  • 19
  • 4
  • Where are you trying to upload to? Can you include the exact input and output of your use of `source` and where you use it? That will help us to reproduce your results and understand your problem better. – Milliron X Sep 06 '19 at 06:07
  • I'm trying to upload the code online via email so it can be accessed by someone else and automatically executed. The Attached code is the exact input, the output when I use source, simply produces in the console > source('~/Downloads/please fucking work.R') (excuse the language). Source echo as mentioned before executes the code – Nathan Sep 06 '19 at 06:16

1 Answers1

0

You seem to have two distinct problems:

Data portability

Your code is balking at the "path" of your excel file. If you wrote the R script in your home directory (~), then the code would be looking for the excel file in ~/Downloads/soccer data.xlsx. When you download it, the path that you execute it from is ~/Downloads, so the script looks for ~/Downloads/Downloads/soccer data.xlsx, which doesn't exist. It's a simple matter of absolute vs. relative paths.

Your code is executing just fine until it reaches the read_excel line, when it throws an error and stops. Although I don't have your data, here is what a non-existent path looks like.

Script contents

library(readxl)
data <- read_excel('somewhere over the rainbow')

summary(data)
head(data,10)
tail(data,10)
sum(complete.cases(data))

Console

> source('myscript.R')
Error: `path` does not exist: ‘somewhere over the rainbow’

Note that it's generally better practice to include your data as a part of your script if you are going to send it to someone else. Here are instructions of how you could embed the soccer data in your R script directly.

Displaying results

If you want to display results, you can

  1. Use the source('myscript.R', echo=TRUE) route, like you already have
  2. Add print statements to your script, like this
print(summary(data))
print(head(data,10))
print(tail(data,10))
print(sum(complete.cases(data)))

That will show your results even if you're using Rscript, and is a better option, in my mind.

Milliron X
  • 1,174
  • 14
  • 26