0

Consider the following code, the data below is only sample of the complete data set. In actual all these data a data.frames.

#Input data
x = c(70407, 70407, 10977, 10977, 70668, 70450, 70276, 70450, 20820,"L06G1", "L06AP",20820, 70450, 70450, 70450, 70190, 70450)

#Shiny ui.R
selectInput(inputId = "z",label = "PN",choices = unique(x),multiple = TRUE,selected = "")

#if else statement in server.R
y = if(is.null(input$z)) as.vector(unique(x)) else input$z

My concern is even if select specific value for z , output y instead of being z , is showing unique(x).

All data types are same, character.

Is there a better solution in dplyr ?

Stedy
  • 7,359
  • 14
  • 57
  • 77
user3883642
  • 31
  • 1
  • 5
  • I understand that the code is not reproducible. The file is too big to be posted here. The concern here is ifelse does not seems to work when the content in dataframe is too much similar (i assume) . Any comments on how we can do it in R ? Like in excel we have for vlookup "EXACT" ? – user3883642 Jul 30 '18 at 08:59
  • thanks all. the code finally worked , i used as_vector (from purrr) instead of as.vector. looks to be accuracy with which it matches. – user3883642 Jul 31 '18 at 11:49

1 Answers1

1

There should be another problem with your implementation. Here is a minimal working example based on your description:

library(shiny)

x = c(70407, 70407, 10977, 10977, 70668, 70450, 70276, 70450, 20820, "L06G1", "L06AP", 20820, 70450, 70450, 70450, 70190, 70450)

## if x is a column of the data frame df as you stated:
# x <- df$x[!is.na(df$x)]

ui <- fluidPage(
  selectInput(
    inputId = "z", 
    label = "PN", 
    choices = unique(x),
    selected = NULL,
    multiple = T,
  ),
  verbatimTextOutput("y")
)

server <- function(input, output) {
  output$y <- renderPrint({
    y <- if(is.null(input$z)) as.vector(unique(x)) else input$z
    y
  })
}

shinyApp(ui = ui, server = server)

Please note that you can get the most out of the Stack Overflow community if you provide a reproducible question: How to make a great R reproducible example?

OzanStats
  • 2,756
  • 1
  • 13
  • 26
  • Thanks for the feedback on reproducible example, my next question , will include the same. However , the code written above does not seems to work. – user3883642 Jul 30 '18 at 08:52
  • 1
    Can you evaluate a little more? It does not run or it does not meet your criteria? What are you trying to achieve? As far as I understand, you want to store all unique values if the input is null and the selected values if it exists. – OzanStats Jul 30 '18 at 09:13
  • Exactly ! the concern here is even when i select a value , all of unique value are stored as output. – user3883642 Jul 30 '18 at 09:19
  • Could you please run my answer in your console? The text output shows the stored values, which follows the behavior you want: all unique variables are displayed at the initial state as nothing is selected (i.e. input is NULL) and just the selected ones are displayed when you start choosing. I don't see any problem. – OzanStats Jul 30 '18 at 09:27
  • It works ! however when i try to replicate the same to other application (with 50K data points) , it does not seems to work. Any reason ? – user3883642 Jul 30 '18 at 09:41
  • Sorry, this is all I can cook with the ingredients you provided. Already indicated that there is another problem. – OzanStats Jul 30 '18 at 09:52
  • unfortunately the file is too big. can you tell me how do we make the condition in if else more stringent ? i tried it with if_else , here for TRUE value it takes only a single number instead of vector. suggest for any more accurate method .. – user3883642 Jul 31 '18 at 10:16