hi i im trying to have the 'choices' in my pickerInput be dependent on what the user types in the search bar above. im using a spotify r package and if you search a certain artist the api returns a table of artists of the similar name and you need to choose which artist you want. anyways i want that table to go into the pickerInput and i can't seem to get it work.
ui <- dashboardPage(skin = "green",
dashboardHeader(title = "Lyric Prediction"),
dashboardSidebar(
sidebarMenu(
menuItem("Overview", tabName = "Overview", icon = icon("search")),
menuItem("Analysis", tabName = "Analysis", icon = icon("bar-chart-o"))
)
),
dashboardBody(
tags$head(
tags$style(HTML(".fa { font-size: 18px; }"))
),
tabItems(
# First tab content
tabItem(tabName = "Overview",
fluidRow(
column(12,
searchInput(
inputId = "search", label = "Search Artist on Spotify",
placeholder = "Search",
btnSearch = icon("search"),
btnReset = icon("remove"),
width = "500px"
)
), align = "center"
),
#HERE - how can i have pickerInput take in the output
#of "res" from the server?
fluidRow(pickerInput(choices = "res")
)),
)
)
server <- function(input, output) {
#function to take search input
#output list of possible artists
output$res <- renderTable({
#Rspotify
possibleArtists <- searchArtist(input$search,token=my_oauth)
possibleArtists <- as_tibble(possibleArtists)
myCols <- c("display_name","id")
colNums <- match(myCols,names(possibleArtists))
possibleArtists <- possibleArtists %>%
select(colNums)
possibleArtists
})
}
shinyApp(ui, server)
Use update method in an observeEvent like this:
observeEvent(input$search, {
#Rspotify
possibleArtists <- searchArtist(input$search,token=my_oauth)
possibleArtists <- as_tibble(possibleArtists)
myCols <- c("display_name","id")
colNums <- match(myCols,names(possibleArtists))
possibleArtists <- possibleArtists %>%
select(colNums)
updatePickerInput(
session = session,
inputId = "picker",
choices = possibleArtists
)
}, ignoreInit = TRUE)
Full example:
library(shiny)
library(shinydashboard)
library(shinyWidgets)
ui <- dashboardPage(
skin = "green",
dashboardHeader(title = "Lyric Prediction"),
dashboardSidebar(
sidebarMenu(
menuItem("Overview", tabName = "Overview", icon = icon("search")),
menuItem("Analysis", tabName = "Analysis", icon = icon("bar-chart-o"))
)
),
dashboardBody(
tags$head(
tags$style(HTML(".fa { font-size: 18px; }"))
),
tabItems(
# First tab content
tabItem(
tabName = "Overview",
fluidRow(
column(12,
searchInput(
inputId = "search", label = "Search Artist on Spotify",
placeholder = "Search",
btnSearch = icon("search"),
btnReset = icon("remove"),
width = "500px"
)
), align = "center"
),
pickerInput(inputId = "picker", label = "Choose an artist:", choices = NULL)
)
)
)
)
server <- function(input, output, session) {
observeEvent(input$search, {
updatePickerInput(
session = session,
inputId = "picker",
choices = c("The Beatles",
"The Beatles Recovered Band",
"Yesterday - A Tribute To The Beatles",
"The Beatles Revival Band & Orchestra")
)
}, ignoreInit = TRUE)
}
shinyApp(ui, server)
Related
I am trying to clear what ever is written in the text area but looks like it not working. Based on the below applications, when the user clicks on "click" button, the contents (if written) should get cleared. But it is not. Can anyone help me here please........................................
data(mtcars)
library(shiny)
library(shinydashboard)
library(dplyr)
library(DT)
library(shinyjs)
ui <- dashboardPage(
dashboardHeader(title = "Dynamic sidebar"),
dashboardSidebar(sidebarMenu(id = "menu", sidebarMenuOutput("menu"))),
dashboardBody(tabItems
(
tabItem
(tabName = "plots", h2("Dashboard plots"),
fluidRow(column(width = 12, class = "well",
h4("Boxplot"),
plotOutput("bxp")))
),
tabItem(tabName = "dashboard", h2("Dashboard tab content"),
dataTableOutput(outputId = "subdt"),textAreaInput("sd","label1"),textAreaInput("sd1","label2") ,
actionButton("idff","click"))
)
)
)
server <- function(input, output, session) {
output$menu <- renderMenu({
sidebarMenu(
# menuItem("Plots Menu", tabName = "plots", icon = icon("line-chart")),
menuItem("Table Menu", icon = icon("info"),
menuSubItem(
"Dashboard", tabName = "dashboard", icon = icon("calendar")
),
selectInput(
inputId = "mcm", label = "Some label", multiple = TRUE,
choices = unique(mtcars$cyl), selected = unique(mtcars$cyl)
)
)
)
})
observe({
print(input$menu)
})
datsub <- reactive({
mtcars %>%
filter_at(vars("cyl"), all_vars(. %in% input$mcm))
})
output$subdt <- renderDataTable({
datatable(datsub(),selection = if(input$menu == "dashboard"){'single'} else {'none'})
# print(datatable.selection())
})
# datatable(datsub(),selection = if(input$menu == "dashboard"){'single'} else {'none'})
output$bxp <- renderPlot({
hist(rnorm(100))
})
observeEvent(input$idff,{
print("cjec")
shinyjs::reset('sd')
shinyjs::reset('sd1')
})
}
shinyApp(ui, server)
I'd suggest to update the textAreaInput as suggested in the comments. Update the event handler as follows:
observeEvent(input$idff, {
updateTextAreaInput(session = session, inputId = 'sd', value = "")
updateTextAreaInput(session = session, inputId = 'sd1', value = "")
})
I would like to set the starting page empty in my R shiny app. I mean the starting page shouldn't show anything except for the search bar. Then, once any value is plugged in the search bar, the result should show up. How can I achieve this?
My code is here:
library(shiny)
library(shinydashboard)
library(shinyWidgets)
BB = read.table("first.csv", header = TRUE,sep = ";",stringsAsFactors = FALSE)
shinyApp(
ui = dashboardPage(
dashboardHeader(title = "ciliogenics"),
dashboardSidebar(sidebar <- dashboardSidebar(
sidebarMenu(id = "SEARCH",
menuItem("SEARCH", tabName = "SEARCH"),
menuItem("Clinical Signs and Symptoms", tabName = "Clinical Signs and Symptoms"),
menuItem("Diseases", tabName = "Diseases"),
menuItem("Genes", tabName = "Genes"),
menuItem("Classifications and schemas", tabName = "Classification and schemas"),
menuItem("HELP", tabName = "HELP"),
menuItem("Related papers", tabName = "Related papers"),
menuItem("FAQs", tabName = "FAQs")
)
)),
dashboardBody(tabItems(tabItem(tabName = "SEARCH" ,
fluidRow(column(width=6,box(DT::dataTableOutput('myTable'), width=NULL)),
column(width=6,box(textOutput("myText"), width=NULL))))))
),
server = function(input, output, session){
mytbl <- BB
output$myTable <- DT::renderDataTable({DT::datatable(mytbl,
rownames=FALSE)})
output$myText <- renderText({ "The value entered in the seach box should appear here!" })
}
)
You can write your own search function. Using mtcars dataset -
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(DT)
shinyApp(
ui = dashboardPage(
dashboardHeader(title = "ciliogenics"),
dashboardSidebar(sidebar <- dashboardSidebar(
sidebarMenu(id = "SEARCH",
menuItem("SEARCH", tabName = "SEARCH"),
menuItem("Clinical Signs and Symptoms", tabName = "Clinical Signs and Symptoms"),
menuItem("Diseases", tabName = "Diseases"),
menuItem("Genes", tabName = "Genes"),
menuItem("Classifications and schemas", tabName = "Classification and schemas"),
menuItem("HELP", tabName = "HELP"),
menuItem("Related papers", tabName = "Related papers"),
menuItem("FAQs", tabName = "FAQs")
)
)),
dashboardBody(tabItems(tabItem(tabName = "SEARCH" ,
fluidRow(column(width=6,box(DT::dataTableOutput('myTable'), width=NULL)),
column(width=6,box(textOutput("myText"), width=NULL))))))
),
server = function(input, output, session){
rv <- reactiveValues(inds = 0, mytbl = mtcars)
output$myTable <- DT::renderDataTable({DT::datatable(rv$mytbl[rv$inds, ],rownames=FALSE, callback = JS(
"table.on( 'search.dt', function () {",
"Shiny.setInputValue( 'search', table.search() );",
"} );"
))})
output$myText <- renderText({paste0("Searching for ...", input$search)})
observeEvent(input$search, {
rv$inds <- which(rowSums(sapply(rv$mytbl, function(x) grepl(input$search, x))) > 0)
})
}
)
It appears the input value of a selectInput object is not updating. I have inserted one in the sidebar menu. I am using shinyDashboard. here is my code.
header & Sidebar
header <-
dashboardHeader(
title = "REPORT",
tags$li(class = "dropdown",
tags$style(
HTML(
"#import url('//fonts.googleapis.com/css?family=Libre+Baskerville:400,700|Open+Sans:400,700|Montserrat:400,700');"
)
)),
disable = FALSE,
titleWidth = '200'
)
header$children[[3]]$children[[3]] <-
tags$h1("DATABASE",
# align = 'left',
style = "color:#FFFFFF; font-weight: bold; font-family: 'Open Sans','Libre Baskerville',Montserrat, serif;font-size: 23px;")
data_type_list<-c('in vivo','in vitro','pbpk')
siderbar <- dashboardSidebar(
width = 200,
sidebarMenu(
id = 'sidebar',
style = "position: relative; overflow: visible;",
menuItem(
"TK Knowlegebase",
tabName = 'tk',
icon = icon('database'),
badgeColor = "teal",
#radioButtons("tk_data_type", "Select Data Type:",data_type_list)
selectInput('tk_data_type',"Select Data Type",data_type_list, selected=1)
)
)
)
body
body <- dashboardBody(width = 870,
tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "custom.css")
),
#theme = shinythemes::shinytheme("darkly"),
tabItems(
tabItem (
tabName = "tk",
mainPanel(
#in vivo ----
conditionalPanel(
condition ="input.tk_data_type== 'in vivo'",
tags$h2('vivo')
),
# in vitro ----
conditionalPanel(
condition="input.tk_data_type== 'in vitro'",
tags$h2('vitro')
)
,
# pbpk ----
conditionalPanel(
condition="input.tk_data_type== 'pbpk'",
tags$h2('pbpk')
)
)))
server = function(input, output, session) {
observe({input$tk_data_type})
}
ui <- dashboardPage(title = 'ARC Toxkin App', skin = 'purple',
header, siderbar, body)
shiny::shinyApp(ui = ui, server = server)
I even attempted to use observe({input$tk_data_type}) in the server section to no success.
What I obtain is a blank page. what I wish to see is content from dashboardBody() appear.
Thank you for your time
UPDATE
This is a short-term fix solution, thanks to user YBS.
vitro_tabset<-tabsetPanel(
tabPanel("Detailed",
"This is a test"),
tabPanel("Phys-chem",
"This is a test"),
tabPanel("Exploratory",
"This is a test"),
tabPanel("Downloads",
"This is a test")
)
vivo_tabset<-tabsetPanel(
tabPanel("Detailed",
"This is a test"),
tabPanel("Phys-chem",
"This is a test"),
tabPanel("Exploratory",
"This is a test"),
tabPanel("Downloads",
"This is a test")
)
siderbar <- dashboardSidebar(
sidebarMenu(
id = 'sidebar',
menuItem(
"TK Knowlegebase",
tabName = 'tk',
icon = icon('database'),
badgeColor = "teal",
selected = TRUE,
startExpanded = TRUE,
#radioButtons("tk_data_type2", "Select Data Type:",data_type_list),
menuSubItem('vivo', tabName = 'vivo', icon = shiny::icon("angle-double-right"), selected = NULL),
menuSubItem('vitro', tabName = 'vitro', icon = shiny::icon("angle-double-right"), selected = TRUE),
menuSubItem('pbpk', tabName = 'pbpk', icon = shiny::icon("angle-double-right"), selected = NULL)
)
)
)
body <- dashboardBody(width = 870,
tabItems(
tabItem (tabName = "vivo",
vivo_tabset),
tabItem(tabName='vitro',
# in vitro ----
vitro_tabset),
# pbpk ----
tabItem(tabName='pbpk')
It appears that tabItems() and tabItem() don't work well in your program as the sidebarmenu requires minor tweaking. Also, you don't need mainPanel. Try this
data_type_list <- c("in vivo","in vitro","pbpk")
ui <- shinydashboard::dashboardPage(title = "ARC Toxkin App", skin = "purple",
shinydashboard::dashboardHeader(
title = "REPORT" ,
tags$li(class = "dropdown",
tags$style(
HTML(
"#import url('//fonts.googleapis.com/css?family=Libre+Baskerville:400,700|Open+Sans:400,700|Montserrat:400,700');"
)
)),
disable = FALSE,
titleWidth = '200'
),
shinydashboard::dashboardSidebar(width = 220,
useShinyjs(),
sidebarMenu(
id = "tabs",
style = "position: relative; overflow: visible;",
menuItem("TK Knowlegebase", tabName="tk", icon = icon("bar-chart-o"),
menuSubItem("TK Knowlegebase1", tabName = "tk1", icon = icon('database')),
selectInput("tk_data_type", label="Select Data Type", choices = data_type_list, selected=1),
menuSubItem("TK Knowlegebase2", tabName = "tk2", icon = icon('database'))
#radioButtons("tk_data_type2", "Select Data Type:",data_type_list)
)
)
),
shinydashboard::dashboardBody( #width = 870,
tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "custom.css"),
),
#theme = shinythemes::shinytheme("darkly"),
tabItems(
#tabItem(tabName = "tk", headerPanel('First')),
tabItem(tabName = "tk1",
fluidRow(
shinydashboard::box(title = "Graphics Package", width = 12, solidHeader = TRUE, status="info",
## vivo
conditionalPanel(
condition = "input.tk_data_type == 'in vivo'",
tags$h2(' vivo')
),
## in vitro
conditionalPanel(
condition = "input.tk_data_type == 'in vitro'",
tags$h2(' vitro')
),
## pbpk
conditionalPanel(
condition = "input.tk_data_type == 'pbpk'",
tags$h2(' pbpk')
),
verbatimTextOutput("tb1"),
DTOutput("tb2")
))
),
tabItem(tabName = "tk2", headerPanel('Last'), DTOutput("tb3")
#verbatimTextOutput("tb4")
)
)
)
)
server <- function(input, output, session) {
output$tb3 <- renderDT(mtcars)
output$tb2 <- renderDT(iris)
output$tb1 <- renderPrint({input$tk_data_type})
output$tb4 <- renderPrint({input$tk_data_type2})
}
shinyApp(ui, server)
I need a menuItem hidden, when the app is entered into. When a user chooses a certain value, the menuItem has to appear.
I have tried shinyjs functions hidden, and it hides a menuItem, but when using show or toggle, a menuItem doesn't appear.
I've found R shinydashboard - show/hide multiple menuItems based on user input
and came up with this
library(shiny)
library(shinydashboard)
library(shinyjs)
header <- dashboardHeader(title = "APP", titleWidth = 330)
sidebar <- dashboardSidebar(
sidebarMenu(id="tabs",
menuItem("",tabName="default"),
menuItem("Scenarios",tabName = "scenarios", icon = icon("flag")),
uiOutput("recOpt"),
menuItem("Simulation", tabName = "game", icon = icon("gamepad")),
menuItem("Actions", tabName = "actions", icon = icon("folder"),
menuSubItem("Save project", tabName = "save"),
menuSubItem("Open project", tabName = "open")
)
)
)
body <- dashboardBody(
tabItems(
tabItem(tabName = "scenarios",
useShinyjs(),
radioButtons("radio", h3("Radio buttons"),
choices = list("Choice 1" = 1,
"Choice 2" = 2,
"Choice 3" = 3))
)
)
)
ui <- dashboardPage(header, sidebar, body)
server <- function(input, output) {
output$recOpt <- renderUI({
if(input$radio == 2)
menuItem("Options", tabName = "recOpt", icon = icon("bell"),
menuSubItem("No option",tabName="RO_00"),
menuSubItem("Option 1",tabName="RO_01")
)
})
}
shinyApp(ui, server)
It works but the hidden/shown item is not aligned correcty, nor the encoding is correct.
Have any ideas how to make it better?
A little late, but anyway:
Check the shinydashboard capabilities on dynamic content.
This should do it:
library(shiny)
library(shinydashboard)
library(shinyjs)
header <- dashboardHeader(title = "APP", titleWidth = 330)
sidebar <- dashboardSidebar(
sidebarMenu(id="tabs",
menuItem("",tabName="default"),
menuItem("Scenarios",tabName = "scenarios", icon = icon("flag")),
menuItemOutput("recOpt"),
menuItem("Simulation", tabName = "game", icon = icon("gamepad")),
menuItem("Actions", tabName = "actions", icon = icon("folder"),
menuSubItem("Save project", tabName = "save"),
menuSubItem("Open project", tabName = "open")
)
)
)
body <- dashboardBody(
tabItems(
tabItem(tabName = "scenarios",
useShinyjs(),
radioButtons("radio", h3("Radio buttons"),
choices = list("Choice 1" = 1,
"Choice 2" = 2,
"Choice 3" = 3))
)
)
)
ui <- dashboardPage(header, sidebar, body)
server <- function(input, output) {
output$recOpt <- renderMenu({
if(input$radio == 2)
menuItem("Options", tabName = "recOpt", icon = icon("bell"),
menuSubItem("No option",tabName="RO_00"),
menuSubItem("Option 1",tabName="RO_01")
)
})
}
shinyApp(ui, server)
I am trying to build a shiny application using sidebarMenu with menuItems. Currently the menu items are duplicated,
enter image description here
Clicking the first and second menu items are not showing the table or the plot. Only the last two shows the output. How can I modify it to have only two items - 1) Plots Menu, 2) Table Menu (with sub items) and clicking on it show the respective output. Used the mtcars dataset and the code ispasted below
data(mtcars)
ibrary(shiny)
library(shinydashboard)
library(dplyr)
ui <- dashboardPage(
dashboardHeader(title = "Dynamic sidebar"),
dashboardSidebar(
sidebarMenu(
menuItem("Plots Menu", tabName = "plot_page", icon = icon("line-chart")),
menuItem("Table Menu", tabName="intro_page", icon = icon("info"),
selectInput(inputId = "mcm", label = "Some label",
multiple = TRUE, choices = unique(mtcars$cyl), selected = unique(mtcars$cyl)))
),
sidebarMenuOutput("menu")
),
dashboardBody(tabItems(
tabItem(tabName = "plots", h2("Dashboard plots"),
fluidRow(column(width = 12, class = "well",
h4("Boxplot"),
plotOutput("bxp")))
),
tabItem(tabName = "dashboard", h2("Dashboard tab content"),
dataTableOutput(outputId = "subdt"))
)
)
)
server <- function(input, output, session) {
output$menu <- renderMenu({
sidebarMenu(
menuItem("Plots Menu", tabName = "plots", icon = icon("line-chart")),
menuItem("Table Menu", tabName="dashboard", icon = icon("calendar"))
)
})
datsub <- reactive({
mtcars %>%
filter_at(vars("cyl"), all_vars(. %in% input$mcm))
})
output$subdt <- renderDataTable({
datsub()
})
output$bxp <- renderPlot({
hist(rnorm(100))
})
}
shinyApp(ui, server)
I put the code together.
-Ian
data(mtcars)
library(shiny)
library(shinydashboard)
library(dplyr)
ui <- dashboardPage(
dashboardHeader(title = "Dynamic sidebar"),
dashboardSidebar(sidebarMenu(id = "menu", sidebarMenuOutput("menu"))),
dashboardBody(tabItems
(
tabItem
(tabName = "plots", h2("Dashboard plots"),
fluidRow(column(width = 12, class = "well",
h4("Boxplot"),
plotOutput("bxp")))
),
tabItem(tabName = "dashboard", h2("Dashboard tab content"),
dataTableOutput(outputId = "subdt"))
)
)
)
server <- function(input, output, session) {
output$menu <- renderMenu({
sidebarMenu(
menuItem("Plots Menu", tabName = "plots", icon = icon("line-chart")),
menuItem("Table Menu", icon = icon("info"),
menuSubItem(
"Dashboard", tabName = "dashboard", icon = icon("calendar")
),
selectInput(
inputId = "mcm", label = "Some label", multiple = TRUE,
choices = unique(mtcars$cyl), selected = unique(mtcars$cyl)
)
)
)
})
datsub <- reactive({
mtcars %>%
filter_at(vars("cyl"), all_vars(. %in% input$mcm))
})
output$subdt <- renderDataTable({
datsub()
})
output$bxp <- renderPlot({
hist(rnorm(100))
})
}
shinyApp(ui, server)
You have both the standard and reactive sidebar options running in tandem. If you need a reactive sidebar, just put the contents in the server function and call all of it with sidebarMenuOutput in ui.
ui.R
dashboardSidebar(sidebarMenu(id = "menu", sidebarMenuOutput("menu")))
server.R
output$menu <- renderMenu({
sidebarMenu(
menuItem("Plots Menu", tabName = "plots", icon = icon("line-chart")),
menuItem("Table Menu", icon = icon("info"),
menuSubItem(
"Dashboard", tabName = "dashboard", icon = icon("calendar")
),
selectInput(
inputId = "mcm", label = "Some label", multiple = TRUE,
choices = unique(mtcars$cyl), selected = unique(mtcars$cyl)
)
)
)
})