运行此脚本后,我创建了一个包含两列" Customers_one"的DT表。和" Customers_two",R闪亮的selectInput和SliderInput。我从selecInput
中选择了第一列中的名称,并将其与第二列进行比较,并在第三列中给出两者之间的百分比相似度。我的问题是,在服务器代码的最后两个#语句中,我试图使表格使滑块指向一个值时说" 75",我得到的行只有相似度大于或等于75%。但是,当滑块指向100时,这不起作用。我希望滑块指向100并且给我的行只有100%,当80,行80%及以上时,所以使用" 85&#34 ;," 90&#34 ;.在100,我看到整个数据集,这就是问题所在。请帮忙。
## 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 :(得分:1)
当您执行subset(combine_total, similarity >= input$slide)
similarity
时,是一个字符向量,因此与数字input$slide
进行比较将无效。因此,要将similarity
转换为数字,您必须首先从中删除%
,然后使用as.numeric
。
为此,您需要将combine_total1 = subset(combine_total, similarity >= input$slide)
替换为combine_total1 = subset(combine_total, as.numeric(sub("%", "", 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)
通过这个,您可以获得如下所示的输出:
希望它有所帮助!