I’ve copied codes into parent and child file and located the child as C:/temp/child.Rmd
The statements in parent program
---
title: "parent"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
library(ggplot2)
```
```{r, parent, include = FALSE}
src <- lapply(
sort(unique(iris$Species)),
FUN = function(x) {
knitr::knit_expand(
file = here::here("C:/temp/child.Rmd"),
title = x
)
}
)
```
`r knitr::knit(text = unlist(src))`
The statements in child program
---
title: "child1"
output: pdf_document
---
## {{title}}
```{r plot_{{title}}}
iris %>%
filter(Species == "{{title}}") %>%
ggplot() +
aes(x = Sepal.Length, y = Sepal.Width) +
geom_point() +
labs(title = "{{title}}")
```
When I knit the parent program, the error message as below. Why and how to fix it?
Quitting from lines 14-23 (trypc.Rmd)
Error in file(con, "r") : cannot open the connection
Calls: <Anonymous> ... one_string -> paste -> read_utf8 -> readLines -> file
Execution halted
Thanks
Related
I am trying to knit a document using plotly to generate the images and rmarkdown. Below is a basic example:
---
title: "Test"
author: "Me"
date: "`r Sys.Date()`"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(plotly)
library(webshot)
library(webshot2)
```
## R Markdown
This is an R Markdown document. It is a test.
```{r test, echo=FALSE}
x <- seq(-1, 1, 0.1)
y <- dnorm(x, 0, 0.5)
pl1 <- plot_ly(x=x, y=y, type='scatter', mode='lines') %>%
config(mathjax='cdn') %>%
plotly::layout(
showlegend=TRUE,
legend=list(itemsizing='trace', orientation='h',
xanchor='center', x=0.5), margin=list(b=80, l=45, r=30, t=80),
title=list(text=
TeX("M \\text{ updates for } N=n=1 \\text{ for } P(update) = \\gamma_{i}.")))
# pl1
htmlwidgets::saveWidget(widget = pl1, file = "pl1.html")
webshot(url = "pl1.html", file = "pl1.pdf")
The problem is that when I knit the document, the htmlwidgets function generates the correct rendering of Latex (top image), but the webshot function does not (bottom image). Also, I have tried using fig.show='hold' but this doesn't help either.
I have also tried using webshot2 and, when using webshot I have installed webshot::install_phantomjs()
I'm trying to create a dynamic number of tabs in my rmd with some content inside.
This one doesn't help.
Something like this:
---
title: "1"
output: html_document
---
```{r }
library(highcharter)
library(tidyverse)
iris %>%
dplyr::group_split(Species) %>%
purrr::map(.,~{
# create tabset for each group
..1 %>%
hchart("scatter", hcaes(x = Sepal.Length, y = Sepal.Width))
})
```
You can set results = 'asis' knitr option to generate the tabs in the map function using cat.
Getting Highcharter to work with asis was trickier :
Highchart needs to be called once before the asis chunck, probably to initialize properly, hence the first empty chart.
to print the chart in the asis chunck, the HTML output is sent in character format to cat
Try this:
---
title: "Test tabs"
output: html_document
---
`r knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE, cache = F)`
```{r}
library(highcharter)
library(tidyverse)
# This empty chart is necessary to initialize Highcharter in the tabs
highchart(height = 1)
```
```{r, results = 'asis'}
cat('## Tabs panel {.tabset} \n')
invisible(
iris %>%
dplyr::group_split(Species) %>%
purrr::imap(.,~{
# create tabset for each group
cat('### Tab',.y,' \n')
cat('\n')
p <- hchart(.x,"scatter", hcaes(x = Sepal.Length, y = Sepal.Width))
cat(as.character(htmltools::tagList(p)))
})
)
```
Note that while this solution works well, it goes beyond the original use for asis
I have the following R Markdown document. I am trying to render regression output tables directly into pdf, but the tables don't show up. I have tried type = 'latex' instead of type = 'html', but no difference.
I know that I can copy-paste the latex code from the console, but that is really cumbersome. Any other solution to this?
```
---
title: "title"
output:
pdf_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(stargazer)
library(plm)
```
```{r data, echo=FALSE, include=FALSE}
library(haven)
dat <- read_dta("E:/CZ/CZ_fin_distress/CZ_all_2015_2018_small_final.dta")
library(dplyr)
dat <- dat %>%
rename(
log_avgprice = lamt,
log_localprice = lsamt,
negNPM = cat2,
lagged_negNPM = lcat
)
dat <- dat[dat$ptype<3,]
```
```{r prices, echo=FALSE, include=FALSE, results="asis"}
ma <- plm(log_avgprice ~ negNPM, index=c("id", "year"), model="within", data=dat)
mb <- plm(log_localprice ~ negNPM, index=c("id", "year"), model="within" , data=dat)
mc <- plm(log_avgprice ~ lagged_negNPM, index=c("id", "year"), model="within", data=dat)
md <- plm(log_localprice ~ lagged_negNPM, index=c("id", "year"), model="within", data=dat)
stargazer(ma, mb, mc, md, title = "Logged claim amount", type = 'html')
```
I found my mistake: include=FALSE and results="asis" cancel each other out. It works now after eliminating include=FALSE.
I am trying to create .pdf and .doc to generate report from the following code. there are two problems in total:
with .pdf output
R is working out but once i knit with pdf then it run whole code and generate .pdf file with 0 kb. I am not able to open this file.
.doc output
In .doc file i want to change layout of pages and want to use pagebreak but its not working out.
I also want to write code for page layout(landscape or portrait), but its not working. As i can only create the portrait page but table which i created with flextable is not autofitting in page.
I installed MIKTEX but then table and graph is floating in documents. so i uninstalled it.
I dont understand what is "cairo out of memory" in error.
---
title: "Bla bla bla"
author: "Kishan"
date: "`r format(Sys.time(), '%d.%m.%Y')`"
output:
pdf_document: default
word_document: default
html_document: default
---
# Global option --------
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, messAgegr = FALSE,
comment=FALSE, warning=FALSE,
results = "asis", dpi=600,
fig.width= 15, fig.height=8 ,
dev = "cairo_pdf"
)
options(kableExtra.latex.load_packages=TRUE,
kableExtra.auto_format=FALSE)
library(tidyr)
library(dplyr)
library(janitor)
library(ggplot2)
library(readxl)
library(knitr)
library(kableExtra)
library(tinytex)
library(flextable)
options(tinytex.verbose = TRUE)
ggpt <- theme_bw() +
theme(#text=element_text(size=rel(1.7)),
axis.title.x=element_text(size=rel(1.5)),
axis.title.y=element_text(size=rel(1.5)),
axis.text.x=element_text(size=rel (1.5)),
axis.text.y=element_text(size=rel (1.5)),
strip.text.x=element_text(size=rel(1.2)),
strip.text.y=element_text(size=rel(1.2)),
legend.text=element_text(size=rel (1.3)),
legend.title=element_text(size=rel(1.3)),
plot.title=element_text(size=rel (1.5)))
doc_type <- knitr::opts_knit$get("rmarkdown.pandoc.to")
if(is.null(doc_type)) {
doc_type <- "raw"
}
```
```{r}
stdtabl <- function(data, caption=NULL, format="raw") {
if(format %in% c("latex", "html")) {
data %>%
knitr::kable(booktabs=TRUE,
format=format,
digits=0,
caption=caption) %>%
kable_styling(bootstrap_options=c("striped", "hover", "condensed", "responsive"),
full_width=FALSE,
font_size=12,
fixed_thead=TRUE,
position="center") %>%
column_spec(column=1,
bold=TRUE)
} else if(format == "docx") {
## use flextable as kableExtra does not handle Word documents
data %>%
flextable() %>%
theme_zebra() %>%
autofit() %>%
font(fontname="Calibri")
} else { "raw"
data %>%
knitr::kable()
}
}
```
```{r}
mobitab <- EQBase %>%
tabyl(Zeitpunkt, eq5d5lMobil) %>%
adorn_percentages("row") %>%
adorn_pct_formatting(digits = 0 ) %>%
adorn_ns(position = "front") %>%
select(Zeitpunkt,'mob_1','mob_2','mob_3','mob_4','mob_5','V1')
stdtabl(mobitab, caption= 'sdsdyfwerfew', format=doc_type)
```
```{r}
barplotmob <- EQBase %>%
mutate(eq5d5lMobil= factor(eq5d5lMobil, levels = c("","mob_5", "mob_4", "mob_3", "mob_2", "mob_1"))) %>%
group_by(VisitID, eq5d5lMobil) %>%
summarise(Count= n()) %>%
mutate(Percentage = Count/sum(Count)*100, round(Percentage, digits=0)) %>%
mutate(lab_ypos = 100-(cumsum(Percentage) - 0.5 * Percentage))
ggplot(barplotmob, aes(x=factor(VisitID), y=Percentage, fill = factor(eq5d5lMobil))) +
geom_bar(stat="identity", width = 0.7)+
geom_text (aes(y = lab_ypos, label = paste0(round(Percentage, digits=0)), group =eq5d5lMobil),
color = "black", size = 3)
```
>Quitting from lines 238-274 (EQ5D_1.rmd)
Fehler in (function (filename = if (onefile) "Rplots.pdf" else "Rplot%03d.pdf", :
unable to start device 'cairo_pdf'
Ruft auf: <Anonymous> ... in_base_dir -> in_dir -> plot2dev -> do.call -> <Anonymous>
Zusätzlich: Warnmeldungen:
1: Calling `as_tibble()` on a vector is discouraged, because the behavior is likely to change in the future. Use `enframe(name = NULL)` instead.
This warning is displayed once per session.
2: In (function (filename = if (onefile) "Rplots.pdf" else "Rplot%03d.pdf", :
cairo error 'out of memory'
Ausführung angehalten
I want to display two charts with the rCharts package, one next to the other, more or less like the two pies are displayed in this link:
http://nvd3.org/examples/pie.html
I have a partial solution using <iframe>, but the solution has three problems:
It is too case specific
Including controls becomes a complicated task
It does not look too nice
Minimum working example:
---
title: "Example"
output: html_document
---
```{r rcht, message=FALSE, echo=FALSE, results='asis'}
library(rCharts)
df<-data.frame(label=c("One","Two","Three"),valuea=c(1,2,3),othera=c(10,11,12),
valueb=c(4,5,6),otherb=c(10,11,12),stringsAsFactors = FALSE)
p1 <- nPlot(valuea~ label, data = df, type = 'pieChart',height = 225, width = 300)
p2<- nPlot(valueb~ label, data = df, type = 'pieChart',height = 225, width = 300)
p1$show('inline', include_assets = TRUE, cdn = F)
p2$show('inline', include_assets = TRUE, cdn = F)
```
```{r message=FALSE, echo=FALSE}
p1$save("pie1.html", standalone = TRUE)
p2$save("pie2.html", standalone = TRUE)
```
<div align="center">
<font size="10" color="black" face="sans-serif">Both Pies</font><br>
<p>
<iframe src="pie1.html" height="400" width="400"></iframe>
<iframe src="pie2.html" height="400" width="400"></iframe>
</p>
<div>
I know pie charts should not be used and that I could use a multi-bar chart. However, I want to use this type of layout with other kinds of charts in the rCharts package.
Additionally, I would like to include controls in the charts whilst they are shown next to each other. Including the following code before the $save() function adds the controls:
```{r message=FALSE, echo=FALSE}
p1$addControls('y','valuea',values=c('valuea','othera'))
p2$addControls('y','valueb',values=c('valueb','otherb'))
```
This issue is less relevant to me, but if someone has a solution (preferably with only one control for both charts), it would be great.
I understand all this might be too much to handle from R. Any help/advice is appreciated.
Not elegant, but functional (I did not try it with controls):
---
title: "Example"
output: html_document
---
```{r rcht, message=FALSE, echo=FALSE, results='asis'}
library(rCharts)
library(htmltools)
df <- data.frame(label=c("One","Two","Three"),valuea=c(1,2,3),othera=c(10,11,12),
valueb=c(4,5,6),otherb=c(10,11,12),stringsAsFactors = FALSE)
p1 <- nPlot(valuea~ label, data = df, type = 'pieChart',height = 225, width = 300)
p2 <- nPlot(valueb~ label, data = df, type = 'pieChart',height = 225, width = 300)
```
```{r echo=FALSE, results="asis"}
cat("<table width='100%'><tr style='width:100%'><td width='50%'>")
```
```{r echo=FALSE, results="asis"}
p1$show('inline', include_assets = TRUE, cdn = FALSE)
```
```{r echo=FALSE, results="asis"}
cat("</td><td>")
```
```{r echo=FALSE, results="asis"}
p2$show('inline', include_assets = TRUE, cdn = FALSE)
```
```{r echo=FALSE, results="asis"}
cat("</td></tr></table>")
```
Hi I am having the same problem with controls it looks that in the viewer of R-studio everything works fine but not when I compile with Rmarkdown it doesn't show the plot at all.
```{r results = 'asis', comment = NA}
require(rCharts)
require(datasets)
p2 <- nPlot(mpg ~ cyl, group = 'wt',
data = mtcars, type = 'scatterChart')
p2$xAxis(axisLabel = 'Log2')
p2$yAxis(axisLabel = 'Log2')
p2$chart(tooltipContent = "#! function(key, x, y, e){
return '<b>Name:</b> ' + e.point.GeneID
} !#")
p2$chart(color = c('red', 'green'))
p2$addControls("x", value = 'mpg', values = names(mtcars))
p2$addControls("y", value = 'cyl', values = names(mtcars))
cat('<style>.nvd3{height: 400px;}</style>')
p2$print('chart2', include_assets = TRUE)
```
The code above is the addControls are removed actually works also in the rmarkdown.
Also, if you try to run the code above in Rstudio console (just from p2<-nPlot to cat command) and then calling p2 I can actually see the controls.