Saving a complete dataframe to pdf in R - r

I'm looking to save a dataframe called chla.output (~100 rows, 2 columns) to a pdf in R/Rstudio. I've been using:
pdf(file = "output.pdf")
grid.table(chla.output)
dev.off()
Unfortunately when I open the pdf it only saves, say, the last 30 rows. I also get the "null device 1" message in the console. I'm sure there's an easy solution using RMarkdown, unfortunately I'm limited to only using base R or Rstudio because of the target audience for the package this is part of.
Thanks!

I don't have your data.frame so I'm using mtcars.
Using the height argument of pdf() you can specify the document height. You want to adjust this value based on the number of rows your data.frame has. Dividing the number of rows by 3 seems to create a value that works well.
pdf(file = "output.pdf", height=nrow(mtcars)/3)
gridExtra::grid.table(mtcars)
dev.off()

Related

Is there any method to extract pdf table tidy with R?

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

How can I increase the space between columns when creating a table in Rmarkdown?

I have made a table in Rmarkdown comparing information about models I have created. Code below:
mlist <- list(fitdm3hyp,fitdm3.1,fitdm3.2,fitdm3full,fitdm3.5,fitdm3.5b,fitdm3bi,fitdm3bio)
tablea <- compareLavaan(mlist,fitmeas = c("chisq","df","rmsea.robust","cfi.robust","srmr"),digits=4,type="html",chidif=FALSE)
However, the 1st and 2nd columns (chisq and df) end up so close that you can't tell where each value ends and the other begins. It ends up looking like: 343.44160.00 rather than 343.44 | 160.00.
How can I format this to increase the space between columns, please?
You can 'View()' function in dyplr to better view the dataset. make sure you are using a wider window view too. If you use the Tidyverse package, and make it a dataframe, the data should pad itself enough for better visuals.

Make and Visualise table in R

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)

Metafor measure argument error

I have calculated effect size and pooled SE in the way that I wanted. Only thing is drawing a forest plot and let metafor calculate the summary effect size. I have over 30 .csv data files to plot separately. When I do that with the following data (below), it plots and calculates summary effect smoothly.
DeltaPI Spooled
-75.35224985 7.618629848
-51.85221078 7.513461236
-37.77455275 7.164279414
The line I use is:
meta1<-rma(yi=mydata$DeltaPI, sei=mydata$Spooled)
forest(meta1,slab=paste(mydata$Study,mydata$Genotype..Experimental.),showweight=TRUE,alim=c(-100,25),at=c(-100,-50,0,25),xlab="Percentage Change of PI Score",cex=0.7,cex.lab=1,col="red")
However, when I try to do same thing with some other .csv files I have, rma gives an error and asks for 'measure' argument to plot the output. And since the measure is already DeltaPI i calculated manually, I don't want to use.
Weirdly, even if I change the data in those don't working files with the one that working properly(3 data rows above), it still gives the same error. Although, the same data works properly in some other .csv file.
So I'm not clear why I am getting the error and what is the solution.
Any comment would be appreciated!
My guess is that this has nothing to do with the plotting, but occurs when the rma() command is run. And it sounds to me that there are issues with how variables are named in the data that you are reading in. Now you are reading in data from .csv files, but this is probably what is happening:
> library(metafor)
> dat <- data.frame(DeltaP1 = c(.2,.4), Spooled=c(.1,.1))
> rma(dat$DeltaPI, sei=dat$Spooled)
Error in rma(dat$DeltaPi, sei = dat$s) :
Specify the desired outcome measure via the 'measure' argument.
So, in essence, you should carefully check the variable names.

CSV file to Histogram in R

I'm a total newbie with R, and I'm trying to create a histogram (with value and frequency as the axises) from a csv file (just one row of values). Any idea how I can do this?
I'm also an R newbie, and I ran into the same thing. I made two separate mistakes, actually, so I'll describe them both here.
Mistake 1: Passing a frequency table to hist(). Originally I was trying to pass a frequency table to hist() instead of passing in the raw data. One way to fix this is to use the rep() ("replicate") function to explode your frequency table back into a raw dataset, as described here:
Creating a histogram using aggregated data
Simple R (histogram) from counted csv file
Instead of that, though, I just decided to read in my original dataset instead of the frequency table.
Mistake 2: Wrong data type. My raw data CSV file contains two columns: hostname and bookings (idea is to count the number of bookings each host generated during some given time period). I read it into a table.
> tbl <- read.csv('bookingsdata.csv')
Then when I tried to generate a histogram off the second column, I did this:
> hist(tbl[2])
This gave me the "'x' must be numeric" error you mention in a comment. (It was trying to read the "bookings" column header in as a data value.)
This fixed it:
> hist(tbl$bookings)
You should really start to read some basic R manual...
CRAN offers a lot of them (look into the Manuals and Contributed sections)
In any case:
setwd("path/to/csv/file")
myvalues <- read.csv("filename.csv")
hist(myvalues, 100) # Example: 100 breaks, but you can specify them at will
See the manual pages for those functions for more help (accessible through ?read.table, ?read.csv and ?hist).
To plot the histogram, the values must be of numeric class i.e the data must be of numeric value. Here the value of x seems to be of some other class.
Run the following command and see:
sapply(myvalues[1,],class)

Resources