r - 如何在闪亮的modalDialog中呈现不同的表格
问题描述
当我渲染表 mtcars 时,我无法回到我通常的主页。所以我想在我的弹出窗口中选择一个数据框,在显示后我想关闭我的窗口以便我回到我的主页。如何在下面的代码中实现这个想法
library(shiny)
library(ggplot2)
diamonds <- diamonds
mtcars <- mtcars
shinyApp(
ui = basicPage(
actionButton("show", "Show modal dialog"),
verbatimTextOutput("dataInfo")
),
server = function(input, output) {
vals <- reactiveValues(data = NULL)
dataModal <- function(failed = FALSE) {
modalDialog(
textInput("dataset", "Choose data set",
placeholder = 'Try "mtcars" or "diamonds"'
),
span('(Try the name of a valid data object like "mtcars" or "diamonds", ',
'then a name of a non-existent object like "abc")'),
if (failed)
div(tags$b("Invalid name of data object", style = "color: red;")),
footer = tagList(
modalButton("Cancel"),
actionButton("ok", "OK")
)
)
}
observeEvent(input$show, {
showModal(dataModal())
observeEvent(input$ok, {
if (!is.null(input$dataset) && nzchar(input$dataset) &&
exists(input$dataset) && is.data.frame(get(input$dataset))) {
vals$data <- get(input$dataset)
output$dataInfo <- renderPrint({
if (is.null(vals$data))
"No data selected"
else
summary(vals$data)
})
removeModal()
} else {
showModal(dataModal(failed = TRUE))
}
})
output$dataInfo <- renderPrint({
if (is.null(vals$data))
"No data selected"
else
summary(vals$data)
})
})
}
)
如何在 diamonds 和 mtcars 之间进行选择,以便弹出一个表格,关闭对话框后我回到我的主页。
解决方案
所以我可以做到。有没有更聪明的方法?如何在更大的弹出窗口中显示数据框。
shinyApp(
ui = basicPage(
actionButton("show", "Show modal dialog")
#verbatimTextOutput("dataInfo"),
),
server = function(input, output) {
vals <- reactiveValues(data = NULL)
dataModal <- function(failed = FALSE) {
modalDialog(
textInput("dataset", "Choose data set",
placeholder = 'Try "mtcars" or "abc"'
),
span('(Try the name of a valid data object like "mtcars" or "diamonds", ',
'then a name of a non-existent object like "abc")'),
if (failed)
div(tags$b("Invalid name of data object", style = "color: red;")),
footer = tagList(
modalButton("Cancel"),
actionButton("ok", "OK")
)
)
}
dataModal2 <- function() {
modalDialog(
dataTableOutput("table"),
footer = tagList(
modalButton("Cancel"),
actionButton("ok2", "OK")
)
)
}
observeEvent(input$show, {
showModal(dataModal())
#showModal(modalDialog(rHandsontableOutput("hot")))
})
observeEvent(input$ok, {
if (!is.null(input$dataset) && nzchar(input$dataset) &&
exists(input$dataset) && is.data.frame(get(input$dataset))) {
vals$data <- get(input$dataset)
output$table <- renderDataTable(summary(vals$data))
showModal(dataModal2())
} else {
showModal(dataModal(failed = TRUE))
}
})
observeEvent(input$ok2, {
removeModal()
})
}
)
推荐阅读
- javascript - 数组包含的元素少于其长度。为什么?
- sql - 有没有办法在 Toad 中安排作业,使其每 3 小时返回一次计数?
- java - 倒转阿拉伯语单词
- vba - 尝试实例化自定义类时出现“未设置对象变量”(VBA 7.1)
- javascript - 无法从 XML 文件中获取数据
- git - 将 id_rsa.pub 密钥添加到 bitbucket 后,我无法 git clone ssh 协议存储库
- web-audio-api - Web Audio API 振荡器不会发出任何声音
- javascript - 将 Async/Await 与 Promise.all 与 for 循环一起使用
- vb.net - 连接到 Oracle 10g
- swift - SwiftUI - 如何在点击时更改按钮的图像?