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

时间:2015-05-20 09:14:27

标签: html r shiny knitr r-markdown

我正在尝试设计一个前端来显示我的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代码进行执行,并给出输出它所属的商品类别。我的意思是说而不是Detail.csv我想向R提供表单数据。 以下是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并提供输出。我没有得到如何将此HTML链接到R. 我知道我应该使用R Markdown和knitR包。点击SUBMIT后,我没有得到如何将这些数据传递给R。

赞赏任何建议。

3 个答案:

答案 0 :(得分:1)

这是一个小小的例子:

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)

这是一个“单个文件Shiny app”。如果您的代码变得更复杂,您可能需要考虑使用单独的server.Rui.R文件。

official Shiny tutorial非常好,你应该能够设计风格&amp;以您需要的方式布置您的应用程序。阅读完教程后,您可能决定使用actionButton vs submitButton,但这取决于您。

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

答案 1 :(得分:1)

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)

答案 2 :(得分:0)

查看闪亮图库中的this示例。这是为了下载编织报告。

希望这有帮助!