【问题标题】:SliderInput issue with table in R shinyR Shiny中的表格的SliderInput问题
【发布时间】:2018-03-28 17:37:11
【问题描述】:

运行此脚本后,我创建了一个包含两列“Customers_one”和“Customers_two”的 DT 表,即 R 中的 selectInput 和 SliderInput。我从第一列中的selecInput 中选择一个名称,并将其与第二列进行比较,并在第三列中给出两者之间的百分比相似度。我的问题是,在服务器代码中的最后两个 # 语句中,我试图使表格使得当滑块指向一个值说“75”时,我只得到相似度大于或等于 75% 的行。但是,当滑块指向 100 时,这不起作用。我希望滑块指向 100 并给我只有 100% 的行,当 80 时,还有 80% 及以上的行,所以使用“85”、“90” .在 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)

【问题讨论】:

标签: r shiny shinydashboard dt


【解决方案1】:

当你在做 subset(combine_total, similarity &gt;= input$slide) similarity 是一个字符向量,因此与 input$slide 比较是无效的。因此,要将similarity 转换为数字,您必须先从中删除%,然后使用as.numeric

为此,您需要将 combine_total1 = subset(combine_total, similarity &gt;= input$slide) 替换为 combine_total1 = subset(combine_total, as.numeric(sub("%", "", similarity)) &gt;= 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)

这样你就可以得到如下所示的输出:

希望对你有帮助!

【讨论】:

  • 谢谢,但是在执行此操作后,我无法在数据表中看到数据。你运行这个脚本并检查了吗?
  • 我没有得到这个,请帮助我,比较与 input$slide 的相似性除了只有当滑块指向 100 时,在 100 处,它会给出整个数据集。我就是不明白这个问题。
  • 另外,最后的相似度列应该用%来表示。
  • @AshminKaul 看看我的编辑。希望它能解决您的问题。
  • 你能帮我这个链接吗? stackoverflow.com/questions/46952771/…
猜你喜欢
  • 2015-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-09
  • 2016-05-17
  • 2017-03-25
  • 2022-01-22
  • 2022-01-13
相关资源
最近更新 更多