본문 바로가기
Biz/Analysis

Shiny에서 SparkR 실행하기

by 조병희 2016. 3. 28.

Shiny와 SparkR을 통해서 웹으로 분석결과 보여주기

 

server.R에서는 glm 을 통해서 회귀 모델을 구해서 웹에서 변수값에 따른 결과를 보여준다.

 

server.R

 

# First install shiny library
library(shiny)
library(ggfortify)

# Set the system environment variables
Sys.setenv(SPARK_HOME = "C:/app/gitProjects/spark")
.libPaths(c(file.path(Sys.getenv("SPARK_HOME"), "R", "lib"), .libPaths()))

#load the Sparkr library
library(SparkR)

# Create a spark context and a SQL context
sc <- sparkR.init(master = "local")
sqlContext <- sparkRSQL.init(sc)

#create a sparkR DataFrame for the "iris" dataset
iris_DF <- createDataFrame(sqlContext, iris)
cache(iris_DF)
local_DF <- as.data.frame(iris_DF)
# Statistical machine learning
model_fit <- glm(Sepal_Length ~ Species + Petal_Width + Petal_Length, data = iris_DF, family = "gaussian")

local_model_fit <- glm(Sepal_Length ~ Species + Petal_Width + Petal_Length, data = local_DF, family = "gaussian")

# Define server logic required to predict the sepal length
shinyServer(function(input, output) {

  output$summary_model <- renderPrint({summary(model_fit)})

    output$predict_new_value <- renderText({
   
    input$predictSepalLength
   
    isolate({
      Species <- as.character(input$species)
      Petal_Width <- as.double(input$petalWidth)
      Petal_Length <- as.double(input$petalLength)

      new_data_frame <- data.frame(Species = Species,
                                 Petal_Width = Petal_Width,
                                 Petal_Length = Petal_Length)
     
      newDataFrame <- createDataFrame(sqlContext, new_data_frame)
     
      predicted_value <- predict(model_fit, newData = newDataFrame)
     
      unlist(head(select(predicted_value, "prediction")))
    })
  })
 
  output$graph1 <- renderPlot({
    print(autoplot(local_model_fit))
  })
})

 

ui.R

 

library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(
 
  # Application title
  titlePanel("Shiny and SparkR!"),
 
  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(
     
      # add some help text
      h3("Get Started"),
     
      p("This is a simple application that demonstrates how to use Apache SparkR to power
        a shiny application")
     
      ),
   
    mainPanel(
     
      # add a selection box for selecting a county
      h3("Prediction of Sepal Length of Flowers"),
      p("Select a set of input variables below to predict the sepal lenght of flowers"),
     
      fluidRow(

               selectInput("species",
                           label = "Select the Species",
                           choices = list("versicolor", "virginica"),
                           selected = "versicolor")
,
               numericInput("petalWidth",
                           label = "Petal Width (mm):",
                           min = 0.01, max = 3, value = 0.1)
,       
               sliderInput("petalLength",
                           label = "Petal Length (mm):",
                           min = 0.1, max = 7.0, value = 0.1)
      ),
     
      actionButton("predictSepalLength",
                   label="Predict Sepal Length"),
      br(),
      br(),
     
      verbatimTextOutput("predict_new_value"),
     
      plotOutput("graph1"),

      h3("The Estimated Model"),
      p("The table below shows the estimated model for predicting the Sepal Length"),
     
      verbatimTextOutput("summary_model")
   
  )
  )))

 

 

 

 

'Biz > Analysis' 카테고리의 다른 글

RStudio Server: 서버 구성과 관리  (0) 2016.07.17
RStudio Server with a Proxy  (0) 2016.07.17
RStudio 에서 Spark 사용하기  (1) 2016.03.26
Hands-on Tour of Apache Spark in 5 Minutes  (0) 2016.03.25
Magellan: Geospatial Analytics on Spark  (0) 2016.03.25

댓글