I am trying to execute following code as .R file in Azure batch service:
Data <- st_as_sf(data, coords = c("Longitude", "Latitude")) %>% sf::st_set_crs(4326)
and I am getting following error:
Error in [.data.frame(x, coords) : undefined columns selected
Calls: %>% ... st_as_sf.data.frame -> as.data.frame -> lapply -> [ -> [.data.frame
Execution halted
Any kind of help would be appreciated
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,]
I've got an RMarkdown script that works fine if I run the chunks manually, either one at a time or with "Run All". But when I try to use knitr to generate HTML or a PDF, I'm getting an error: Error in select(responses, starts_with("Q1 ") & !contains("None")) %>% : could not find function "%>%"
The actual full line reads:
cols <- select(responses, starts_with("Q1 ") & !contains("None") ) %>% colnames()
I'm working with data from a survey, where a lot of questions were "select as many as apply" type questions, and there was an open ended "None of the above" option. At this point, I'm pulling out exactly the columns I want (all the Q1 responses, but not Q10 or Q11 responses, and not the open ended response) so I can use pivot_longer() and summarize the responses. It works fine in the script: I get a list of the exact column names that I want, and then count the values.
But when I try to use knitr() it balks on the %>%.
processing file: 02_Survey2020_report.Rmd
|.... | 6%
ordinary text without R code
|......... | 12%
label: setup (with options)
List of 1
$ include: logi FALSE
|............. | 19%
ordinary text without R code
|.................. | 25%
label: demographics gender
Quitting from lines 28-46 (02_Survey2020_report.Rmd)
Error in select(responses, starts_with("Q1 ") & !contains("None")) %>% :
could not find function "%>%"
Calls: <Anonymous> ... handle -> withCallingHandlers -> withVisible -> eval -> eval
Execution halted
A simplified reproducible example gets the same results. I run the following and get what I expect, a tidy table with the count times each answer was selected:
example <- data.frame("id" = c(009,008,007,006,005,004,003,002,001,010), "Q3_Red" = c("","","","Red","","","","Red","Red","Red"), "Q3_Blue" = c("","","","","","Blue","Blue","Blue","",""),
"Q3_Green" = c("","Green","Green","","","","","Green","",""), "Q3_Purple" = c("","Purple","","","Purple","","Purple","","Purple","Purple"),
"Q3_None of the above" = c(009,008,"Verbose explanation that I don't want to count." ,006,005,004,003,002,"Another verbose entry.",010)
)
cols <- select(example, starts_with("Q3") & !contains("None") ) %>% colnames()
example %>%
pivot_longer(cols = all_of(cols),
values_to = "response") %>%
filter(response != "") %>%
count(response)
But when I use ctrlshiftk to output a document, I get the same error:
processing file: 00a_reproducible_examples.Rmd
Quitting from lines 9-25 (00a_reproducible_examples.Rmd)
Error in select(example, starts_with("Q3") & !contains("None")) %>% colnames() :
could not find function "%>%"
Calls: <Anonymous> ... handle -> withCallingHandlers -> withVisible -> eval -> eval
Execution halted
Why is knitr balking at a pipe?
recently I run into similar problem.
Not sure if there is more sophisticated solution, but loading library in each chunk of code worked for me.
To display result of a code, w/o message regarding library loading add message = FALSE.
Example:
```{r, echo=FALSE, message = FALSE}
library(dplyr)
>>your code with dplyr<<
```
When Knitting ans Rmarkdown (R Studio) document containing chunks of R code a chunck returns
line 192 Error in stats::hclust(dd, method=hc.method) :
must have n>= 2 objects to cluster Calls: <Anonymous> ... eval -> ggcorrplot -> .hc_cormat_order -> <Anonymous>
Same piece of code runs fine when run in R (R Studio) and runs from within markdown. This only happens during Knit!
Made no significant changes code in chunk is copy/paste from R-file. Added cache=FALSE and echo=FALSE. Didn't help.
Did put every line in separate chunk. This showed the error not to be at the library (dplyr) line but at the actual ggcorrplot line
library(dplyr)
dfNed_2b <- dfNed_2a %>% dplyr:: select(grep("GDP", names(dfNed_2a)), grep("GDY", names(dfNed_2a)), grep("GNP", names(dfNed_2a)))
dfNed_2b[is.na(dfNed_2b)] <- 0
corrNed_2b <- round(cor(dfNed_2b), 4)
library(ggcorrplot)
ggcorrplot(corrNed_2b, hc.order = TRUE,
type = "lower",
lab = TRUE,
lab_size = 2,
method="circle",
tl.cex=7,
colors = c("orangered1", "white", "darkolivegreen1"),
title="Correlogram GDP, GDY en GNP indicatoren",
ggtheme=theme_minimal)
From the R code I get a great Correlogram. The knit in Rmd returns:
line 192 Error in stats::hclust(dd, method=hc.method) :
must have n>= 2 objects to cluster Calls: <Anonymous> ... eval -> ggcorrplot -> .hc_cormat_order -> <Anonymous>
Error seem to refer back to first line in chunk while actual error is on the ggcorrplot line(s)
==================================================
Error still seems to persist. Changed Rmd to only use external R scripts
Rmd:
### Correlatie plot van de GDP, GDY en GNP indicatoren
{r cache=FALSE}
knitr::read_chunk('6e_corr_dfNed_2b.R')
```{r 6e_corr_dfNed_2b}
```
R:
## ---- 6e_corr_dfNed_2b
library(dplyr)
dfNed_2c <- dfNed_2b %>% dplyr::select(grep("GDP", names(dfNed_2b)),
grep("GDY", names(dfNed_2b)), grep("GNP", names(dfNed_2b)))
dfNed_2c[is.na(dfNed_2c)] <- 0
head(dfNed_2c,2)
library(ggplot2)
library(scales)
library(ggcorrplot)
corrNed_2c <- round(cor(dfNed_2c), 4)
ggcorrplot(corrNed_2c, hc.order = TRUE,
type = "lower",
lab = TRUE,
lab_size = 2,
method="circle",
tl.cex=7,
colors = c("orangered1", "white", "darkolivegreen1"),
title="Correlogram GDP, GDY en GNP indicatoren",
ggtheme=theme_minimal)
Only thing I can think of is that Knitr does like the library(dplyr) being used in aprevious chunk. Thought that was tackled by the cache=FALSE
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