shiny is UI framework in R language
example
library(shiny)
library(DT)
library(tidyverse)
library(jsonlite)
library(httr)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Sentiment Analysis"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
textInput("caption", label="Enter your text here.", value="", placeholder = "Phrase to get a Sentiment..."),
verbatimTextOutput("value"),
# Input: Select a file ----
fileInput("file1", "upload csv file here",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
# Button
downloadButton("downloadData", "Download the Predictions")
),
# Show the table with the predictions
mainPanel(
verbatimTextOutput("Sentiment"),
DT::dataTableOutput("mytable")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
reactiveDF<-reactive({
req(input$file1)
df <- read.csv(input$file1$datapath, sep="\t", stringsAsFactors = FALSE)
url="http://127.0.0.1:5000/endpoint"
fdf<-NULL
for (i in 1:nrow(df)) {
body<-list(my_text=df[i,1])
b<-POST(url, body = body, encode = "json")
t1<-content(b, type="application/json")
tmpdf<-data.frame(InputText=df[i,1], Sentiment=t1$output)
fdf<-rbind(fdf, tmpdf)
}
return(fdf)
})
output$mytable <- DT::renderDataTable({
req(input$file1)
return(DT::datatable(reactiveDF(), options = list(pageLength = 100), filter = c("top")))
})
reactiveInput<-reactive({
req(input$caption)
url="http://127.0.0.1:5000/endpoint"
body<-list(my_text=input$caption)
b<-POST(url, body = body, encode = "json")
t1<-content(b, type="application/json")
df<-data.frame(Sentiment=t1$output)
return(df)
})
output$Sentiment<-renderText({
req(input$caption)
reactiveInput()$Sentiment
})
# Downloadable csv of selected dataset ----
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(reactiveDF(), file, row.names = FALSE)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
combine Flexdashboard with Shiny
Flexdashboard is an static or dynamic R markdown file. By combining flexdashboard with Shiny, you can write dynamic web applications without any knowledge of HTML, CSS, or JavaScript.
example
an examle web app with two user inputs and three reactive outputs. prerequisites are installation of shiny, flexdashboard, plotly, and dplyr. final effect is shown here: https://nataberishvili.shinyapps.io/EDAdashboard/
---
title: "EDA Dashboard"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(dplyr)
library(plotly)
```
```{r data}
data <- read.csv("BankChurners.csv")
Categorical.Variables = c("Gender", "Education_Level", "Marital_Status")
Numeric.Variables = c("Customer_Age", "Total_Trans_Ct", "Credit_Limit")
```
Column {.sidebar data-width=200}
-------------------------------------------------------------------
```{r}
selectInput(inputId="categorical_variable", label = "Select Categorical Variable:", choices = Categorical.Variables, selected = Categorical.Variables[1])
selectInput(inputId="numeric_variable", label = "Select Numeric Variable:", choices = Numeric.Variables, selected = Numeric.Variables[1])
```
Column {data-width=400}
-------------------------------------------------------------------
### **Box plot** shows the relationship between categorical and numeric variables
```{r}
renderPlotly({
plot_ly(data,
x = ~data[[input$numeric_variable]],
color = ~data[[input$categorical_variable]],
colors = "Paired",
type = "box") %>%
layout(title = "",
xaxis = list(title = "" ,
zeroline = FALSE))
})
```
Column {data-width=400}
-------------------------------------------------------------------
### **Bar chart** shows the distribution of categorical veriable
```{r}
renderPlotly({
data %>%
count(var = data[[input$categorical_variable]], name = "count") %>%
plot_ly( x = ~var, y = ~ count, type = "bar", marker = list(color = '#008ae6',
line = list(color = '#008ae6', width = 2)), hoverinfo = "x+y") %>%
add_text(text = ~paste0( " (", scales::percent(count/sum(count)),")"),
textposition = "bottom",
textfont = list(size = 12, color = "white"),
showlegend = FALSE) %>%
layout(xaxis = list(title = ""), yaxis = list(title = ""))
})
```
### **Histogram** shows the distribution of numeric variable
```{r}
renderPlotly({
plot_ly(x = data[[input$numeric_variable]], type = "histogram", marker = list(color = "#008ae6",
line = list(color = "darkgray",
width = 1)))
})
```