首页 > 解决方案 > 如何在 R Shiny 中将起始页设置为空?

问题描述

我想在我的 R 闪亮应用程序中将起始页面设置为空。我的意思是起始页不应该显示任何东西,除了搜索栏。然后,一旦在搜索栏中插入任何值,结果就会显示出来。我怎样才能做到这一点?我的代码在这里:

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!" })
  }
)


标签: rshiny

解决方案


您可以编写自己的搜索功能。使用mtcars数据集 -

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)
    })
  }
)

推荐阅读