Sagar Pathak
2016-03-27
An application to convert temperature from Celsius to Fahrenheit and vice versa.
(Click next from the button right cornor)
Objective
As the name refers, the objective of this application is to convert temperature from following manners.
Following is the code written for server.R
celsiusToFahrenheit<- function(celsius){
(9.0 / 5.0) * celsius + 32.0;
}
farenheitToCelsius<- function(fahrenheit){
(5.0/9.0) * (fahrenheit-32.0)
}
shinyServer(
function(input, output) {
output$temperatureText <- renderText({
if(input$choice == 1){
temp <- "Celsius"
}else{
temp <- "Fahrenheit"
}
paste("Enter temperature in ",temp, "(degree)")
})
result <- eventReactive(input$convert, {
input$temperature
temp <- as.numeric(input$temp)
if(input$choice == 1){
f <- celsiusToFahrenheit(temp)
paste(temp, 'degree Celsius is = ',f, ' degree Fahrenheit')
}else{
c <- farenheitToCelsius(temp)
paste(temp, 'degree Fahrenheit is = ',c, ' degree Celsius')
}
})
output$result <- renderText({
result()
})
}
)
Following is the code written for ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Temperature Converter"),
sidebarLayout(
sidebarPanel(
h4("Documentation"),
HTML("This application consists of three input elements, <br/> 1. Radio button (to get the user choice of conversion<br/>2. Input Box (to get the value of temperature)<br/> 3. Submit button (To process the conversion in server so that it will display the result.)<br/>"),
HTML("Enter the information below to start with this application.")
),
mainPanel()
),
sidebarLayout(
sidebarPanel(
helpText('Please enter the information below.'),
fluidRow(
column(12,
radioButtons("choice", label = h3("Convert temperature from"),
choices = list("Celsius to Fahrenheit" = 1, "Fahrenheit to Celsius" = 2),selected = 1))
),
fluidRow(
column(12,
textInput('temp', label = textOutput('temperatureText'),
value = "", placeholder = "36"))
),
actionButton("convert", label = "Convert")
),
mainPanel(
h2(textOutput("result"))
)
))
)
When the application loads, all three html elemets will be rendered in the side panel as like below.
After 'Convert' button click action. The result will be displayed in main panel.
An application to convert temperature from Celsius to Fahrenheit and vice versa has be created successfully with shiny.
Thanks, Sagar Pathak