aggregate function fails in knitr but succeeds in console - r

I have the following R code:
cleanData <- aggregate(x = stormData[c("FATALITIES", "INJURIES")],
by = list(tolower(stormData$EVTYPE)), sum)
It runs fine in the R Studio console, but when I copy/paste this in a chunk in knitr it generates an error:
Error in `$<-.data.frame`(`*tmp*`, "HARM", value = c(15, 0, 2, 2, 2, 6, :
replacement has 902297 rows, data has 898
Calls: <Anonymous> ... withVisible -> eval -> eval -> $<- -> $<-.data.frame
I'm not sure why this is happening.

Related

Download summary to MS Word

This is what i've written for my task:
1:12
12:1
rep(1:2,12)
rep("Red",6)
c(rep("Red",6),rep("Blue",6))
sample(1:12,4)
sample(1:12)
sample(c(rep("Red",6),rep("Blue",6)))
alder <- data$alder
respons <- data$respons
plot(alder, respons)
behandling <- rep(c("FB2M","Placebo"), each = 15)
behandling <- factor(behandling)
data$behandling <- behandling
boxplot(respons ~ behandling, data=data)
data$randomisering <- sample(data$behandling)
boxplot(respons ~ data$randomisering, data=data)
Menn <- data[data$kjonn == 'Mann',]
table(Menn$randomisering)
..but when i try to file->compile report to MS Word, this is what happens:
Quitting from lines 3-33 (Oving3.spin.Rmd)
Error in data$alder : object of type 'closure' is not subsettable
Calls: <Anonymous> ... withVisible -> eval_with_user_handlers -> eval -> eval
Execution halted
Can someone help me?

code can be progress but shown has error when i convert html in r

I would like to convert my r file to HTML, but it always turns this error even if my code is good to process.
error
Quitting from lines 71-90 (HW1.Rmd)
Error in eval(quote(list(...)), env) : object 'MAD' not found
Calls: <Anonymous> ... [.data.frame -> order -> standardGeneric -> eval -> eval -> eval
Execution halted
error part code
ss <- data.frame(
MED = apply(exprs(ESET),1,median),
MAD = apply(exprs(ESET),1,mad)
)
sort(ss$MAD, decreasing = TRUE)[1:101]
highlight_df <- ss %>%
filter(MAD>3.800260)
ss %>%
ggplot(aes(x=MED,y=MAD)) +
geom_point(pch=20) +
geom_point(data=highlight_df,
aes(x=MED,y=MAD,color = top100),
color='red',
size=3)
top <- ss[order(-MAD,-MED),]
top[1:10,]

Using output of summarise() in R

How do I use the output of the summarise() in R. I know it creates a table, but I can't seem to use it for further calculations.
I'm trying to calculate the margin of error for a sample proportion.
us_pro <- us12 %>%
summarise(us_prop=sum(response == "atheist")/sum(response == "atheist" | response == "non-atheist"), n=n()) %>%
as.data.frame()
us_pro %>%
me_perc=qnorm(0.975)*sqrt(us_prop*(1-us_prop)/n)
The ME calculation errors out with
Error in eval(expr, envir, enclos) : object 'us_prop' not found Calls:
... handle -> withCallingHandlers -> withVisible -> eval
-> eval Execution halted
I'm guessing it's a very basic error, but i can't understand what

How to suppress knitr/RMarkdown "eval eval withVisible..." output in code chunk with tracemem function?

I have a simple RMarkdown document:
---
title: "Untitled"
author: "Author"
date: "November 22, 2015"
output: html_document
---
```{r}
x <- 1:10
tracemem(x)
x[2] <- 22
```
When I knit this into HTML (using RStudio -> Knit HTML button) it shows a strange output from tracemem ("eval eval withVisible..."):
x <- 1:10
tracemem(x)
## [1] "<0x32b7c28>"
x[2] <- 22
## tracemem[0x32b7c28 -> 0x33cda68]: eval eval withVisible withCallingHandlers handle evaluate_call <Anonymous> in_dir block_exec call_block process_group.block process_group withCallingHandlers process_file <Anonymous> <Anonymous>
## tracemem[0x33cda68 -> 0x2bcd818]: eval eval withVisible withCallingHandlers handle evaluate_call <Anonymous> in_dir block_exec call_block process_group.block process_group withCallingHandlers process_file <Anonymous> <Anonymous>
If I run the same code directly (in R without knitr or via "run current chunk") I get the output as expected:
> x <- 1:10
> tracemem(x)
[1] "<0x504eee0>"
> x[2] <- 22
tracemem[0x504eee0 -> 0x5063460]:
tracemem[0x5063460 -> 0x4feec68]:
Where do these strange "eval eval withVisible..." messages come from and how can I suppress them?

R Markdown: my code runs on the console, but not when I try to knit to HTML

I am running the following code (referencing a data frame in my Global Environment):
schedule_sort_1 <- group_by(FileName, Date)
schedule_table_1 <- summarize(schedule_sort_1, Run_Time_hrs = sum(as.numeric(Interval_s))/3600, Start_Time = Time[1], End_Time = Time[length(Time)], Mean_Outdoor_Temp = mean(Outside.Air.Temp))
kable(schedule_table_1, digits = 3, align = "c")
When I test using the "Run Current Chunk" button, the table appears as expected; however, when I then try to knit the document, I get the following error:
Error in group_by_(.data, .dots = lazyeval::lazy_dots(...), add=add) : object 'FileName' not found Calls: <Anonymous> ...withVisible -> eval -> eval -> group_by -> group_by_ Execution halted
Any tips would be appreciate...thx

Resources