I have a UI that is projectdashboard in R shiny. I want to be able to type in a text/search box and have the data associated with it show up as i type.
server <- function(input, output,session) {
output$ui_names = renderUI({
name_list = mydata()[,"names"]
pickerInput("name", label=h3(" Names:"),
choices = sort(unique(name_list)),options = list("actions-box" = TRUE,"live-search" = TRUE,"none-selected-text"='Select Names'),
selected = NULL,multiple = TRUE)
})
ui <- dashboardPage(
dashboardHeader(title=textOutput("title"),titleWidth = 1500),
dashboardSidebar(
uiOutput("ui_names")
)
shinyApp(ui = ui, server = server)
This however does not give me expected or working results. How can i put a text/searchbar in the dashboard side bar, that will 'live-search' the data i am feeding it.
you can use the following:
sidebarSearchForm(textId = "searchText", buttonId = "searchButton",label = "Search...")
Please check if this meet your requirements
Related
I'm trying to have a Shiny app, that is connecting to a database, but the IP and so an can be modified by a User.
In short, I kind of just want to know, if its possible to have a shiny app, with objects from reactiv server parts included in the UI select inputs.
Like this:
library(shiny)
ui <- fluidPage(
selectizeInput(inputId = 'inSelect',
label = "countries",
choices = getTable(),
multiple = TRUE,
options = list(maxItems = 4,
placeholder = 'select up to 4 countries')
)
)
server <- function(input, output) {
getTable <- reactive({
country <- data.frame(name=c("Germany","France","Japan"),
code=c("DEU","FRA","JPN"))
countryN <- country$code
names(countryN) <- country$name
})
}
shinyApp(ui = ui, server = server)
when it works, it should look like this:
I know, that I can achieve this effect, by just building the DF before the UI, but my real problem is ab bit more complicated:
I want the user to type in IP, Username,PW, and DB name, to connect to the DB.
After a button click, the connection should be established and the df. country should be pulled out of the DB, not constructed.
I think, that I have to do this part in the (reactive) Server part, because I am using Input$x in the DB connection.
I hope, that my vision is somehow possible and you guys can help me,
Thanks.
you can use updateSelectInput() to update a select input from the server side of the app more info found here: https://shiny.rstudio.com/reference/shiny/1.2.0/updateSelectInput.html
in your specific example you can achieve this by:
library(shiny)
ui <- fluidPage(
selectizeInput(inputId = 'inSelect',
label = "countries",
choices = NA,
multiple = TRUE,
options = list(maxItems = 4,
placeholder = 'select up to 4 countries')
)
)
server <- function(input, output,session) {
getTable <- reactive({
country <- data.frame(name=c("Germany","France","Japan"),
code=c("DEU","FRA","JPN"))
countryN <- country$code
names(countryN) <- country$name
return(country)
})
observe({updateSelectInput(session, inputId="inSelect", label = NULL, choices = getTable()$name,
selected = NULL)})
}
shinyApp(ui = ui, server = server)
by adding the argument session to the server function and then using the updateSelectInput() function, this must be wrapped in a reactive expression in this example I used observe()
I have the shiny dashboard below in which I want to use a variable from my pickerInput() and create a plot. The issue is that if I use ,for example name or snID instead of input$DB the plot is created. But when I use input$DB I get: Warning: Error in table: all arguments must have the same length
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(ggplot2)
library(plotly)
ui <- dashboardPage(
header = dashboardHeader(title = "My dashboard"),
sidebar = dashboardSidebar(
uiOutput("dbs")
),
body = dashboardBody(
plotlyOutput("fn")
)
)
server <- function(input, output, session) {
sts<-c("Rev","Rev")
sID<-c("123","124")
snID<-c("23","34")
name<-c("s","d")
pe<-data.frame(sts,sID,snID,name)
output$dbs<-renderUI({
pickerInput("DB", "Select Database/s",
choices = c("name","snID"),
multiple = F,options = list(`actions-box` = TRUE),
selected = "name")
})
output$fn<-renderPlotly({
#2.2 MAKING A TABLE for public.exists
tbl<-table(pe[[input$DB]], pe$sts)
ggplotly(
ggplot(as.data.frame(tbl), aes(!!sym(input$DB), Freq, fill = sts))
)
})
}
shinyApp(ui, server)
I suspect your output$fn reactive is executing before input$DB has a value. Therefore, add
req(input$DB)
at the start of the reactive, and you should be OK.
In the absence of any demo input data, it's difficult to be definitive.
I am creating an R Shiny application primarily using checkboxGroupInput where for each checkbox name I check, the corresponding table should display in the main UI panel. I have linked each checkbox option to its corresponding table (already in my previous script) in the "choices" argument of checkboxGroupInput. I use eventReactive to make a working button and renderTable to produce the appropriate tables. However, what displays in the main panel when I click the button is a list of each cell in the table rather than the table itself. This list of values looks a bit like this:
list(CUI = "C05372341", LAT = "ENG", TS = "P", LUI = "L0883457", STT = "PF", SUI = "S13423408", ISPREF = "N", AUI = "A10344304", SAUI = "21823712", SCUI = "1341953", SDUI = NA, SAB = "LKDHDS", TTY = "IN", CODE = "139433", STR = "Pramlintide", SRL = "0", SUPPRESS = "Y", CVF = "4354")
I would like this to have been printed in table form.
When I simply use renderTable({table_name}) on any given one of the tables, the table prints in the main panel how I would like it to. However, when I use eventReactive, name that variable, and renderTable on that variable, that is when the list of table values prints instead. Any ideas?
library(shiny)
ui <- fluidPage(
titlePanel("RxNorm Diabetic Drug Mapping based on Epocrates Classes"),
sidebarLayout(
sidebarPanel(
checkboxGroupInput("drugs", "Drug Class", choices = list("ALPHA GLUCOSIDASE INHIBITORS" = agi, "AMYLIN MIMETICS" = pramlintide, "BIGUANIDES" = biguanides, "DOPAMINE AGONISTS" = bromocriptine, "DPP4 INHIBITORS" = dpp4, "GLP1 AGONISTS" = glp1, "INSULINS" = insulins, "MEGLITINIDES" = meglitinides, "SGLT2 INHIBITORS" = sglt2, "SULFONYLUREAS" = sulfonylureas, "THIAZOLIDINEDIONES" = thiazolidinediones)),
actionButton("button", "Retrieve Data")
),
mainPanel(
tableOutput("results")
)
)
)
server <- function(input, output) {
table_reactive <- eventReactive(input$button, {
input$drugs
})
output$results <- renderTable({
table_reactive()
})
}
shinyApp(ui = ui, server = server)
In your choices:
choices = list("ALPHA GLUCOSIDASE INHIBITORS" = agi, "AMYLIN MIMETICS" = pramlintide ...), it's not valid if agi and pramlintide ... are pointing to tables. choices values can only be string.
You shouldn't pass variable as values in checkboxGroupInput. Instead you should pass the table name as string.
To answer your questions:
Please see the demos below:
If your tables are saved as separate variables, you should use sym() and eval_tidy() in rlang package to convert string to varaible.
library(shiny)
library(rlang)
ui <- fluidPage(
fluidRow(
checkboxGroupInput(
inputId = "checkgroup",
label = "Select Table",
choices = list(iris = "iris", mtcars = "mtcars")
),
actionButton(
inputId = "confirm",
label = "Confirm Table(s)"
)
),
fluidRow(
tags$div(id = "tables")
)
)
server <- function(input, output, session) {
observeEvent(input$confirm,{
removeUI(selector = "#tables > *",multiple = TRUE)
req(length(input$checkgroup) > 0)
for(table_name in input$checkgroup){
table_id <- paste0("mytable",table_name)
insertUI(
selector = "#tables",
ui = dataTableOutput(table_id)
)
output[[table_id]] <- renderDataTable(eval_tidy(sym(table_name)))
}
})
}
shinyApp(ui, server)
I'm trying to use awesomeCheckbox from the shinyWidgets package and am running into an issue where I can't check/uncheck the box that a server rendered.
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
awesomeCheckbox(inputId = "checkboxA",
label = "A checkbox",
value = TRUE),
uiOutput("checkboxB"),
uiOutput("FS1")
)
server <- function(input, output) {
output$checkboxB <- renderUI({
awesomeCheckbox(inputId = "checkboxB",
label = "B checkbox",
value = TRUE)
})
output[[paste0("FS", 1)]] <- renderUI({
awesomeCheckbox(inputId = paste0("FS", 1),label = "FS", value = FALSE)
})
}
shinyApp(ui= ui, server=server)
I need this piece of code as part of a larger Shiny App where checkboxes are generated dynamically in the server (hence the weird paste0 naming).
I've checked my version of R and have tried using both Chrome and Safari but can't seem to get the FS checkbox to check/uncheck. I also can't seem to find anything out of the ordinary when I use "Inspect element" in my browser.
You have outputids checkboxB and FS1 respectively already rendered once, yet you create other components with the same names, hence they wont work, change the names as you cant have duplicate divs like so:
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
awesomeCheckbox(inputId = "checkboxA",label = "A checkbox",value = TRUE),
uiOutput("checkboxB"),
uiOutput("FS1")
)
server <- function(input, output) {
output$checkboxB <- renderUI({
awesomeCheckbox(inputId = "checkboxBx",label = "B checkbox", value = TRUE)
})
output[[paste0("FS", 1)]] <- renderUI({
awesomeCheckbox(inputId = paste0("FSx", 1),label = "FS", value = FALSE)
})
}
shinyApp(ui= ui, server=server)
I'm working on a quiz type program in Shiny, which needs to have radio buttons that update with answers from a table. I have the code below, but the radio buttons don't update, and the answers remain 2, 3, 4, 5, and 6 despite changing questions. Any ideas on why this might be happening, and what would fix this?
library(shiny)
ui <- fluidPage(
selectInput("numberchoice",label = "Choose an image", choices = c(1:6), selected = 1)
,
imageOutput("image")
,
radioButtons("answerchoice", "Answers", choices = c(2:6))
)
server <- function(input,output,session) {
answers <- read.csv("~/Answers.csv")
questions <- read.csv("~/Answers.csv")
output$image <- renderImage(list(src = paste("~",".png", sep = "")
,contentType = "image/png", alt = "Face"),deleteFile = FALSE)
eventReactive(input$numberchoice,{updateRadioButtons(session,"answerchoice",choices = questions[input$numberchoice,2:6])})
}
shinyApp(ui = ui, server = server)
Try replacing eventReactive with observeEvent. The following code works for me.
library(shiny)
ui <- fluidPage(
selectInput("numberchoice", label = "Choose an image", choices = 1:6, selected = 1),
radioButtons("answerchoice", "Answers", choices = 1:6 )
)
server <- function(input, output, session) {
observeEvent(input$numberchoice,{
updateRadioButtons(session, "answerchoice",
choices = letters[1:input$numberchoice])})
}
shinyApp(ui = ui, server = server)
It seems like eventReactive didn't trigger so updateRadioButtons was not the problem.