knitr::opts_chunk$set(fig.width=12, fig.height=8, fig.path='Figs/', results="hide", warning=FALSE)
Download libraries
library(survival)
library(survminer)
library(rms)
library(shiny)
library(readxl)
library(DynNom)
Import data
# data <- read_xlsx()#your data pathway
#This is mock dataframe so I can show you where to put each variable. But use the code above to get your data from excel or csv.
df <- data.frame(ID = c(1, 2, 3, 4, 5),
outcome = c(1,1,0,0, 1),
var1 = c('a', 'b', 'c', 'd', 'e'),
var2 = c(1, 1, 0, 0, 1))
print(df)
Run univariable log regression using a loop
lapply(c('outcome',
'var1',
'var2'), #list all your variables
function(var) {
formula <-
as.formula(paste("outcome ~", var)) # this is a way of telling R to run the part that is between "" with all the variables that are in your lapply list
res.logist <-
glm(formula, data = df, family = "binomial") #glm is the function for log regression. You can change this with the name of the test you need to run.
summary(res.logist)
}
)
Run multivariable log regression
mul.logreg <- glm(outcome ~ var1 +var2, data = df, family = "binomial")
summary(mul.logreg)
Creating the app
data <- datadist(df)
options(datadist = 'df')
DNbuilder(mul.logreg, DNtitle="This is an example", DNxlab = "Probability of survival")
model <- glm(outcome ~ var1 +var2, data = df, family = "binomial")
#This will create a set of R scripts in your directory.
#functions.R contains the general functions the app needs - Do not touch this.
#global.R contains your data and model, this will be used by the other scrips to run the app
#server.R contains the actaul interactive code. This is like the javascrip of your app
#ui.R contains the layout and style, the CSS of the app.
#Open all those files in Rstudio, and copy paste "data <- datadist(df) options(datadist = 'df') model <- glm(outcome ~ var1 +var2, data = df, family = "binomial")" in the global.R after 'source'.
#Press 'RunApp' (top right of your Source panel). You'll see stuff happening in the console and the Viewer panel going blank (The viewer panel is contained in one of the four panels in your Rstudio. Next to Files, Plots, Packages, Help).
#There's a button next the to STOP button in the Viewer panel that has a little arrow. Click that.
# A new page will open with your new working app :)
#The layout, style and everyting will be looking quite raw. You can make it look nicer changing the CSS code. Happy to help out in case you get stuck.
# this is the app I created using the same DynNom package : https://predictepilepsy.github.io/