R/functions1.R

Defines functions myhux myfun hello

Documented in hello

# Hello, world!
#
# This is an example function named 'hello'
# which prints 'Hello, world!'.
#
# You can learn more about package authoring with RStudio at:
#
#   http://r-pkgs.had.co.nz/
#
# Some useful keyboard shortcuts for package authoring:
#
#   Install Package:           'Ctrl + Shift + B'
#   Check Package:             'Ctrl + Shift + E'
#   Test Package:              'Ctrl + Shift + T'
#
# https://r-pkgs.org/git.html
# https://www.youtube.com/watch?v=MoszELQFrvQ
# https://www.youtube.com/watch?v=XbSwdHBiO4E
# GitHub - My private github email: 26294221+fmgithub2017@users.noreply.github.com
  # (click on the profile picture -> email settings -> ...)
  # However it makes the package deployment to Github fail
  # So the solution is to uncheck that privacy or use this:
    # https://github.community/t/push-declined-due-to-email-privacy-restrictions/990/6
    # The steps I took to fix (thanks for the advice on this thread) were roughly
    # as follows: git config --global user.email
    # “YOUR_EMAIL_ADDRESS_HERE@users.noreply.github.com” git rebase -i git commit
    # --amend --reset-author git rebase --continue git push
    #
    # I found rebase -i allowed me to edit the commit message but retained the
    # previous (private) email address in the log. The commit --amend
    # --reset-author seemed to be the easiest way to replace the offending email
    # address.
# Git
  # Install: https://git-scm.com/download/
# Github
  # Install Github Desktop: https://desktop.github.com/
# Git & Github Manual
 #  https://happygitwithr.com/index.html

## https://www.youtube.com/watch?v=XbSwdHBiO4E
# Install Github Desktop and login with your Github account;
# Install Git;
# type git config --global --list at Terminal to see if the email and
# user name are correct.
# Global Options -> Git -> Enable -> Select Git Executable Path -> Restart R
# Create Project -> New Directory -> R Package ->
# -> Mark option "create a git repository" (this option will appear only after the
# previous installations);
# Create your .R files (and Description files if wanted);
# Press -> More -> Clean and Rebuild
# Then go to Github Desktop, select this repository and "Commit to Master" ->
# Click Publish to Github.
# Done. Now you can install your package via devtools::install_github(".../...")



hello <- function() {
  print("Hello, world!")
}
## [OBS:]++ The package directory cannot contain spaces or special characters

## Install and Load Packages
chooseCRANmirror(graphics=FALSE, ind=1)
# ## Default repo
# local({r <- getOption("repos")
#        r["CRAN"] <- "http://cran.r-project.org"
#        options(repos=r)
# })
# insert in the document, and Display Regressions Output Tables on the Browser ----
# install.packages("pacman", repos='http://cran.us.r-project.org')
# library(pacman)
# install.packages('huxtable')
install.packages(c('knitr','huxtable','estimatr'))
if(!require('tidyverse')) {install.packages('tidyverse')}
library(knitr) # generates error
library(huxtable)
library(estimatr)
library(tidyverse)

##
myfun <- function(x){
  y <- x*10
  return(y)
}

##
myhux <- function(tab, title=NA, pane='viewer'){
	#
	# pacman::p_load('huxtable','tidyverse')
	#
	ht <- tab %>% huxtable::tidy() %>% huxtable::hux()
	ht <- ht %>%
		# huxtable::add_colnames()  %>%
	  # Horizontal Alignment
		huxtable::set_align('center') %>%
		# Page Alignment
		huxtable::set_position('center') %>%
		# Layout Theme
	  huxtable::theme_article() # theme_blue() %>%
	# set_width() is important to control alignment and line breaks in PDF Output
  # huxtable::set_width(0.73)
	#
	if(!is.na(title)){
		ht <- huxtable::set_caption(ht, title)
	}
	#
	# Save file in a temporary file inside R session,
	# which is required to open in RStudio Viewer
	# generate a temporary html file and display it
	dir <- tempfile()
	dir.create(dir)
	html_file <- file.path(dir, "my_huxtable.html")
	huxtable::quick_html(ht, file=html_file)
	#
	if(pane=='browser'){
		# Open file in the browser
		# shell.exec('ht_temp.html')
		utils::browseURL(html_file)
	}
	if(
	pane=='viewer' &
  # rstudioapi::...() generates error when rendering files with knitr
	# so we have to not run this type of function in this context
	knitr::is_html_output()==FALSE &
	knitr::is_latex_output()==FALSE
	){
		# Open file in RStudio Viewer
		rstudioapi::viewer(html_file)
	}
	#
	return(ht)
}
# ## Applications
# # Linear Regression
# Futilities::myhux(summary(lm(cars[,1]~cars[,2])), title='Linear Regression Table')
# myhux(summary(lm(cars[,1]~cars[,2])), title='Linear Regression Table')
# # Robust SE Regression with estimatr package
# # pacman::p_load('estimatr')
# myhux(estimatr::lm_robust(cars[,1]~cars[,2]), pane='browser')

##
# install.packages('devtools')
# devtools::install_github("fmgithub2017/Futilities")
# Futilities::myhux(summary(lm(cars[,1]~cars[,2])), title='Linear Regression Table')
# Futilities::myhux(estimatr::lm_robust(cars[,1]~cars[,2]), pane='browser')
GitHunter0/Futilities documentation built on July 12, 2020, 12:03 a.m.