HTML page that takes input, runs R code and returns the result in HTML

I am trying to create a front end to display the output of my R code which takes 2 files, master.csv and detail.csv. Master.csv contains reference data with 2 columns which contains product category and product name as shown below:

Sl.No. Commodity Category Commodity Name
1 Stationary Pencil
2 Stationary Pen
3 Stationary Marker
4 Office Utensils Chair
5 Office Utensils Drawer
6 Hardware Monitor
7 Hardware CPU

Detail.csv contains custom data that contains different product names as shown below:

Sl.No. Commodity Name
1 Pancil
2 Pencil-HB 02
3 Pencil-Apsara
4 Pancil-Nataraj
5 Pen-Parker
6 Pen-Reynolds
7 Monitor-X001RL

The output I get is the corrected product names categorized into their respective product categories as shown below:

Commodity.Name.Old Commodity.Name Commodity.Category
1 Pancil Pencil Stationary
2 Pencil-HB 02 Pencil Stationary
3 Pencil-Apsara Pencil Stationary
4 Pancil-Nataraj Pencil Stationary
5 Pen-Parker Pen Stationary
6 Pen-Reynolds Pen Stationary
7 Monitor-X001RL Monitor Hardware

Below is the R code that performs this amendment and classification:

library(stringdist)
library(dplyr)

file1 <-read.csv("/Users/Desktop/Master.csv",sep=",",header=TRUE)
file2 <-read.csv("/Users/Desktop/Detail.csv",sep=",",header=TRUE)
file3 <-read.csv("/Users/Desktop/file3.csv",sep=",",header=TRUE)


CName <- levels(file1$Commodity_Name)

CName.lower <- tolower(CName)

correct_1 <- function(x){
  scores = stringdistmatrix(tolower(x), CName.lower, weight=c(1,0.001,1,0.5))
  if (min(scores)>2) {
    return("Others")
  } else {
    return(as.character(CName[which.min(scores)]))
  }
}


correct <- function(x) {
  sapply(as.character(x), correct_1)}


correctedfile2 <- file2 %>%
  transmute(Commodity.Name.Old = Commodity_Name, Commodity_Name = 

              correct(Commodity_Name))

file1$Commodity.Name = as.character(file1$Commodity_Name)


merged <- merge(file3,correctedfile2,all.correctedfile2=TRUE)
mm <- merged[,c("Commodity.Name.Old","Commodity_Name","Commodity_Category")]

      

Here I provide 2 excel files. But now I want to make the second file dynamic. I want to create an html page that takes a product name as input from a user and feeds it into R code for execution and gives an inference as to which product category it belongs to. I want to say instead of Detail.csv I want to provide R form data. Below is the HTML code

<body>
<p>Enter the commodity name </p>

<form id="myForm">
Commodity Name: <input type="text" name="Commodity Name" value=""><br>
<input type="submit" value="Submit" >
</form>

<p>Click the "Submit" button to check the commodity category</p>

//R code runs and fetches the output
<p>The entered commodity belongs to Category --- </p>

</body>
</html>

      

After I submit, the trade name must be passed to R and the result passed. I don't understand how to link this HTML to R. I know I have to use R Markdown and knitR for this. After I click on SUBMIT, I don't get how to pass this data to R.

Any suggestions are greatly appreciated.

+3


source to share


3 answers


library(shiny)
library(dplyr)

server <- function(input, output) {

  file1 <- read.csv("/Users/Desktop/unspscList.csv", sep=",", header=TRUE)

  CName <- levels(file1$Commodity_Name)
  CName.lower <- tolower(CName)

  correct_1 <- function(thing){
    scores = stringdistmatrix(tolower(thing), CName.lower, weight=c(1,0.001,1,0.5))
    if (min(scores)>2) {
      return("UNKNOWN")
    } else {
      return(as.character(CName[which.min(scores)]))
    }
  }

classify <- function(thing) {
    result <- file1 %>% filter( tolower(Commodity_Name) == tolower(sapply(as.character(thing), correct_1))  )
    print(paste0("The commodity Code is: ",result$Full_code))
    print(paste0("The commodity Category is: ",result$Level1_Description))
    print(paste0("The commodity Name is: ",result$Commodity_Name))
  }

  output$commodity <- renderText(

    if(input$commodity == "")
    {
      paste("You entered: ''")
    }
    else if(input$commodity == " ")
    {
      paste("You entered: 'Space'")
    }

    else
    {
      paste("You entered:", "'",input$commodity,"'")
    })

  ntext <- eventReactive(input$goButton, 
                         { if(input$commodity == "")
                         {
                           "Please enter a valid commodity name!"
                         }
                           else if(input$commodity == " ")
                           {
                             "Blank Space is not a valid commodity name! Try Again!"
                           }
                           else
                           {
                             classify(input$commodity)
                           }
                         }
  )


  output$classifiedThing <- renderPrint({
    ntext()
  })

}
ui <- fluidPage(
  titlePanel("Commodity Classification App"),
  sidebarLayout(
    sidebarPanel(
      textInput("commodity", "Enter the Commodity Name:"),
      actionButton("goButton", "Classify!"),
      p("Click the classify button to View the commodity Category and code in the main panel.",
        style = "font-family: 'times'; font-si16pt ; color:purple "),

      tags$head(tags$style("#commodity{color: blue;
                           font-size: 15px;
                           font-style: italic;
                           }","body {background-color: PeachPuff;}"
                         )
      )



      ),
    mainPanel("Classification of Commodities",

              verbatimTextOutput("commodity"),
              verbatimTextOutput("classifiedThing"))
  )
)

shinyApp(ui = ui, server = server)

      



0


source


Here's a small example of Shiny:

library(shiny)
library(dplyr)

server <- function(input, output) {

  # setup "do once" only things
  # if your data doesn't change much it should really be in 
  # an R data file
  file1 <- read.csv("Master.csv", sep=",", header=TRUE)

  # you have more code but this is a sample "classify" function
  classify <- function(thing) {
    file1 %>% filter(CommodityName == thing)
  }

  output$classifiedThing <- renderPrint({
    # this is where you'd call your classification function
    classify(input$commodity)
  })

}

ui <- fluidPage(
  titlePanel("My Awesome Classification App"),
  sidebarLayout(
    sidebarPanel(
      textInput("commodity", "Commodity Name:", "Pen"),
      submitButton("Classify!")
    ),
    mainPanel(verbatimTextOutput("classifiedThing"))
  )
)

shinyApp(ui = ui, server = server)

      

This is "One File Brilliant App". If your code gets more complex, you might want to use separate files server.R

and ui.R

.



the official shiny tutorial is pretty good and you should be able to style and place your app however you want. You can use actionButton

vs a submitButton

after reading the tutorial, but that is up to you.

If the list of products is not huge, I would suggest a popup menu to restrict user selection and free form text.

+1


source


Check out this example in the brilliant gallery. It is designed to download a user report.

Hope this helps!

0


source







All Articles