Using abbreviated code to specify a custom renderer using table1 in R - r

Hello I don't have many experience with R but I need help using abbreviated code to specify a custom renderer for the "table1" package in R. I would like to not display the default stats(FREQ,PCT) variable for categorical data and only display the frequency and omit the percent. The code in R Documentations show how to customize this for continuous variables and show some custom statistics for different variables.
This is just for the convenience of displaying a frequency table with the built html formatting of package 1. I do know how to get this information otherwise. I am interested learning how to use more this package in R.
rndr <- function(x, name, ...)
{(what <- switch(name,
c_race = "FREQ",
parse.abbrev.render.code(c("", what))(x))
}
table1::table1(~c_race|c_ethnicity*c_gender, data = childlearn_demo,
render=rndr, overall="Total",topclass="Rtable1-zebra")

The purpose of "abbreviated code" is to allow flexibility without needing to write your own function. Thus, you can get the desired result by simply using render.categorical="Freq", i.e.:
table1::table1(~ c_race|c_ethnicity*c_gender, data = childlearn_demo,
overall="Total",topclass="Rtable1-zebra", render.categorical="Freq")
(note: I am the table1 package author)

Related

In R, how do I save the dataframe which is one of the outputs from a function which uses print() and cat()?

I am using a summary() function from an R package. It prints out multiple results by using cat() and print() functions interweavingly (I checked its source code by getAnywhere()). It uses cat() to output descriptive messages, and uses print() to print out the dataframe and the matrix that I wanted to save. I am using R notebook, so I can see the cat() output and matrix output in the R Console tab, and the dataframe output by itself in another result tab. I wasn't able to save or retrieve the dataframe.
I tried sink() and capture.output() functions, but it only saved the descriptive messages and matrix together in a text file, and no dataframe saving. Does anyone know how to retrieve the dataframe object by itself? And I may also need to retrieve the matrix object by itself too.
The R package is called "lmmlasso". I guess it's not very well maintained. Here I am just giving an example code:
summary.foo= function(){
cat("random effects\n")
print(matrix(rnorm(9),3))
cat("fixed effects \n")
print(data.frame(X = c("A","B","C"), Estimate = rnorm(3)))
}
summary.foo()
I was not able to solve it directly, so I turned to the alternative way: I copied the original source code, return the dataframe and matrix objects at the end, and changed its function name. I got the dataframe by using the new summary function.
You should provide a reproducible code.
Anyway, try to assign it to a variable and then use the variable attributes, either with $ or #, to access the output you desire

Pulling Portfolio Data From Bloomberg using R Package "Rblpapiā€

I am trying to pull portfolio data from Bloomberg using command getPortfolio(My Portfolio,"Portfolio_Data"). Is it Possible to indicate reference date in the command?
When using excel it is possible to extract a portfolio at a certain point in time using BDS(My Portfolio,"Portfolio_Data","reference date = date") function. I wonder if this is possible in R.
BDS command returns NULL when requesting portfolio data in R. It seems like getPortfolio() should be the right solution, however I cannot figure out how to incorporate date into the function.
Figured out the correct syntax. The way to incorporate reference date into getPortfolio() function should look like the following:
overrides = c("REFERENCE_DATE" = '20181010')
getPortfolio("My Portfolio","Portfolio_Data", overrides = overrides)
"My Portfolio" should be portfolio ID from BBG's PRTU function with "client" appended (should look like this: "U1234567-8 Client")

R rvest connect with local host

I am creating a way to read in SPSS labels into R. Using library(sjPlot), view_spss(df, useViewer = FALSE) I can create a local html page such as http://localhost:11773/session/file1e0c67270a5.html that shows a nice table with columns for the variable names and the labels I am looking for.
Now I want to use rvest to scrape it but when I start with a command such as page <- rvest::html("http://localhost:11773/session/file1e0c67270a5.html") R just seems to get stuck.
I've tried searching for "connect with local host" but I can't seem to find any questions or answers related to the R package.
This doesn't really answer your specific question as I think the reason is that R spins up a non-persistent process to serve that HTML view of your data. But your approach seems quite round-a-bout to just get to variable labels. This is a general way that works quite well:
library(foreign)
d <- read.spss("your_data.sav", use.value.labels=TRUE, to.data.frame=FALSE)
var_labels <- attr(d, "variable.labels")
## To access the label of a variable named 'var_name':
var_labels[["var_name"]]
Where d results in a list of data, and var_labels is a named list of labels keyed by variable/column.
If you want to get variable and/or value label of SPSS-imported data, you can use get_val_labels and get_var_labels of the sjmisc-package.
See examples here. Both functions accept either a single variable (vector) or a data frame and return the associated variable and value labels. See also this blog post.
The sjmisc-Package supports data frames imported both with the haven- or foreign-package.

plyr rename function not working

I can't figure out why this version of plyr's rename function isn't working.
I have a dataframe where I have a single column that ends up being named seq(var_slcut_trucknumber_min, var_slcut_trucknumber_max) because I made it like this:
df_metbal_slcut <- as.data.frame(seq(var_slcut_trucknumber_min,var_slcut_trucknumber_max))
The terms var_slcut_trucknumber_min and var_slcut_trucknumber_max are defined as the min and max of another column.
However, when trying to rename it by the following code,
var_temp <- names(df_metbal_slcut)
df_metbal_slcut <- rename(df_metbal_slcut, c(var_temp="trucknumber"))
I get an error as follows:
The following `from` values were not present in `x`: var_temp
I don't understand why. I know that I can easily do this as colnames(df_metbal_slcut)[1] <- "trucknumber", but I'm an R n00b, and I was looking at a data manipulation tutorial that said that learning plyr was the way to go, so here I am stuck on this.
Try this instead:
df_metbal_slcut <- rename(df_metbal_slcut, setNames("trucknumber",var_temp))
The reason it wasn't working was that c(var_temp = "trucknumber") creates a named vector with the name var_temp, which is not what you were intending. When creating named objects using the tag = value syntax, R won't evaluate variables. It assumes that you literally want the name to be var_temp.
More broadly, it might make sense to name the column more sensibly when initially creating the data frame again using setNames.

Is it possible to to export from reporttools?

I am using tableNominal{reporttools} to produce frequency tables. The way I understand it, tableNominal() produces latex code which has to be copied and pasted onto a text file and then saved as .tex. But is it possible to simple export the table produced as can be done in print(xtable(table), file="path/outfile.tex"))?
You may be able to use either latex or latexTranslate from the "Hmisc" package for this purpose. If you have the necessary program infrastructure the output gets sent to your TeX engine. (You may be able to improve the level of our answers by adding specific examples.)
Looks like that function does not return a character vector, so you need to use a strategy to capture the output from cat(). Using the example in the help page:
capture.output( TN <- tableNominal(vars = vars, weights = weights, group = group,
cap = "Table of nominal variables.", lab = "tab: nominal") ,
file="outfile.tex")

Resources