calling a function in R script - function

I wrote a R script in which I wrote a function and called the function. here is the whole script:
PrepData = function(infile){
data <- read.table(infile, header=TRUE, as.is = TRUE, sep = ",")
data = data[, 2:ncol(data)]
merged.data = data
colnames(merged.data[1]) < "CodeCount"
rownames(merged.data) <- merged.data$Name
x <- list(counts = merged.data, raw.counts = merged.data)
return(x)
}
data <- PrepData(myfile.csv)
data
but when I run it using the following command:
Rscript myscript.r
it gives this error:
Error in read.table(infile, header = TRUE, as.is = TRUE, sep = ",") :
object 'myfile.csv' not found
Calls: PrepData -> read.table
Execution halted
do you know how to fix it?

Try to change
data <- PrepData(myfile.csv)
To
data <- PrepData("myfile.csv")
You need to have quotation mark on the filename when you use read table function.

Related

Shiny - assigning argument to function

I am trying to create a shiny app that applies a self-made function to an uploaded dataset, then allows to download the modified results. Here is my code:
library(shiny)
library(tidyverse)
namkurz <- function(data, a_spalte) {
kuerzel <- vector(length = length(data$a_spalte))
for (i in 1:length(data$a_spalte)){
spez = data$Art[i]
s = unlist(strsplit(spez, " ", fixed = TRUE))
s = substr(s, 1, 2)
s = paste(s, collapse = ' ')
kuerzel[[i]] = s
}
data <- data %>%
mutate(kurz = kuerzel)
}
ui <- fluidPage(
fileInput('upload','Deine Kartierungsdaten'),
textInput('art', 'Wie heißt die Spalte mit Artnamen?'),
downloadButton('analyse','Artenkürzel hinzufügen')
)
server <- function(input, output, session) {
data <- reactive({
req(input$upload)
ext <- tools::file_ext(input$upload$name)
switch(ext,
csv = vroom::vroom(input$upload$datapath, delim = ";"),
validate("Invalid file; Please upload a .csv file")
)
})
art <- reactive(input$art)
output$analyse <- downloadHandler(
filename = function() {
paste0('mit_kuerzel', ".csv")
},
content = function(file) {
ergebnis <- reactive(namkurz(data(), art()))
vroom::vroom_write(ergebnis(), file)
}
)
}
shinyApp(ui, server)
When trying to save the output I get a 'Warning: Unknown or uninitialised column:' error. I think my problem is in the assignment of argument 'art' to the 'ergebnis' object, but I can't find the way to fix it.
I recommend a few things:
(Required) In your function, a_spalte is a character vector and not the literal name of column in the frame, so you need to use [[ instead of $, see The difference between bracket [ ] and double bracket [[ ]] for accessing the elements of a list or dataframe and Dynamically select data frame columns using $ and a character value.
Change all references of data$a_spalte to data[[a_spalte]].
namkurz <- function(data, a_spalte) {
kuerzel <- vector(length = length(data[[a_spalte]]))
for (i in 1:length(data[[a_spalte]])){
spez = data$Art[i]
s = unlist(strsplit(spez, " ", fixed = TRUE))
s = substr(s, 1, 2)
s = paste(s, collapse = ' ')
kuerzel[[i]] = s
}
data <- data %>%
mutate(kurz = kuerzel)
}
Your function is a bit inefficient doing things row-wise, we can vectorize that operation.
namkurz <- function(data, a_spalte) {
spez <- strsplit(data$Art, " ", fixed = TRUE)
data$kurz <- sapply(spez, function(z) paste(substr(z, 1, 2), collapse = " "))
data
}
(Optional) The content= portion of downloadHandler is already reactive, you do not need to wrap namkurz in reactive. Because of this, you also don't need to treat ergebnis as reactive.
output$analyse <- downloadHandler(
filename = ...,
content = function(file) {
ergebnis <- namkurz(data(), art())
vroom::vroom_write(ergebnis, file)
}
)
(Optional) Your output filename is fixed, so two things here: if it's always going to be "mit_kuerzel.csv", then there's no need for paste0, just use function() "mit_kuerzel.csv".
However, if you are intending to return a file named something based on the original input filename, one could do something like:
filename = function() {
paste0(tools::file_path_sans_ext(basename(input$upload$name)),
"_mit_kuerzel.",
tools::file_ext(input$upload$name))
},
to add _mit_kuerzel to the base portion of the uploaded filename. Note that the file in the content= section is never this name, the new_mit_kuerzel.csv is the filename offered to the downloading browser as a suggestion, that is all.
(Optional) You are using a .csv file extension in the downloadHandler, but the default for vroom::vroom_write is to use delim = "\t", which is not a CSV. I suggest either adding delim = ";" (or similar), or changing the returned filename extension to .tsv instead.

convert json to datatable using rjsonio

I have properly formatted JSON fetched through a standard API.
API Basically returns an array of JSON objects each time I fetch data.
LIke this:
[
{},
{},
{}
]
I have used JSON editor to check structure of JSON data and that looks perfect.
I need to convert it to CSV so tried this:
freshDeskRaw <- fromJSON(freshDeskTickets)
tmp <- lapply(freshDeskRaw , function(u)
lapply(u, function(x) if(is.null(x)) NA else x)
)
tmp <- lapply( tmp, as.data.frame)
tmp <- do.call( rbind, tmp )
At
tmp <- lapply( tmp, as.data.frame)
, I get an error:
tmp <- lapply( tmp, as.data.frame)
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 0, 1
how do I fix this? what looks wrong? I have tried to use
as.data.frame(tmp) as well but still get the same error.
The problem might be that the JSON above produces empty lists and
is.null(list())
# [1] FALSE
Try
is.null(x) || (is.list(x) && length(x) == 0)

Eval parse for a JSON

I am trying to automate a JSON parsing in R (I had to remove the "https:// from the URLs because I don't have enough reputation points):
library(Quandl)
library(jsonlite)
tmp <-
fromJSON("www.quandl.com/api/v3/datasets.json?database_code=WIKI&page=2",flatten = TRUE)
for various numbers in page=X. The above code snippet executes properly. For that I am trying to use eval(parse()) but I am doing something wrong. So I have the following:
text1 <- 'fromJSON("www.quandl.com/api/v3/datasets.json?database_code=WIKI&page='
text2 <- '",flatten = TRUE)'
and to verify that I create the string properly:
> text1
[1] "fromJSON(\www.quandl.com/api/v3/datasets.json?database_code=WIKI&page="
> text2
[1] "\",flatten = TRUE)"
> cat(text1,n,text2,sep="")
fromJSON("www.quandl.com/api/v3/datasets.json?database_code=WIKI&page=2",flatten = TRUE)
BUT when I try to execute:
koko <- eval(parse(text = cat(text1,n,text2,sep="")))
where n<-2 or any other integer then the console freezes with the following error messsage:
?
Error in parse(text = cat(text1, n, text2, sep = "")) :
<stdin>:1:4: unexpected '{'
1: D_{
^
What am I doing wrong here?
Have a read of the difference between paste and cat
cat will just print to the screen, it won't return anything. To create a string you should use paste or paste0.
For example, consider
concat <- cat(text1, n, text2)
p <- paste0(text1, n, text2)
Even when running concat <- cat(text1, n, text2), it prints the output to the console, and concat is empty/NULL
The solution is to use paste0 to create the string expression
text1 <- 'fromJSON("http://www.quandl.com/api/v3/datasets.json?database_code=WIKI&page='
text2 <- '",flatten = TRUE)'
n <- 2
koko <- eval(parse(text = (paste0(text1, n, text2))))
Also, you don't need to use eval, you can use paste0 directly
text1 <- 'http://www.quandl.com/api/v3/datasets.json?database_code=WIKI&page='
n <- 2
koko <- fromJSON(paste0(text1, n), flatten=TRUE)

Loading json encoded log files into R for analysis

I have a log file with each line a json-encoded entry:
{"requestId":"5550d","partnerId":false,"ip":"170.158.3.1", ... }
I tried reading each line, json_decode and then append to the dataframe:
loadLogs <- function(fileName) {
conn <- file(fileName, "r", blocking = FALSE)
linn <- readLines(conn)
long <- length(linn)
df = data.frame(requestId = character(0),
partnerId = character(0),
ip = character(0)
)
for (i in 1:long) {
jsonRow <- fromJSON(linn[i])
df <- rbind(df, data.frame(requestId = jsonRow$requestId,
partnerId = as.character(jsonRow$partnerId),
ip = jsonRow$ip
}
close(conn)
return(df)
}
The above code is extremely slow for large files though. Is there any way to speed this up? A few options I can think of at the moment:
pre-allocate the dataframe as its copying the entire data for a new append
using apply function on the json_decode
???
How would I do (1) and (2) in R? I'm new.
Thanks for looking at my question.

"attempt to select less than one element" with R and json

I use the package smappR for twitter-harvesting and analysis. When using the function getTimeline (https://github.com/SMAPPNYU/smappR/blob/master/R/get-timeline.R) in this loop:
#import excel-file with twitter usernames
riksdagsledamoter <- read.xlsx("lista_riksdagsledamoter_twitter.xlsx", sheetIndex=1)
# delete MPs with out twitter account
twitterusers <- riksdagsledamoter[!(is.na(riksdagsledamoter$twitterusername)), ]
row.names(twitterusers) <- seq(nrow(twitterusers))
names(twitterusers) <- c("twitterusername", "name", "parti") #convertering to vector
twitterusersvector <- as.vector(twitterusers$twitterusername)
for(i in 1:length(twitterusersvector)){
user <- twitterusersvector[i]
# user <- twitterusers$twitterusername[i]
# rov <- as.vector(user)
getTimeline(screen_name = user,
filename = "pa324.json", # where tweets will be stored
n=3200, ## number of tweets to download (max is 3,200)
oauth_folder = "~/Dropbox/Privat/R/credentials",
sleep=10, )
if ( file.info("pa324.json")$size > 120000000 ){
tweetsToMongo(file.name="pa324.json", ns="xxxx.yyyy",
host="192.168.x.x", username="", password="")
file.remove("pa324.json")
}
}
I get this error:
Error in json.data[[tweets]] : attempt to select less than one element
I have googled it and have tried:
row.names(twitterusers) <- seq(nrow(twitterusers))
With no help. What could be wrong?