I'd like to do a contingency table between sex and disease. As I use R.markdown for pdf. reports, I use kableExtra to customize the tables. KableExtra doesn't make the tables well when they are not data.frame. So they make ugly table with tableby
With this data.frame here is what I got.
library(kableExtra)
library(arsenal)
set.seed(0)
Disease<-sample(c(rep("Name of the first category of the disease",20),
rep("Name of the Second category of the disease",32),
rep("Name of the third category of the disease",48),
rep("The category of those who do not belong to the first three categories",13)))
ID<-c(1:length(Disease))
Gender<-rbinom(length(Disease),1,0.55)
Gender<-factor(Gender,levels = c(0,1),labels = c("F","M"))
data<-data.frame(ID,Gender,Disease)
When I run the result of this analysis with R.markdown (pdf) I get this kind of table
There are two problems, thirsly KableExtra:: doesn't deal with the characters
Secondly I can't customize columns width when I use tableby with kableExtra, cause I would like to enlarge the column containing variable names, since I am really working with data where the names of the variable values are very long. But if I use kable of knitr::, the characters are removed, but the tables are not scale down, and a part is not displayed. I think knitr has many limitations.
How can I deal with this problem? Or is there another function which could be used in R.markdown (pdf format) to make beautiful contingency table with p.value.
To avoid the being added to your output when using knitr, use results='asin' in the chunk set up options, so like:
{r results='asis'}
You can control the width of the column with your variable names in them by using the width option in the print function. You technically do not need to wrap the summary call in print, but it doesn't change anything if you do, and you are able to adjust the width setting.
So, for your example:
print(summary(tableby(Gender~Disease)), width = 20)
should make it more readable when it renders to pdf. You can change the width and it will wrap at the limit you set.
Using the code from your example and the above function call, the table looks like this when knit to pdf:
Related
I'm using the excellent modelsummary package to summarize my data and regression models.
One problem I've run into is having column labels -- variables for datasummary_correlation and model names for regression output -- that are too wide for the PDF page size I want. It looks fine in Rstudio and HTML, but that's just because there's no width issue there. In PDF, an overly wide table runs into the margin.
Here's an example where you can see how the column labels are too wide.
What I would like is to break up the column labels at the place of my choosing, so that they would consist of multiple rows (and narrower columns). In fact, what it looks like more on the HTML rendering:
Looks like the correct strategy (h/t the author of modelsummary) is to pipe the output into one of the table helper functions, then to use the column_spec() function.
So for me, I can do (making the data columns shorter and the label column a little longer):
out <- datasummary_correlation(output="kableExtra", booktabs=T)
out %>% column_spec(1,width="1.5in") %>% column_spec(2:5,width="0.75in")
I am trying to print a series of named numbers as part of an R Markdown html document (specifically, the fitted values and residuals of an lm regression. The list of numbers is very long, so I have the document set to paged tables using the option df_print: paged. However, I've noticed that there is a lot of unused blank space because there is only one column in addition to row names. I would like to try and set the table such that the displayed table wraps around and there are two or three series of columns displayed at once, sort of like what happens if I try to print a named number and there are two columns of named numbers displayed.
Here is an example that produces results similar to what I am trying to avoid using the mtcars dataset.
data(mtcars)
fit<-lm(mpg~cyl,data=mtcars)
data.frame(fit$residuals)
fit$residuals
Printing fit$residuals gets me closer to what I want in terms of space optimization, but it prints every single row and I cannot paginate the data (or at least, I do not know if it can be done).
For two columns:
data(mtcars)
fit<-lm(mpg~cyl,data=mtcars)
df=data.frame(fit$residuals)
df1=cbind(df[1:16,], df[17:32,])
library(htmlTable)
htmlTable(df1, cgroup=c("Residuals 1:16", "Residuals 17:32"), n.cgroup=c(1,1), rnames=FALSE)
For three columns:
data(mtcars)
fit<-lm(mpg~cyl,data=mtcars)
df=data.frame(fit$residuals)
df1=cbind(df[1:11,], df[12:22,], df[23:33,])
htmlTable(df1, cgroup=c("Residuals 1:11", "Residuals 12:22", "Residuals 23:32"), n.cgroup=c(1,1,1), rnames=FALSE)
I need an automatic code to extract pdf table in R.
So I searched website, find tabulizer package.
and I use
extract_tables(f2,pages = 25,guess=TRUE,encoding = 'UTF-8',method="stream")#f2 is pdf file name
I tried every method type, but the outcome is not tidy.
Some columns are mixed and there is a lot of blank as you can see image file.
I think I would do modify the data directly. But the purpose is automizing it. So general method is needed. And every pdf file is not organized. Some table is very tidy with every related line matched perfectly but others are not..
As you can see in my outcome image, in column 4, the number is mixed in same column. Other columns, the number is matched one by one what I mean is I want to make column tidy like table in pdf automatically.
Is there any package or some method to make extracted table tidy?
my Code result
table in PDF
First I have an input and I want to get an output like this
(I want to group the occurrences in the dataset between columns):
Second:
Display this table in a good looking way (something that looks like Word or Excel)
I can't use Word or Excel as I'm making some calculations in R with this dataset (it contains columns with numbers which aren't displayed here)
calculating the output table
I don't really see how the output table is calculated. Shouldn't it be output_table["A", "C"] = "e"?
Displaying your data
There are a lot of ways to do that. You might consider using RMarkdown to create a report-style output.
The DT library is also a very handy tool to display tables. It works well with RMarkdown and can be embedded in HTML documents. If you are using RStudio, you can use the following code to display your data
library(DT)
DT::datatable(iris)
How do I get apsrtable table to generate a multi-column table from an R data.frame that spans two columns, i.e.
\begin{table*}
...
\end{table*}
instead of
\begin{table}
...
\end{table}
As it is, I have to manually edit that part every time I regenerate the Rnw file, otherwise my wide tables don't fit inside a single column (they overlap with text).
I didn't find any such option in the package's help page.
Use the float argument to manually specify the environment:
apsrtable(..., float='table*')