Auto complete and selection of multiple values in text box shiny
Look into shinysky
package and textInput.typeahead
. You can further customize the style of the textinput
yourself. Edit: I added example with select2Input
from the shinysky
package also for reference
rm(list = ls())
library(shinysky)
library(shiny)
my_autocomplete_list <- c("John Doe","Ash","Ajay sharma","Ken Chong","Will Smith","Neo")
ui <- shinyUI(
fluidPage(tags$style(type="text/css",".shiny-output-error { visibility: hidden; }",".shiny-output-error:before { visibility: hidden; }"),
tags$style(type="text/css","#search { top: 50% !important;left: 50% !important;margin-top: -100px !important;margin-left: -250px
!important; color: blue;font-size: 20px;font-style: italic;}"),
mainPanel(
# one way of doing it
textInput.typeahead(id="search",
placeholder="Type your name please",
local=data.frame(name=c(my_autocomplete_list)),
valueKey = "name",
tokens=c(1:length(my_autocomplete_list)),
template = HTML("<p class='repo-language'>{{info}}</p> <p class='repo-name'>{{name}}</p>")
),
br(),br(),
# using select2Input
select2Input("select2Input1","",choices=c(my_autocomplete_list),type = c("input", "select"))
)
)
)
server <- function(input, output, session) {}
shinyApp(ui = ui, server = server)
Edit 2 as per request. Please wrap your objects in a reactive
expressions as I did e.g. var <- reactive({...})
so you can re-use those later
rm(list = ls())
library(shinysky)
library(shiny)
my_autocomplete_list <- c("John Doe","Ash","Ajay sharma","Ken Chong","Will Smith","Neo")
ui <- shinyUI(
fluidPage(sidebarPanel(select2Input("txt","",choices=c("a","b","c"),selected=c("")), br(),actionButton("go","submit"), width =2),
mainPanel(textOutput('text'))
)
)
server <- function(input, output, session) {
var <- reactive({
if(input$go==0){return()}
isolate({
input$go
cbind("a","c")
})
})
output$text <- renderText({var()})
}
shinyApp(ui = ui, server = server)
Auto complete text field in Shiny
one way of doing this:
1-read your data in the server.r file
2-make a textinput module in server.r file
in server.r :
library(shiny)
library(shinydashboard)
function(input, output){
my_list <- reactive({
data <- read.csv("common_tree_names_246.csv", header = FALSE)$V1
my_list <- as.character(data)
})
output$tree <- renderUI({
selectInput(inputId = "tree", label = "Trees", choices = my_list())
})
}
and in ui.r:
library(shiny)
library(shinydashboard)
dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
uiOutput("tree")
)
)
Shiny: Combination of selectizeInput and textInput possible?
Took me nearly two years until I found the solution:
selectizeInput(..., options = list(create = TRUE))
Also mentioned here: shiny - looking for shortcut to combine selectize and textInput
How to Auto-Fill the fields in a shiny R form using a data frame info?
I guess this is what you are looking for:
The dataset I am using is as below. I had loaded the same data from a csv
file.
autoFillDF <- structure(list(Name = c("ABC", "XYZ", "PQR"), Age = c(30L, 24L,
27L), Grade = c("A", "B", "D")), .Names = c("Name", "Age", "Grade"
), class = "data.frame", row.names = c(NA, -3L))
ui.R code
shinyUI(fluidPage(
titlePanel("Auto Fill"),
sidebarPanel(
selectizeInput("p1", choices = autoFillDF$Name, selected = NULL, label = 'Name'),
selectizeInput("p2", choices = NULL, label = 'Age'),
selectizeInput("p3", choices = NULL, label = 'Grade')
),
mainPanel(
DT::dataTableOutput('table')
)
)
)
server.R code
autoFillDF <- read.csv('..../test.csv', stringsAsFactors = FALSE)
shinyServer(function(input, output, session) {
updateApp <- reactive({
data <- autoFillDF
data <- data[data$Name %in% input$p1,]
updateSelectizeInput(session, 'p2', choices = data$Age, selected = data$Age, server = TRUE)
updateSelectizeInput(session, 'p3', choices = data$Grade, selected = data$Grade, server = TRUE)
data
})
output$table <- DT::renderDataTable(
DT::datatable(updateApp())
)
})
Textinput in Shiny - How to cancel autocomplete
If you can't use passwordInput
we can use htmltools::tagQuery to set autocomplete = "off":
library(shiny)
library(htmltools)
ui <- fluidPage(
tagQuery(textInput("test", "test"))$find("input")$addAttrs("autocomplete" = "off")$allTags()
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
Related Topics
Namespace Dependencies Not Required
Does Roxygen2 Automatically Write Namespace Directives for "Imports:" Packages
How to Reference the Local Environment Within a Function, in R
Accessing Excel File from Sharepoint with R
Encrypting R Script Under Ms-Windows
Forcing R (And Rstudio) to Use the Virtual Memory on Windows
Convert from Lowercase to Uppercase All Values in All Character Variables in Dataframe
Cor Shows Only Na or 1 for Correlations - Why
"Factor Has New Levels" Error for Variable I'm Not Using
Subtract a Constant Vector from Each Row in a Matrix in R
Cartogram + Choropleth Map in R
Installation of Rodbc on Os X Yosemite
Using Trycatch and Rvest to Deal with 404 and Other Crawling Errors
Simple Method of Counting Non-Nas in Column of Data String
Regular Analysis Over Irregular Time Series