r - 如何使用操作按钮在 R Shiny 中显示和隐藏表格输出?
问题描述
在下面的简单 App 代码中,我在 Shiny 模态对话框中生成了一个用户输入表(或矩阵)。单击“修改”操作按钮拉出一个默认用户输入表,用户可以在其中修改默认值、插入/删除输入列等。“显示”操作按钮table2
在主页上拉出,“隐藏”隐藏同一个表. (您可以忽略table1
模态框中出现的那个,它暂时存在用于测试目的,稍后将被删除)。“重置”按钮将表格恢复为默认表格。
问题是“显示”和“隐藏”只工作一次。此外,在修改输入表(或矩阵)后,单击“修改”会拉出默认表,而不是最近修改的表。
那么,我将如何修改下面的内容,以便(i)单击“显示”和“隐藏”分别重复显示和隐藏最近修改的表(确定也有一个组合显示/隐藏按钮,使用 shinyjstoggle
函数,我玩弄了),(ii)在第一次调用应用程序时单击“修改”会拉出默认表(就像当前所做的那样),但随后单击“修改”会拉出最近修改的表,以及(iii)单击在没有先修改表格的情况下“显示”拉出默认表格?
MWE代码:
library(shiny)
library(shinyMatrix)
library(shinyjs)
matrix3Input <- function(x){
matrixInput(x,
label = 'Series terms:',
value = matrix(c(1,24,0,1),4,1,dimnames=list(c("A","B","C","D"),NULL)),
rows = list(extend = FALSE,names = TRUE),
cols = list(extend = TRUE,names = TRUE,editableNames = TRUE,delete = TRUE),
class = "numeric") # close matrix input
} # close function
ui <- fluidPage(
useShinyjs(),
titlePanel("Inputs"),
fluidRow(actionButton("modify","Modify"),
actionButton("show","Show"),
actionButton("hide","Hide"),
actionButton("reset","Reset"),
tableOutput("table2")
) # close fluid row
) # close fluid page
server <- function(input, output, session) {
observeEvent(input$modify,{showModal(modalDialog(
matrix3Input("matrix"),
tableOutput("table1"))
)})
output$table1 <- renderTable(input$matrix, rownames = TRUE)
observeEvent(input$show,{
tableOutput("table2")
output$table2 <- renderTable(input$matrix, rownames = TRUE)
})
observeEvent(input$hide,{hide("table2")})
observeEvent(input$reset,{
tableOutput("table2")
output$table2 <- renderTable(input$matrix, rownames = TRUE)
})
} # close server
shinyApp(ui, server)
解决方案
我认为这应该涵盖所有不同的场景。
我已经习惯reactiveValues
了保存matrix3Input
和矩阵。
library(shiny)
library(shinyMatrix)
library(shinyjs)
default_mat <- matrix(c(1,24,0,1),4,1,dimnames=list(c("A","B","C","D"),NULL))
matrix3Input <- function(x, default_mat){
matrixInput(x,
label = 'Series terms:',
value = default_mat,
rows = list(extend = FALSE,names = TRUE),
cols = list(extend = TRUE,names = TRUE,editableNames = TRUE,delete = TRUE),
class = "numeric") # close matrix input
} # close function
ui <- fluidPage(
useShinyjs(),
titlePanel("Inputs"),
fluidRow(actionButton("modify","Modify"),
actionButton("show","Show"),
actionButton("hide","Hide"),
actionButton("reset","Reset"),
tableOutput("table2")
) # close fluid row
) # close fluid page
server <- function(input, output, session) {
rv <- reactiveValues(mat = matrix3Input("matrix", default_mat), input = default_mat)
hide("table2")
observeEvent(input$modify,{
showModal(modalDialog(
rv$mat,
tableOutput("table1"))
)
hide("table2")
})
output$table1 <- renderTable({
rv$mat <- matrix3Input("matrix", input$matrix)
rv$input <- input$matrix
input$matrix
}, rownames = TRUE)
observeEvent(input$show,{
show("table2")
})
observeEvent(input$hide, hide("table2"))
observeEvent(input$reset,{
hide("table2")
rv$input <- default_mat
rv$mat <- matrix3Input("matrix", default_mat)
})
output$table2 <- renderTable({
rv$input
}, rownames = TRUE)
} # close server
shinyApp(ui, server)
推荐阅读
- java - Heroku - Java Maven 项目解聚 - 完成 git push heroku master 后出现应用程序错误
- jquery - 带有复选框的多选选项使用jquery选择和取消选择选项
- android - JSONException while using last.fm API in android
- android - Fresco:如何在下载前获取图像纵横比?
- sql-server - 如何在没有 Management Studio 的情况下在 MSSQL 中创建存储过程?
- azure - 限制同时运行的 azure 函数的数量
- javascript - Angular 6绑定树对象与html元素
- php - 在 Heroku 连接到 RDS MySQL 时出现连接超时错误
- sql-server - SQL 根据唯一值更新
- excel - 在公式中重复相同的函数会增加计算时间吗?