SliderInput issue with table in R shiny

风格不统一 提交于 2019-12-11 17:30:32

问题


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)

回答1:


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.

To do that you need to replace combine_total1 = subset(combine_total, similarity >= input$slide) with combine_total1 = subset(combine_total, as.numeric(sub("%", "", similarity)) >= input$slide)

EDIT

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:

Hope it helps!



来源:https://stackoverflow.com/questions/46783181/sliderinput-issue-with-table-in-r-shiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!