2017-10-17 124 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

另外,最終相似性列應以%表示。 –