4

In there is the amazing convenience of putting s into the menuItem(menuSubItems()) portion of the dashboardSidebar(). But I want the several elements of my UI and Server coded into modules so I can adhere to the framework... and I'm not seeing a clear way to do this without creating multiple UI functions for a single module. I've seen the shinydashboard golem example on github and it's too simple of an example that doesn't help.

For example, Is there a way I can do this?

In a module format:

 library(shiny)
 library(shinydashboard)

 ###   The Sidebar Menu with a Widget Subitem
 mod_myAppSidebar_ui<-function(id) {
      ns <- NS(id)
      tagList(menuItem("Attributes", tabName="ourdata",
               textInput("textSearch","SQL Search String", value = "")))
 }

 ###   The Dashboard Body output
 mod_myAppBody_ui<-function(id) {
      ns <- NS(id)
      tagList(box(shiny::dataTableOutput(outputId = "OutputData")))
 }

 mod_myApp_server<-function(input, output, session) {
        ns <- session$ns
        output$OutputData<-shiny::renderDataTable({
              somedata=data.frame(Rows=letters,Indexes=1:length(letters))
              somedata[grepl(tolower(input$textSearch),somedata$Rows),]
              })
 }

 ###   DashboardPage requires separate arguments for the UI elements
 ui <- dashboardPage(header = dashboardHeader(title = "Rosetta"),
                     sidebar = dashboardSidebar(mod_myAppSidebar_ui("MySearch")),
                     body = dashboardBody(mod_myAppBody_ui("MySearch")))

 server <- function(input, output, session) {
           callModule(mod_myApp_server, "MySearch")
 }

 shinyApp(ui,server)

Is there any way to make this kind of thing work? The widget isn't showing up, likely because I don't think the modular framework allows for me to make two different UI elements for one piece of functionality.

quickreaction
  • 613
  • 4
  • 16

1 Answers1

4

Alright, so I got this working... surprisingly didn't take too much. I don't know if the complexity of my app will break this, but for anyone who was hoping to do this, maybe this is helpful:

library(shiny)
library(shinydashboard)
library(DT)

mod_myAppSidebar_ui<-function(id) {
  ns <- NS(id)
  tagList(menuItem("Attributes", tabName="ourdata",
                   textInput(ns("textSearch"),"SQL Search String", value = ""),
                   actionButton(ns("go"),label = "Search")))
}

mod_myAppBody_ui<-function(id) {
  ns <- NS(id)
  tagList(fluidRow(title = "Data Selected",
                   box(DT::dataTableOutput(outputId = ns("OutputData")))))
}

mod_myApp_server<-function(input, output, session, r) {
  ns <- session$ns

  observeEvent( input$go , {
    r$textSearch<-input$textSearch
    print(r$textSearch)
    somedata=data.frame(Rows=letters,Indexes=1:length(letters))
    r$chooseData<-somedata[grepl(tolower(input$textSearch),somedata$Rows),]
  })

  output$OutputData<-DT::renderDataTable(r$chooseData)

}

ui <- dashboardPage(header = dashboardHeader(title = "Rosetta"),
                    sidebar = dashboardSidebar(mod_myAppSidebar_ui("MySearch")),
                    body = dashboardBody(mod_myAppBody_ui("MySearch")))

server <- function(input, output, session) {
  r<-reactiveValues()
  callModule(mod_myApp_server, "MySearch", r)
}

shinyApp(ui,server)
quickreaction
  • 613
  • 4
  • 16