1 | library(shiny)
|
2 | library(tidyverse)
|
3 | library(shinythemes)
|
4 | library(data.table)
|
5 | library(RCurl)
|
6 | library(randomForest)
|
7 | library(mlbench)
|
8 | library(janitor)
|
9 |
|
10 |
|
11 | # Read data
|
12 | DATA <- BostonHousing
|
13 |
|
14 | # Rearrange data so the response variable is located in column 1
|
15 | DATA <- DATA[,c(names(BostonHousing)[14],names(BostonHousing)[-14])]
|
16 |
|
17 | # Creating a simple RF model
|
18 | model <- randomForest(medv ~ ., data = DATA, ntree = 500, mtry = 4, importance = TRUE)
|
19 |
|
20 |
|
21 | # UI -------------------------------------------------------------------------
|
22 | ui <- fluidPage(
|
23 |
|
24 | sidebarPanel(
|
25 |
|
26 | h3("Parameters Selected"),
|
27 | br(),
|
28 | tableOutput('show_inputs'),
|
29 | hr(),
|
30 | actionButton("submitbutton", label = "calculate", class = "btn btn-primary", icon("calculator")),
|
31 | hr(),
|
32 | tableOutput("tabledata")
|
33 |
|
34 |
|
35 | ), # End sidebarPanel
|
36 |
|
37 | mainPanel(
|
38 |
|
39 | h3("Input widgets"),
|
40 | uiOutput("select")
|
41 |
|
42 | ) # End mainPanel
|
43 |
|
44 | ) # End UI bracket
|
45 |
|
46 |
|
47 |
|
48 | # Server -------------------------------------------------------------------------
|
49 | server <- function(input, output, session) {
|
50 |
|
51 | # Create input widgets from dataset
|
52 | output$select <- renderUI({
|
53 | df <- req(DATA)
|
54 | tagList(map(
|
55 | names(df[-1]),
|
56 | ~ ifelse(is.numeric(df[[.]]),
|
57 | yes = tagList(sliderInput(
|
58 | inputId = paste0(.),
|
59 | label = .,
|
60 | value = mean(df[[.]], na.rm = TRUE),
|
61 | min = round(min(df[[.]], na.rm = TRUE),2),
|
62 | max = round(max(df[[.]], na.rm = TRUE),2)
|
63 | )),
|
64 | no = tagList(selectInput(
|
65 | inputId = paste0(.),
|
66 | label = .,
|
67 | choices = sort(unique(df[[.]])),
|
68 | selected = sort(unique(df[[.]]))[1],
|
69 | ))
|
70 | )
|
71 | ))
|
72 | })
|
73 |
|
74 |
|
75 | # creating dataframe of selected values to be displayed
|
76 | AllInputs <- reactive({
|
77 | id_exclude <- c("savebutton","submitbutton")
|
78 | id_include <- setdiff(names(input), id_exclude)
|
79 |
|
80 | if (length(id_include) > 0) {
|
81 | myvalues <- NULL
|
82 | for(i in id_include) {
|
83 | myvalues <- as.data.frame(rbind(myvalues, cbind(i, input[[i]])))
|
84 |
|
85 | }
|
86 | names(myvalues) <- c("Variable", "Selected Value")
|
87 | myvalues %>%
|
88 | slice(match(names(DATA[,-1]), Variable))
|
89 | }
|
90 | })
|
91 |
|
92 |
|
93 | # render table of selected values to be displayed
|
94 | output$show_inputs <- renderTable({
|
95 | AllInputs()
|
96 | })
|
97 |
|
98 |
|
99 | # Creating a dataframe for calculating a prediction
|
100 | datasetInput <- reactive({
|
101 |
|
102 | df1 <- data.frame(AllInputs(), stringsAsFactors = FALSE)
|
103 | input <- transpose(rbind(df1, names(DATA[1])))
|
104 |
|
105 | write.table(input,"input.csv", sep=",", quote = FALSE, row.names = FALSE, col.names = FALSE)
|
106 | test <- read.csv(paste("input.csv", sep=""), header = TRUE)
|
107 |
|
108 |
|
109 | # defining factor levels for factor variables
|
110 | test[4] <- factor(test[4], levels = unique(DATA[,5])) # <- This line will cause problems if multiple factors in dataset or if different column location
|
111 |
|
112 |
|
113 | # Making the actual prediction and store it in a data.frame
|
114 | Prediction <- predict(model,test)
|
115 | Output <- data.frame("Prediction"=Prediction)
|
116 | print(format(Output, nsmall=2, big.mark=","))
|
117 | })
|
118 |
|
119 |
|
120 | # display the prediction when the submit button is pressed
|
121 | output$tabledata <- renderTable({
|
122 | if (input$submitbutton>0) {
|
123 | isolate(datasetInput())
|
124 | }
|
125 | })
|
126 |
|
127 |
|
128 | } # End server bracket
|
129 |
|
130 |
|
131 |
|
132 | # ShinyApp -------------------------------------------------------------------------
|
133 | shinyApp(ui, server)
|