r - 如何创建一个弹出窗口供用户在 R Shiny 中输入信息?
问题描述
这是一个例子。我想要的是用户可以通过DEMO
按钮多次运行演示。然而,当他们点击Browse
上传本地数据(不是reset
我在示例中演示的按钮)时,我会弹出一个窗口让用户在两个输入框中输入他们的姓名和状态。在下面的示例中,单击RESET
,将启动一个弹出框(可能不是正确的方式)。
library(shiny)
library(shinyWidgets)
if (interactive()) {
# Display an important message that can be dismissed only by clicking the
# dismiss button.
shinyApp(
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
actionBttn("runexample", "DEMO", style="simple", size="sm", color = "primary"),
# input1: Select a file ----
fileInput("file1", "Count matrix File (.xlsx)",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
#action run
actionBttn("runbutton", "GO", style="simple", size="sm", color = "primary"),
actionBttn("reset", "RESET", style="simple", size="sm", color = "warning"),
verbatimTextOutput(outputId = "reset"),
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Data file ----
span(textOutput("nrows"),style="color:blue"),
span(textOutput("ncols"),style="color:blue"),
tableOutput("matrix"),
)
)
)
)
),
server = function(input, output, session) {
###display demo count matrix
observeEvent(input$runexample, {
#ngenes
output$nrows <- renderText({paste("Number of genes: ", dim(mtcars)[1], " [First 10 rows displayed]")})
#nsamples
output$ncols<- renderText({paste("Number of genes: ", (dim(mtcars)[2]), " [First 10 rows displayed]")})
#display 10rows count matrix
output$matrix <- renderTable({
mtcars
})
}
)
observeEvent(input$reset, {
inputSweetAlert(
session = session, inputId = "mytext", input = "text",
title = "This is a free program, please leave your email:"
)
})
output$text <- renderPrint(input$mytext)
}
)
}
解决方案
这是一个使用 提供的模态对话框选项的示例shiny
。我将您的示例精简为重要的部分:
if (interactive()) {
shinyApp(
ui <- fluidPage(
actionBttn("reset", "RESET", style="simple", size="sm", color = "warning"),
verbatimTextOutput(outputId = "text")
),
server = function(input, output, session) {
l <- reactiveValues()
observeEvent(input$reset, {
# display a modal dialog with a header, textinput and action buttons
showModal(modalDialog(
tags$h2('Please enter your personal information'),
textInput('name', 'Name'),
textInput('state', 'State'),
footer=tagList(
actionButton('submit', 'Submit'),
modalButton('cancel')
)
))
})
# only store the information if the user clicks submit
observeEvent(input$submit, {
removeModal()
l$name <- input$name
l$state <- input$state
})
# display whatever is listed in l
output$text <- renderPrint({
if (is.null(l$name)) return(NULL)
paste('Name:', l$name, 'and state:', l$state)
})
}
)
}
推荐阅读
- android - ConstraintLayout内TextView的动态点
- javascript - How can I seperate data in a single form into JSON array?
- ada - 如何为记录类型定义“+”
- excel - R1C1 Notation Macro - 仅搜索数值
- angular - 如何根据第一个或两个第一个数字显示信用卡徽标
- java - .noSuchElementException 询问用户输入时
- algorithm - 在每个语句的末尾给出次数。对于循环头的情况,只需给出将被检查的次数
- vue.js - 如何多次添加vue.js组件@click?
- css - 为什么高像素密度的小屏幕会呈现错误的媒体查询?
- reactjs - 打字稿,样式组件错误:TS2769:没有重载匹配此调用。重载 1 of 2