2017-10-17 125 views
0

在运行此脚本时,我创建了一个DT表,其中包含两列“Customers_one”和“Customers_two”,它们是R闪亮式中的selectInput和SliderInput。我从第一列中选​​择selecInput的名称,并将其与第二列进行比较,并在第三列中给出两者之间的百分比相似性。我的问题是,在服务器代码中的最后两个#语句中,我试图制作表格,以便当滑块指向值“75”时,我得到的行只有相似度大于或等于75%。但是,当滑块指向100时,这不起作用。我希望滑块指向100,并且只给出100%的行,同样当80时,80%及以上的行,以及“85”,“90” 。在100处,我看到整个数据集,这就是问题所在。请帮忙。SliderInput与R闪亮表中的表问题

## 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) 
+0

可能重复的[滑块没有给予适当的值,当指针在R闪存表100](https://stackoverflow.com/questions/46766286/slider-not-giving-proper-value-when-pointed-at- 100-in-r-shiny-table) –

+0

嗨,我希望是的,我试过了,但我仍然无法弄清楚。如果你可以在这里运行这个脚本,你可以很容易地得到我的问题。 –

回答

1

当你正在做subset(combine_total, similarity >= input$slide)​​因此是与input$slide这数字将无法工作比较在一个字符向量。因此,要将​​转换为数字,您必须首先从中删除%,然后使用as.numeric

要做到这一点,你需要combine_total1 = subset(combine_total, as.numeric(sub("%", "", similarity)) >= input$slide)

编辑取代combine_total1 = subset(combine_total, similarity >= input$slide)

看一看与改变了这种修改后的代码如上文所述:

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

有了这个你如预期的输出如下所示:

enter image description here

希望它有帮助!

+0

谢谢,但发布后,我无法看到数据表中的数据。你运行这个脚本并检查了吗? –

+0

我没有得到这个,请帮助我,比较输入$幻灯片作品的相似性,除非滑块指向100,在100,它会给出整个数据集。我无法解决问题。 –

+0

另外,最终相似性列应以%表示。 –