Why is knitr choking on a pipe that works fine otherwise? - r

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<<
```

Related

Consistent errors when uploading an R Leflet map to RPubs

I'm attempting to upload a large leaflet map r-file (97.5mb) from RStudio to RPubs. I keep getting the same error and I wonder if anyone has a solution/work-around. The error states:
"Error in rawToChar(response$headers) : argument 'x' must be a raw
vector Calls: ... http -> -> grep ->
parse_headers -> rawToChar Execution halted"
I've constructed a large polygon choropleth shapefile detailing all of the census tracts for Pennsylvania, New Jersey, and Delaware. When I hit "publish" in the Viewer window of RStudio, I'm first directed to choose where I want to publish the file: RPubs or RStudio Connect. I have an RPubs account so I choose that option and then click "Publish" in the second RPubs option window. I then receive a buffering label followed by an error message stating:
"Error in rawToChar(response$headers) : argument 'x' must be a raw
vector Calls: ... http -> -> grep -> parse_headers -> rawToChar
Execution halted"
Here's the code for my map:
shapefile <- readOGR("filename")
shapefile <- spTransform(shapefile,"+proj=longlat +datum=WGS84")
### Creating the leaflet map ###
bins <- c(60,65,70,75,80,85,90,95)
pal <- colorBin("YlGnBu", domain=shapefile$lifeexpectancy, bins=bins)
m <- leaflet(shapefile) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
setView(lng=-75.165222, lat=39.952583, 11) %>%
addPolygons(fillColor = ~pal(lifeexpectancy),
weight=0.5,
opacity=1,
color="gray",
fillOpacity=0.4,
highlightOptions = highlightOptions(color="white",
weight=1.5,
bringToFront = T,
opacity = 0.7),
label=mapply(
function(x,y) {
HTML(sprintf("<strong>Life Expectancy</strong><br/><em>%s years</em><br/><strong>Median Household Income</strong><br/><em>$%s</em>",
htmlEscape(x),htmlEscape(y)))
},
shaoefile$lifeexpectancy,shapefile$`Median Household Income`, SIMPLIFY = F),
labelOptions = lapply(1:nrow(shapefile),function(x) {
labelOptions(direction = "auto",
textsize = "15px")
})) %>%
addLegend(pal=pal,
values=bins,
opacity = 0.7,
title ="Life Expectancy<br>(in years)",
position="bottomright")
I've also received the following error, once:
"Error in curl::curl_fetch_memory(url, handle = handle) : Empty
reply from server Calls: ... tryCatch -> tryCatchList ->
tryCatchOne -> Timing stopped at: 0.03 0.05 1.3 Execution
halted"
I've seen previous answers about fixing the .Rprofile code in my system's folder, which none worked for me. Any help you can offer would be great.
In order to get slimmer html files, you could try to simplify geometry of polygons. See this notebook for more details.

Why does a piece of R code does not work during Knit in Rmarkdown

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

Script works fine in Rstudio but not in Rscript

I would like to schedule a R script which works fine via Rstudio. But when try to run the script with Rscript I got an error. The script take some parameters in a csv file and try to gather data on opentsdb.
library(devtools)
install_local("/home/me/Downloads/opentsdbr-master")
install_local("/home/me/Downloads/nomalyDetection-master")
install_local("/home/me/Downloads/anomalydots", force="TRUE")
install_local("/home/me/Downloads/RAD")
library(reticulate)
library(anomalydots)
lbrary('methods')
l <- read.csv(file = "/home/bbtex/Downloads/metrics_list.csv", sep = ",")
n <- length(l$metrics)
for (i in 1:n){
tag=as.character(l$tags[i])
metric=as.character(l$metrics[i])
tag=eval(parse(text=paste("c(",tag,")",sep="")))
newdata=getTsOpenTSDBOptimized(metric=metric,hostname="opentsdb-read.intcs.meshcore.net",port=4242,downsample=10,endDate=as.character(Sys.Date()),agg=l$agg[i],tags=tag)
newdata = anomalydots::fillHoles2(newdata)
newdata = anomalydots::timeSeriePreparation(newdata)
timeseries[[i]] <- newdata
}
print ("Upload completed successfully")
When I run the following code with Rscript I have the following error:
Error in tsd_get_ascii(metric, interval, tags, agg, rate, downsample,
:
Calls: getTsOpenTSDBOptimized -> getTsOpenTSDB -> tsd_get -> tsd_get_ascii
In addition: Warning messages:
1: In tsd_get_ascii(metric, interval, tags, agg, rate, downsample, :
Response code 503
2: In tsd_get_ascii(metric, interval, tags, agg, rate, downsample, :
URL: http://opentsdb-read.intcs.meshcore.net:4242/q?start=2018%2F10%2F19-02%3A00%3A00&m=avg%3A600s-sum%3Adb.mysql.threads_connected%7Bhost%3Dbpct4005s%7D&end=2018%2F10%2F22-01%3A59%3A59&ascii=
Execution halted
When I follow the link in the error, I can see the data printed in the webpage.
Thank you in advance for your help

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

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