R Shiny中表格的SliderInput问题 [英] SliderInput issue with table in R shiny

查看:329
本文介绍了R Shiny中表格的SliderInput问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行此脚本后,我创建了一个DT表,其中包含两列 Customers_one和 Customers_two,R光泽中的selectInput和SliderInput。我也从第一列的 selecInput 中选择一个名称,并将其与第二列进行比较,并在第三列中给出两者之间的百分比相似度。我的问题是,在服务器代码中的最后两个#语句中,我试图使表如此,以使得当滑块指向一个值 75时,我只会得到相似度大于或等于75%的行。但是,这在滑块指向100时不起作用。我希望滑块指向100,并给我仅100%的行,当80时,行的80%或更高,因此 85, 90 。在100,我看到了整个数据集,这就是问题所在。请帮忙。

Upon running this script, I create a DT table with two columns "Customers_one" and "Customers_two", a selectInput and SliderInput in R shiny. I select a name from selecInput which is in the first column too and compare it with the second column and give percentage similarity between the two in the third column. My issue is that, in the last two # statements in server code, I am trying to make the table such that when slider points at a value say "75", I get rows only with similarity greater or equal to 75%. This, however, does not work when slider points at 100. I want the slider to point at 100 and give me rows with only 100%, Also when 80, rows with 80% and above, so with "85","90". At 100, I see the entire dataset and this is the problem. Please help.

## app.R ##
library(shiny)
library(shinydashboard)
library(stringdist)
library(RecordLinkage)
library(dplyr)
library(scales)
library(DT)

Customers_one = 
c("Ashminkaul","Ashminkaul","Ashminkaur","Ashminkau","Ashmkaul","Ainkaul")
Customers_two = 
c("Ashminkau","Ashminka","Ashminkaul","Ashmink","Ashminkaul","Ashminkaulb")
Customers_one = as.character(Customers_one)
Customers_two = as.character(Customers_two)
ui <- fluidPage(
titlePanel("DT table Issue"),

# Create a new Row in the UI for selectInputs
fluidRow(

column(4,
       selectInput("names",
                   "Customer:",
                   c(as.character(Customers_one))),
       sliderInput("slide", "Select the name with similarity %",
                   min = 75, max = 100,
                   value = 75, step = 5)
 )),
 # Create a new row for the table.
 fluidRow(
 DT::dataTableOutput("table")
 )
 )
 server <- function(input, output) {

 output$table <- DT::renderDataTable(DT::datatable({
 similarity = percent(RecordLinkage::levenshteinSim(input$names, 
 Customers_two))
 combine_total = data.frame(Customers_one,Customers_two, similarity)
 combine_total
 #combine_total1 =  subset(combine_total, similarity >= input$slide)
 #combine_total1
 }))
 }
 shinyApp(ui, server)


推荐答案

当您执行子集(combine_total,相似度> = input $ slide) 相似度是字符矢量时,在带有 input $ slide 的数字将不起作用。因此,要将相似度转换为数字,必须首先从中删除,然后使用 as.numeric

When you are doing subset(combine_total, similarity >= input$slide) similarity is a character vector hence comparing in with input$slide which is numeric won't work. So to convert similarity into numeric you'll have to first remove the the % from it and then use as.numeric.

为此,您需要替换 combine_total1 = subset(combine_total,相似性> =输入$幻灯片)组合_total1 =子集(combine_total,as.numeric(sub(%,,相似性))> =输入$幻灯片)

编辑

看一下经过修改的代码,其内容如上所述:

Have a look at this modified code with the changes as described above:

 ## app.R ##
    library(shiny)
    library(shinydashboard)
    library(stringdist)
    library(RecordLinkage)
    library(dplyr)
    library(scales)
    library(DT)
    
    
    Customers_one = 
      c("Ashminkaul","Ashminkaul","Ashminkaur","Ashminkau","Ashmkaul","Ainkaul")
    Customers_two = 
      c("Ashminkau","Ashminka","Ashminkaul","Ashmink","Ashminkaul","Ashminkaulb")
    Customers_one = as.character(Customers_one)
    Customers_two = as.character(Customers_two)
    ui <- fluidPage(
      titlePanel("DT table Issue"),
      
      # Create a new Row in the UI for selectInputs
      fluidRow(
        
        column(4,
               selectInput("names",
                           "Customer:",
                           c(as.character(Customers_one))),
               sliderInput("slide", "Select the name with similarity %",
                           min = 75, max = 100,
                           value = 75, step = 5)
        )),
      # Create a new row for the table.
      fluidRow(
        DT::dataTableOutput("table")
      )
    )
    server <- function(input, output) {
      
      output$table <- DT::renderDataTable(DT::datatable({
        similarity = percent(RecordLinkage::levenshteinSim(input$names, 
                                                           Customers_two))
        
        combine_total = data.frame(Customers_one,Customers_two, similarity)
        combine_total
        combine_total1 =  subset(combine_total, as.numeric(sub("%", "", similarity)) >= input$slide)
        combine_total1
      }))
    }
    shinyApp(ui, server)

这样您将获得预期的输出,如下所示:

With this you get the output as expected shown below:

希望有帮助!

这篇关于R Shiny中表格的SliderInput问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆