Skip to content

Commit

Permalink
Commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
FerreiraAS committed Feb 11, 2025
1 parent 8db6ecc commit c8be412
Show file tree
Hide file tree
Showing 123 changed files with 7,043 additions and 4,468 deletions.
Binary file modified .DS_Store
Binary file not shown.
512 changes: 0 additions & 512 deletions .Rhistory (1)

This file was deleted.

Binary file modified Ciencia-com-R.docx
Binary file not shown.
3,154 changes: 2,046 additions & 1,108 deletions Ciencia-com-R.log

Large diffs are not rendered by default.

Binary file modified Ciencia-com-R_files/figure-html/evento-dado-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions R/parseRmd.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
ParseRmdContent <- function(rmd_files, bib, k) {
# parse Rmd files to get questions, answers, section and chapter
rmd_rawdata <- parsermd::parse_rmd(rmd_files[k], allow_incomplete = FALSE, parse_yaml = FALSE) |>
tibble::as_tibble()

# clean R chunks and headings
rmd_data <- rmd_rawdata[rmd_rawdata$type != "rmd_chunk", ]
rmd_data <- rmd_data[rmd_data$type != "rmd_heading", ]

# keep rows with complete headers 1 to 3
rmd_data <- rmd_data[complete.cases(dplyr::select(rmd_data, c("sec_h1", "sec_h2",
"sec_h3"))), ]

# loop over all answers
for (i in 1:nrow(rmd_data)) {
chapter <- rmd_data[i, ]$sec_h1
# remove ** from chapter
chapter <- gsub("\\*\\*", "", chapter)
# remove {-} from chapter
chapter <- gsub("\\{-\\}", "", chapter)

section <- rmd_data[i, ]$sec_h2
# remove {-} from section
section <- gsub("\\{-\\}", "", section)

question <- rmd_data[i, ]$sec_h3

answers <- rmd_data[i, ] |>
parsermd::as_document()
# keep only rows starting with '- '
answers <- answers[grep("^- ", answers)]
# remove - from answers
answers <- gsub("^- ", "", answers)
# replace $$ by $ from LaTeX equations
answers <- gsub("\\$\\$", "$", answers)
# replacE $ in begining of any word string by $\\
answers <- gsub("\\$([a-zA-Z])", "$\\\\\\1", answers)
# replace $\\ by $ from answers
answers <- gsub("\\$\\\\", "$", answers)

# get content within brackets starting with @ [@]
key <- gsub(".*\\[@(.*?)\\].*", "\\1", answers)

# get source from answers [@key]
answers <- trimws(gsub("\\[@(.*?)\\]", "", answers), which = "left")

entry <- data.frame(chapter = rep(chapter, length(answers)), section = rep(section,
length(answers)), question = rep(question, length(answers)), answer = answers,
key = key)

if (nrow(entry) != 0) {
for (j in 1:nrow(entry)) {
# save post to PNG
SavePost(entry[j, ], bib)
}
}
}
}
108 changes: 108 additions & 0 deletions R/savePost.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
SavePost <- function(entry, bib) {
chapter <- entry$chapter
section <- entry$section
question <- entry$question
answer <- entry$answer
key <- entry$key

# detect if there is a r`` code block
if (grepl("`r", answer)) {
# extract code block
code <- gsub(".*`r", "", answer)
code <- gsub("`.*", "", code)
# evaluate code
code.eval <- eval(parse(text = code))
# replace code block with result
answer <- gsub("`r.*`", code.eval, answer)
}

# double $ to do not convert to math
answer <- gsub("\\$", "$$", answer)
# convert markdown to HTML text
answer <- markdown::markdownToHTML(text = answer, fragment.only = TRUE)
# convert back $$ to $
answer <- gsub("\\$\\$", "$", answer)
# remove HTML <p> tags
answer <- gsub("<p>", "", answer)
answer <- gsub("</p>", "", answer)
# remove \n t the end
answer <- gsub("\n", "", answer)

# function to convert italic and bold to tex
emphasis <- function(text) {
text <- gsub("<em>", "\\\textit{", text)
text <- gsub("</em>", "}", text)
text <- gsub("<strong>", "\\\textbf{", text)
text <- gsub("</strong>", "}", text)
return(text)
}

question <- emphasis(question)
answer <- emphasis(answer)

source <- bib[key]
citation <- format(source, style = "text")
citation <- gsub("\\[1\\]", "", citation)
# remove underline character
citation <- gsub("_", "", citation)
# remove content within < >.
citation <- gsub("<.*?>.", "", citation)
# merge citations
citation <- paste0(citation, collapse = "\\")

# Ensure chapter folder exists
chapter_folder <- file.path(getwd(), "posts", chapter)
dir.create(chapter_folder, showWarnings = FALSE, recursive = TRUE)

# Ensure section folder exists
section_folder <- file.path(chapter_folder, section)
dir.create(section_folder, showWarnings = FALSE, recursive = TRUE)

# File name based on question
file_name <- paste0(gsub(" ", "_", question), ".png")
# add a number to file name
file_name <- paste0(as.character(length(list.files(section_folder, pattern = ".png"))),
"_", file_name)
file_path <- file.path(section_folder, file_name)

# read tex file
tex <- readLines(file.path("tex", "POST.tex"))

# replace CAPITULO with chapter
tex <- gsub("CAPITULO", chapter, tex)
# replace SECAO with section
tex <- gsub("SECAO", section, tex)
# replace QUESTAO with question
tex <- gsub("QUESTAO", question, tex)
# replace RESPOSTA with answer
tex <- gsub("RESPOSTA", answer, tex)
# replace CITACAO with citation
tex <- gsub("CITACAO", citation, tex)
# escape & character
tex <- gsub("\\&", "\\\\&", tex)
# escape % character
tex <- gsub("\\%", "\\\\%", tex)

# compile tex
writeLines(tex, file.path("posts", "POST.tex"))

# render text file to pdf
tools::texi2pdf(file.path("posts", "POST.tex"), clean = TRUE)

# move pdf to posts
file.rename(from = file.path("POST.pdf"), to = file.path("posts", "POST.pdf"))

# render PDF to PNG file
bitmap <- pdftools::pdf_render_page(file.path("posts", "POST.pdf"), dpi = 300)
png::writePNG(bitmap, file_path)

img <- magick::image_read(file_path)
cover <- magick::image_read(file.path(getwd(), "images", "Cover_1.png"))
cover <- magick::image_scale(cover, "400x400") # Ajuste o tamanho conforme necessário
img <- magick::image_composite(img, cover, offset = "+1200+0") # Ajuste a posição conforme necessário
magick::image_write(img, path = file_path, format = "png", quality = 100, density = 300)

# delete pdf and tex files
file.remove(file.path("posts", "POST.pdf"))
file.remove(file.path("posts", "POST.tex"))
}
2 changes: 1 addition & 1 deletion _bookdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ rmd_files:
- "rmd/analise-descritiva.Rmd"
- "rmd/analise-causal.Rmd"
- "rmd/analise-inferencial.Rmd"
- "rmd/efeito-pvalor.Rmd"
- "rmd/tamanhoefeito-pvalor.Rmd"
- "rmd/parte-3.Rmd"
- "rmd/selecao-testes.Rmd"
- "rmd/testes-estatisticos.Rmd"
Expand Down
55 changes: 55 additions & 0 deletions bib/references.bib
Original file line number Diff line number Diff line change
Expand Up @@ -4501,3 +4501,58 @@ @article{grDevices
url = {https://www.R-project.org/}
}


@article{gelman2014,
title = {Beyond Power Calculations},
author = {Gelman, Andrew and Carlin, John},
year = {2014},
month = {11},
date = {2014-11},
journal = {Perspectives on Psychological Science},
pages = {641--651},
volume = {9},
number = {6},
doi = {10.1177/1745691614551642},
url = {http://dx.doi.org/10.1177/1745691614551642},
langid = {en}
}

@article{lu2018,
title = {A note on Type S/M errors in hypothesis testing},
author = {Lu, Jiannan and Qiu, Yixuan and Deng, Alex},
year = {2018},
month = {03},
date = {2018-03-23},
journal = {British Journal of Mathematical and Statistical Psychology},
pages = {1--17},
volume = {72},
number = {1},
doi = {10.1111/bmsp.12132},
url = {http://dx.doi.org/10.1111/bmsp.12132},
langid = {en}
}

@article{correctR,
title = {correctR: Corrected Test Statistics for Comparing Machine Learning Models
on Correlated Samples},
author = {Henderson, Trent},
year = {2025},
date = {2025},
url = {https://CRAN.R-project.org/package=correctR}
}

@misc{CRAN,
author = {, R Core Team},
title = {The Comprehensive R Archive Network},
url = {https://cran.r-project.org},
year = {2021},
organization = {cran.r-project.org}
}

@misc{R_Packages,
title = {All R CRAN packages [Full List]},
url = {https://r-packages.io/packages},
urldate = {2025-02-11},
year = {2025},
organization = {R PACKAGES}
}
Binary file modified covers/Cover_1.pdf
Binary file not shown.
Binary file modified covers/Cover_2.pdf
Binary file not shown.
Binary file modified covers/Cover_4.pdf
Binary file not shown.
Loading

0 comments on commit c8be412

Please sign in to comment.