I am trying to create a table in R. I used mtcars as ax example dataset. I keep getting the error below. I am truly at a loss for how exactly to fix this and where to move certain files or programs to (ghostscript, magick, phantomjs..). Is there an easy fix? I am not using markdown.
kable(rawCSV, "latex", booktabs = T) %>%
kable_styling(latex_options = c("solid", "scale_down")) %>%
as_image()
save_kable(file="table.pdf")
Error in save_kable_latex(x, file, latex_header_includes, keep_tex, density) :
We hit an error when trying to use magick to read the generated PDF file. You may check your magick installation and try to use magick::image_read to read the PDF file manually. It's also possible that you didn't have ghostscript installed.
Related
Can't find generic `sew` in package knitr to register S3 method.
i This message is only shown to developers using devtools.
i Do you need to update knitr to the latest version?
I keep receiving the above error when I try to knit my rmarkdown file to html.
All I am doing is cleaning up my dataset with dplyr (select, mutate,etc.)
And generating cross tabs with the expss package
I am not using devtools. In fact, I didn't even have the package installed when the error appeared.
My code was knitting to html just fine. All I did was copy my code and change one variable (Race to Ethnicity), and then knit to HTML stopped working. It was working minutes before.
To fix this, I have tried:
knit to PDF (same error)
updating knitr and rmarkdown and expss
installing devtools and running library(devtools)
Closing out R and restarting my session
Clearing out my global environment and re-running everything
deleting the new code and running the original code that was working minutes before
None of these things worked! Any idea what could have happened with R markdown?
All I do is read in my excel sheet into "FullData"
And then I run this code
```{r, include = FALSE}
Racedata <- FullData %>%
select(Coder, Link, Chamber, SenateCommittee, SenateSubcommittee, HouseCommittee, HouseSubcommittee, Name, BillType, BillNumber, Race1, Race2) %>%
#Fix issues with Race columns
mutate(Race2 = na_if(Race2, "Other")) %>%
mutate(Race2 = na_if(Race2, "White"))
#Combine 2 Race columns into 1
Racedata$Race2 <- gsub("Unknown", "Audio Only", Racedata$Race2)
Newrace <- data.frame(Race = with(Racedata, replace(Race2, is.na(Race2), Race1[is.na(Race2)])))
Racedata$Race <- Newrace$Race
Racedata <- Racedata %>%
select(-c(Race1, Race2))
```
The code works just fine when I run it, but the error occurs when I try to knit to html.
This is the table that I want to produce with Knit to HTML:
```{r RACE CHARTS, echo = FALSE}
cross_cpct(Racedata, Race, list(total(), Chamber, HouseCommittee, SenateCommittee))
```
I figured it out! Stupid mistake
Two of my code blocks had the same name (facepalm)
Let's consider very simple table created by kable
library(knitr)
library(kableExtra)
x <- data.frame(1:3, 2:4, 3:5)
x <- kable(x, format = "pipe", col.names = c("X_1", "X_2", "X_3"), caption = "My_table")
I want to save this table into .pdf format
x %>% save_kable("My_table.pdf")
But I get error:
PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
However, when trying to install it by proposed command:
webshot::install_phantomjs()
I get error:
Error in utils::download.file(url, method = method, ...) :
cannot open URL 'https://github.com/wch/webshot/releases/download/v0.3.1/phantomjs-2.1.1-windows.zip'
So my question is - Is there any possbility to save kable table without using phanomjs?
The command works for me and the URL is also available.
I suspect that the file (it's a .zip file) is being blocked by your firewall or anti-virus software.
I just found out the awesome knitr library in R, when viewing the result in the viewer it seems nice. However, when I write this to a html file the style is lost.
Code
library(knitr)
library(kableExtra)
some.table <-
data.frame (
x = rep(1,3),
y = rep(1,3)
)
some.table
x <- kable(some.table, format = "html") %>%
kable_styling(bootstrap_options = "striped", full_width = F, position = "left")
x
file <- file('test.html')
write(x, file)
Table in viewer
Table in browser
How can I export the table with the same style to a html file?
Note that I have more data in the html file, so I should be able to append it.
Response to comment(s)
User: #Hao
When I use 'inspect element' in the Rstudio viewer, I can find this link to a stylesheet:
However the code herein seems to be huge as it is 582.298 characters.
The typical way of doing this is to put the code inside a rmarkdown document. It will handle everything for you.
The only case you need to use the save_kable function kableExtra is that you have lots of tables and you want to save them as fragments. In that case, you can use
library(kableExtra)
cars %>%
kable() %>%
kable_styling() %>%
save_kable()
I'm attempting to make my code more modular: data loading and cleaning in one script, analysis in another, etc. If I were using R scripts, this would be a simple matter of calling source on data_setup.R inside analysis.R, but I'd like to document the decisions I'm making in an Rmarkdown document for both data setup and analysis. So I'm trying to write some sort of source_rmd function that will allow me to source the code from data_setup.Rmd into analysis.Rmd.
What I've tried so far:
The answer to How to source R Markdown file like `source('myfile.r')`? doesn't work if there are any repeated chunk names (a problem since the chunk named setup has special behavior in Rstudio's notebook handling). How to combine two RMarkdown (.Rmd) files into a single output? wants to combine entire documents, not just the code from one, and also requires unique chunk names. I've tried using knit_expand as recommended in Generate Dynamic R Markdown Blocks, but I have to name chunks with variables in double curly-braces, and I'd really like a way to make this easy for my colaborators to use as well. And using knit_child as recommended in How to nest knit calls to fix duplicate chunk label errors? still gives me duplicate label errors.
After some further searching, I've found a solution. There is a package option in knitr that can be set to change the behavior for handling duplicate chunks, appending a number after their label rather than failing with an error. See https://github.com/yihui/knitr/issues/957.
To set this option, use options(knitr.duplicate.label = 'allow').
For the sake of completeness, the full code for the function I've written is
source_rmd <- function(file, local = FALSE, ...){
options(knitr.duplicate.label = 'allow')
tempR <- tempfile(tmpdir = ".", fileext = ".R")
on.exit(unlink(tempR))
knitr::purl(file, output=tempR, quiet = TRUE)
envir <- globalenv()
source(tempR, local = envir, ...)
}
I have a .Rnw file that I am able to compile into a PDF using the "Compile PDF" button in RStudio (or Command+Shift+k). However, when I use knit2pdf the graphics are not created and the complete PDF is not created. Why would this happen? How do you specifically set where the images will be stored so that pdflatex can find them?
Here is an example. I am aware that this question that I posted a few days ago has a similar example, but in my mind these are two different questions.
This file will run just fine and produce a PDF if I hit "Compile". I don't get any errors, the figure is produced in the /figure directory, and all is well.
%test.Rnw
\documentclass{article}
\usepackage[margin=.5in, landscape]{geometry}
\begin{document}
This is some test text!
<<setup, include=FALSE, results='hide', cache=FALSE>>=
opts_chunk$set(echo=FALSE, warning = FALSE, message = FALSE,
cache = FALSE, error = FALSE)
library(ggplot2)
#
<<printplotscreen, results='asis'>>=
ggplot(diamonds) +
geom_bar(aes(x = color, stat = "bin"))
#
\end{document}
However, when I run this script that is intended to do exactly the same thing as hitting "Compile" (is it?) the figure is not created and I get the not-surprising error below about not being able to find it.
#test.R
library("knitr")
knit2pdf(input = "~/Desktop/thing/test.Rnw",
output=paste0('~/Desktop/thing/test','.tex'))
Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, :
Running 'texi2dvi' on 'test.tex' failed.
LaTeX errors:
! LaTeX Error: File `figure/printplotscreen-1' not found.
NOTE: If you are trying to reproduce this (and thanks!) then make sure you run the knit2pdf script FIRST to see that it doesn't create the figures. If you hit "Compile" first then the figures will be there for knit2pdf to use, but it will not accurately represent the situation.
The solution: Make sure to set the working directory to the project directory before using knit2pdf, then shorten the "input" path to just the .Rnw file. Thus...
test.R
library("knitr")
diamonds = diamonds[diamonds$cut != "Very Good",]
setwd("/Users/me/Desktop/thing")
knit2pdf(input = "test.Rnw", output = "test.tex")
Here are some references on this issue:
Changing working directory will impact the location of output #38
;
make sure the output dir is correct (#38)
It seems that when using knit2pdf(), it automatically set your output files to the directory where your input file in. And the author doesn't recommend us changing work-directory during the middle of a project.
So the current solution for me is to save the working directory as old one(getwd()), change the working directory to where you want to save the output files, use knit2pdf() to output files, and change the working directory to the original one finally.