【问题标题】:How to reset an ui in shiny?如何重置闪亮的ui?
【发布时间】:2020-01-27 04:39:42
【问题描述】:

我创建了一个 actionButton RUN demo data 作为应用程序的演示,我想知道如何重置它,以便用户可以开始输入用户数据集。我环顾了reset 按钮,但仍然无法通过。

rm(list=ls())
library(tidyverse)
library(shiny)

# Define UI ----
ui <- fluidPage(
  tabsetPanel(
    #tabPanel-Input
    tabPanel("Input", fluid = TRUE,

             # tab title ----
             titlePanel("Upload data"),

             # sidebar layout with input and output tables ----
             sidebarLayout(

               # sidebar panel for inputs ----
               sidebarPanel(

                 #show ct demo
                 actionButton("runexample", "RUN demo data"),

                 # input1: Select a file ----
                 fileInput("file1", "Count matrix File (.xlsx)",
                           multiple = TRUE,
                           accept = c("text/csv",
                                      "text/comma-separated-values,text/plain",
                                      ".csv")),

                 #input2: select a file ----
                 fileInput("file2", "Manifest File (.xlsx)",
                           multiple = TRUE,
                           accept = c("text/csv",
                                      "text/comma-separated-values,text/plain",
                                      ".csv")),

                 #select column name
                 selectInput("design", "Column name for analysis", " "),

                 #select ref group
                 uiOutput("level0"),

                 #select study group
                 uiOutput("level1"),

                 #action run
                 actionButton("runbutton", "Run"),

                 #comment message
                 p("Click to perform differential gene expression analysis between the selected groups"),

                 #README link
                 uiOutput("README"),

                 #issue report
                 uiOutput("issue")

               ),
               # Main panel for displaying outputs ----
               mainPanel(

                 # Output: Data file ----
                 tableOutput("matrix"),
                 tableOutput("pdat")
               )
             )
    ),

    #tabPanel-Results
    tabPanel("Results", fluid = TRUE,
             # App title ----
             titlePanel("Download results"),

             # Sidebar layout with input and output definitions ----
             sidebarLayout(

               # Sidebar panel for inputs ----
               sidebarPanel(

                 # Input: Choose dataset ----
                 selectInput("results", "Choose a dataset:",
                             choices = c("Results", "Normalized matrix")),

                 # Button
                 downloadButton("downloadData", "Download")

               ),

               # Main panel for displaying outputs ----
               mainPanel(

                 tableOutput("table")

               )

             )             

    ),
    #tabPanel-Plots
    tabPanel("Plots", fluid = TRUE,
             fluidRow(
               column(width = 8,
                      plotOutput("plot1", height = 800,
                                 # Equivalent to: click = clickOpts(id = "plot_click")
                                 click = "plot1_click",
                                 brush = brushOpts(
                                   id = "plot1_brush"
                                 )
                      )
               ),
               column(width = 4,
                      h4("Brushed points"),
                      verbatimTextOutput("brush_info")
               )
             )
    )
  )
)


# Define Server ----
server <- function(input, output, session) {

  #tabPanel-Input

  ###demo data
  ####count
  set.seed(123)
  ctdemo<- t(rmultinom(1000, size = 50, prob = c(rep(0.4, 4), rep(0.6, 4))))

  ####manifest
  pdemo<-data.frame(Samples=paste0("Sample", 1:8),
                    Treatment=rep(c("DrugA", "DrugB"), each=4))
  ###display demo count matrix
  observeEvent(input$runexample, {
    output$matrix <- renderTable({
      head(ctdemo, 10)
    })
    output$pdat <- renderTable({
      head(pdemo, 10)
    })

    observe({
      updateSelectInput(session, "design", choices="Treatment")
    })

    output$level0 <- renderUI({
      selectInput("ref0", "Reference group", "DrugA")
    })


    output$level1 <- renderUI({
      selectInput("ref1", "Study group", "DrugB")
    })
  })
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    实际上你已经完成了大部分工作(我没有复制粘贴完整的代码,它很长,解决方案很短)。

    首先,使用actionButton("reset", "Reset"), 在ui 部分中创建“重置”按钮(我将它放在按钮runexample 之后)。

    然后,将server 部分的几乎所有代码放入由reset 触发的observeEvent 中(将这段代码放在server 部分的末尾):

      observeEvent(input$reset, {
        output$matrix <- renderTable(NULL)
        output$pdat <- renderTable(NULL)
        observe({
          updateSelectInput(session, "design")
        })
        output$level0 <- renderUI(NULL)
        output$level1 <- renderUI(NULL)
      })
    

    就是这样!

    【讨论】:

      猜你喜欢
      • 2020-10-29
      • 2013-08-25
      • 1970-01-01
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-24
      相关资源
      最近更新 更多