在闪亮的UI的闪亮主面板中显示输出(代码正在运行,没有任何错误,结果不会显示在UI上)

时间:2016-01-07 09:58:51

标签: r shiny

我正在尝试创建一个可以上传文件的UI,还有一个文本输入,我可以在其中编写我想在上传文件中搜索的产品名称。我正在使用Levenshtein距离函数(adist()函数)。现在,一旦我得到编辑距离为0的结果,我想在主面板的表中显示这些行。无论UI上的文本输入中给出什么输入,都会根据上载文件中的项目列进行搜索。上传的CSV文件的示例图像是 -

Sample image of the CSV file which is input by the user

一旦我运行代码并找到所有单词的编辑距离,我将它们存储在一个向量中,然后用它来打印文件中编辑距离等于0的行。问题是当我点击在提交时,结果不会显示在UI上,但会显示在R-studio控制台上。我该如何解决这个问题?

请帮我解释一下代码。

library(shiny)

ui = shinyUI(fluidPage(
  titlePanel("LEVENSHTEIN DISTANCE function trial"),

  sidebarLayout(

    sidebarPanel(
      numericInput("rows","Enter the number of rows",value=NULL),
      textInput("product", "input product name"),
      br(),
      br(),
      fileInput("file", "Input the file"),
      submitButton("Find!")),

    mainPanel(
      tableOutput("result")
    )
  )
))

server = shinyServer(function(input,output) {
  output$result <- renderPrint({ if (is.null(input$file)) return( ); 
    trial = read.csv(input$file$datapath)

    ls = vector('list', length = input$rows)

    for(i in 1:input$rows) {
      edit = adist("earbuds", trial$items[i])
      new_edit = as.numeric(edit)
      ls[i] = edit
      if(ls[i]==0) print(trial[i, ])
    }

  })
})

shinyApp(ui=ui,server=server)

谢谢!

1 个答案:

答案 0 :(得分:3)

在没有样本输入日期的情况下提供工作代码非常困难。但是,这是我试图给你我认为应该起作用的东西。

server = shinyServer(function(input,output) {
  output$result <- renderTable({
    if (!is.null(input$file)) { 
      trial = read.csv(input$file)
      trial <- trial[adist('earbuds', trial$items) == 0), ]
    }
  })
})

如果您提供输入数据和预期输出表,我可以编辑答案更精确。