Let's assume I want to generate a template using R markdown. Let's also assume I have some R objects I want to paste in the Rmarkdown document.
For example, the R objects I have are:
dose = 10
units = "mg"
If I want to write a sentence like:
The dose administered was 10 mg every 3 days.
I can use:
```{r}
paste("The dose administered was",dose,units,"every 3 days.")
```
However, the output will be:
## [1] The dose administered was 10 mg every 3 days.
I know I can remove the "##" using comment=NA.
Is there any way to remove also "[1]"?
Is there any other and more efficient way to insert R objects with text using R markdown?
Thanks in advance,
Don't use an R chunk, use inline code
The dose administered was `r paste(dose, units)` every 3 days.
The function cat() makes the job :)
Related
I have data frames that have wide rows, either because they have a lot of columns of numerical values or because they have long text columns
I typically use RStudio and Rmd Notebooks. I can not figure out how to get the entire line printed without a truncation. I get the same behavior when I run the code in the RStudio console so I do not think it is an R markdown issue.
I also want to use capture.out() to save the formatted output from functions like anova. The capture.out() file is also truncated
Here is an example from the RStudio console
> fdf <- mcols(res.lfcShrink)
> fdf
DataFrame with 4 rows and 2 columns
type description
<character> <character>
baseMean intermediate mean of normalized c..
log2FoldChange results log2 fold change (MA..
lfcSE results posterior SD: diseas..
svalue results FSOS s-value (T=1): ..
> mcols(res.lfcShrink)$description
[1] "mean of normalized counts for all samples"
[2] "log2 fold change (MAP): diseaseState PDAC vs healthy"
[3] "posterior SD: diseaseState PDAC vs healthy"
[4] "FSOS s-value (T=1): diseaseState PDAC vs healthy"
I tried setting "limit length of lines displayed in console to" 500. Did not help
thanks in advance
Andy
I have a data frame which has one column of text with info that I need to extract, here is one observation from that column: each question has three attributes associated to it objectives,KeyResults and responsible
[{"text":"Newideas.","translationKey":"new.question-4","id":4,"objectives":"Great","KeyResults":"Awesome","responsible":"myself"},{"text":"customer focus.","translationKey":"new.question-5","id":5,"objectives":"Goalset","KeyResults":"Amazing","responsible":"myself"}
-------------------------DESIRED OUTPUT -----------------------
Question# Objectives KeyResults responsible Question# Objectives KeyResults responsible
4 Great Awesome myself 5 Goalset Amazin myself
Data is a valid json (but you need square bracket closing ] on it). You can read json into R object using json parser package (eg. jsonlite)
Let say your text is in column text of data frame df, then this will transform that text into R dataframe.
library(jsonlite)
dat <- fromJSON(df$text)
dat
# text translationKey id objectives KeyResults responsible
# 1 Newideas. new.question-4 4 Great Awesome myself
# 2 customer focus. new.question-5 5 Goalset Amazing myself
You need to install jsonlite to make it works
install.packages("jsonlite")
The CIA publishes a list of world leaders and cabinet ministers for all countries multiple times a year. This information is in PDF form.
I want to convert this PDF to CSV using R and then seperate and tidy the data.
I am getting the PDF from "https://www.cia.gov/library/publications/resources/world-leaders-1/"
under the link 'PDF Version for Prior Years' located at the center right hand side of the page.
Each PDF has some introductory pages and then lists the Leaders and Ministers for each country.
With each'Title' and 'Name' being seperated by a '..........' of varying lengths.
I have tried to use the pdftools package to convert from PDF, but I am not quite sure how to deal with the format of the data for sorting and tidying.
Here is the first steps I have taken with a downloaded PDF
library(pdftools)
text <- pdf_text("Data/April2006ChiefsDirectory.pdf")
test <- as.data.frame(text)
Starting with a single PDF, I want to list each Minister in a seperate row, with individual columns for year, country, title and name.
With the step I have taken so far, converting the PDF into .csv without any additional tidying, the data is in a single column and each row has a string of text contining title and name for multiple countries.
I am a novice at data tidying any help would be much appreciated.
You can do it with tabulizer but it is going to require some work to clean it up if your want to import all the 240 pages of the document.
Here I import page 4, that is the first with info regarding the leaders
library(tabulizer)
mw_table <- extract_tables(
"https://www.cia.gov/library/publications/resources/world-leaders-1/pdfs/2019/January2019ChiefsDirectory.pdf",
output = "data.frame",
pages = 4,
area = list(c(35.68168, 40.88842, 740.97853, 497.74737 )),
guess = FALSE
)
head(mw_table[[1]])
#> X Afghanistan
#> 1 Last Updated: 20 Dec 2017
#> 2 Pres. Ashraf GHANI
#> 3 CEO Abdullah ABDULLAH, Dr.
#> 4 First Vice Pres. Abdul Rashid DOSTAM
#> 5 Second Vice Pres. Sarwar DANESH
#> 6 First Deputy CEO Khyal Mohammad KHAN
You can use a vector of pages that you want to import as the argument in pages. Consider that you will have all the country names buried among the people names in the second column. Probably you can work out a method to identifying the indexes of the country by looking for the empty "" occurrences in the first column.
I am very new to RMarkdown and I am trying to generate separate pdf reports with a map from a data frame but the outputs are in a form of assessment questions and recommendations based on specific answers given. I have tried looking for some examples to help to no avail.
Here is a subset of the data frame I am using is as below
mill_name latitude longitude theme section question remark
<chr> <dbl> <dbl> <chr> <dbl> <chr> <chr>
1 Mill A 3.955042 102.7211 environment 1.1 Do you have issue 1 in your area? Refer to Guideline 1
2 Mill A 3.955042 102.7211 environment 1.2 Do you have issue 2 in your area? Refer to Guideline 2
3 Mill A 3.955042 102.7211 social 1.3 Do you have issue 3 in your area? Refer to Guideline 3
4 Mill A 3.955042 102.7211 social 1.4 Do you have issue 4 in your area? Refer to Guideline 4
5 Mill B 1.961030 103.4140 environment 1.1 Do you have issue 1 in your area? Refer to Guideline 1
6 Mill B 1.961030 103.4140 environment 1.2 Do you have issue 2 in your area? Refer to Guideline 2
7 Mill B 1.961030 103.4140 social 1.3 Do you have issue 3 in your area? Refer to Guideline 3
8 Mill B 1.961030 103.4140 social 1.4 Do you have issue 4 in your area? Refer to Guideline 4
My rmarkdown code I managed to produce so far from my own research is shown below
---
title: "Summary Report"
author: "Authors Name"
date: "September 19, 2015"
output: pdf_document
---
# Summary
This is a summary report for your mill
```{r, comment=NA, echo=FALSE}
# A table with data received from R script
# create reports on students from an Excel spreadsheet.
library(readxl)
library(knitr)
setwd("C:/Users/Jason/Mill Summary Report")
# read in the excel file
data <- read_excel("dummy_pat_results.xlsx")
for (i in 1:nrow(data)) {
cat("Section:", data$section[i], "\n")
cat("Question: ",data$question[i], "\n")
cat("Remark: ",data$remark[i], "\n")
cat("\n")
}
```
and the R script I am using is shown below
library("rmarkdown")
for (i in unique(data$mill_name)){
subgroup <- data[data$mill_name == i,]
rmarkdown::render("Test_Markdown_v2.Rmd",
output_file=paste0(i, ".pdf"))
}
This is what I am getting right now for the 2 pdf's I created which for a start isn't splitting them down to each Mill's report (Mill A and Mill B)
Ideally, what I would like to get is something like this
I have seen a number of examples of creating separate reports for plots with different sets of data but nothing quite like a report like the one I require. Any help with this is much appreciated. Thanks in advance.
I think you need Parameterized Rmarkdown document. Following links takes you to a very simple tutorial. I hope this will solve your problem.
http://rmarkdown.rstudio.com/developer_parameterized_reports.html
I am new to R and my question should be trivial. I need to create a word cloud from a txt file containing the words and their occurrence number. For that purposes I am using the snippets package.
As it can be seen at the bottom of the link, first I have to create a vector (is that right that words is a vector?) like bellow.
> words <- c(apple=10, pie=14, orange=5, fruit=4)
My problem is to do the same thing but create the vector from a file which would contain words and their occurrence number. I would be very happy if you could give me some hints.
Moreover, to understand the format of the file to be inserted I write the vector words to a file.
> write(words, file="words.txt")
However, the file words.txt contains only the values but not the names(apple, pie etc.).
$ cat words.txt
10 14 5 4
Thanks.
words is a named vector, the distinction is important in the context of the cloud() function if I read the help correctly.
Write the data out correctly to a file:
write.table(words, file = "words.txt")
Create your word occurrence file like the txt file created. When you read it back in to R, you need to do a little manipulation:
> newWords <- read.table("words.txt", header = TRUE)
> newWords
x
apple 10
pie 14
orange 5
fruit 4
> words <- newWords[,1]
> names(words) <- rownames(newWords)
> words
apple pie orange fruit
10 14 5 4
What we are doing here is reading the file into newWords, the subsetting it to take the one and only column (variable), which we store in words. The last step is to take the row names from the file read in and apply them as the "names" on the words vector. We do the last step using the names() function.
Yes, 'vector' is the proper term.
EDIT:
A better method than write.table would be to use save() and load():
save(words. file="svwrd.rda")
load(file="svwrd.rda")
The save/load combo preserved all the structure rather than doing coercion. The write.table followed by names()<- is kind of a hassle as you can see in both Gavin's answer here and my answer on rhelp.
Initial answer:
Suggest you use as.data.frame to coerce to a dataframe an then write.table() to write to a file.
write.table(as.data.frame(words), file="savew.txt")
saved <- read.table(file="savew.txt")
saved
words
apple 10
pie 14
orange 5
fruit 4