HTML 获取输入、运行 R 代码并将结果返回给 HTML 的页面

HTML page which takes the input, runs the R code and gives the result back to HTML

我正在尝试设计一个前端来显示我的 R 代码的结果,该代码需要 2 个文件,master.csv 和 detail.csv。 Master.csv 包含 2 列的参考数据,其中包含商品类别和商品名称,如下所示:

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包含用户数据,其中包含各种商品名称,如下所示:

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

我得到的输出是更正后的商品名称,分类到各自的商品类别中,如下所示:

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

以下是进行校正和分类的R代码:

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")]

在这里,我提供了 2 个 excel 文件。但是现在我想使第二个文件动态化。我想设计一个html页面,将商品名称作为用户的输入,提供给R代码执行,输出是属于哪个商品类别。我的意思是说我想向 R 提供表单数据而不是 Detail.csv。 下面是HTML代码

<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>

我提交后,商品名称应该传递给R并给出输出。我不知道如何 link 这个 HTML 到 R。 我知道我应该为此使用 R Markdown 和 knitR 包。单击提交后,我不知道如何将此数据传递给 R。

欢迎任何类型的建议。

查看闪亮图库中的 this 示例。用于下载 knitr 报告。

希望对您有所帮助!

这是一个闪亮的小例子:

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)

那是 "single file Shiny app"。如果您的代码变得更加复杂,您可能需要考虑使用单独的 server.Rui.R 文件。

official Shiny tutorial 非常好,您应该能够按照您需要的方式设置应用程序的样式和布局。阅读本教程后,您可能会决定使用 actionButton 还是 submitButton,但这取决于您。

如果商品列表不是很大,我建议使用弹出菜单来限制用户对自由格式文本的选择。

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)