gpt4 book ai didi

datatable - Shiny 的模块命名空间问题 : action buttons in data table

转载 作者:行者123 更新时间:2023-12-04 17:06:17 27 4
gpt4 key购买 nike

我在数据表中添加了一个“删除”按钮来删除行。它适用于 Shiny 的应用程序,但不适用于 Shiny 的模块。我很确定这是一个命名空间问题,在 ShinyInput 函数或 getRemoveButton 函数的某个地方,但我现在不知道如何修复它。

library(DT)
library(dplyr)
library(purrr)
library(shiny)

getRemoveButton <- function(name, idS = "", lab = "Pit") {
if (stringr::str_length(idS) > 0) idS <- paste0(idS, "-")
ret <- shinyInput(actionButton, name,
'button_', label = "Remove",
onclick = sprintf('Shiny.onInputChange(\"%sremove_button_%s\", this.id)' ,idS, lab))
return(ret)
}


shinyInput <- function(FUN, name, id, ses, ...) {
n <- length(name)
inputs <- character(n)
for (i in seq_len(n)) {
inputs[i] <- as.character(FUN(paste0(id, i), ...))
}
inputs
}



uploadFigUI <- function(id) {

ns <- NS(id)

tagList(
fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput(inputId = ns('files'),
label = 'Select an Image',
multiple = TRUE,
accept=c('image/png', 'image/jpeg')),
DT::dataTableOutput(ns("myTable"))
),
mainPanel(
uiOutput(ns('images'))
)
)
)
)
}

uploadFig <- function(input, output, session) {

ns <- session$ns

files <- eventReactive(input$files, {
req(input$files)
files <- input$files
files$datapath <- gsub("\\\\", "/", files$datapath)
files
})


values <- reactiveValues()

observeEvent(files(), {

if(is.null(values$tab)){
values$tab <- files() %>%
mutate(Remove = getRemoveButton(files()$name, idS = "", lab = "Tab1"))

}else{
tab <- files() %>%
mutate(Remove = getRemoveButton(files()$name, idS = "", lab = "Tab1"))
myTable <- bind_rows(values$tab,tab)
replaceData(proxyTable, myTable, resetPaging = FALSE)
values$tab <- myTable
}

})



output$images <- renderUI({
req(values$tab$datapath)
image_output_list <-
lapply(1:nrow(values$tab),
function(i)
{
imagename = ns(paste0("image", i))
uiOutput(imagename)
})

do.call(tagList, image_output_list)
})

observe({

req(values$tab$datapath)
for (i in 1:nrow(values$tab))
{
print(i)
local({
my_i <- i
imagename = paste0("image", my_i)
img <- knitr::image_uri(values$tab$datapath[my_i])
values$img[[i]] <- img
output[[imagename]] <- renderUI({
req(values$img[[i]])

tags$img(src = img, width = "100%", height= "auto")


})
})
}
})

proxyTable <- DT::dataTableProxy("tab")

output$myTable <- DT::renderDataTable({
req(values$tab)
DT::datatable(values$tab %>%
select(-datapath),
options = list(pageLength = 25,
dom = "rt"),
rownames = FALSE,
escape = FALSE,
editable = FALSE)
})



observeEvent(input$remove_button_Tab1, {
myTable <- values$tab
values$what <- str_replace(input$remove_button_Tab1, "button_", "")
s <- str_replace(input$remove_button_Tab1, "button_", "") %>% as.numeric()
myTable <- myTable[-s, ]
replaceData(proxyTable, myTable, resetPaging = FALSE)
values$tab <- myTable
})


}

shinyApp(
ui = fluidPage(
uploadFigUI("test")
),

server = function(input, output) {

suppfigure <- callModule(uploadFig, "test")

}
)

如果这个“删除”按钮起作用,它应该删除主面板上的行和图片。

最佳答案

第一个问题:代理应该是proxyTable <- DT::dataTableProxy("myTable") .

第二个问题:您必须在删除按钮的 id 前面加上命名空间:

getRemoveButton(files()$name, idS = "test", lab = "Tab1")

这就像这样。

关于datatable - Shiny 的模块命名空间问题 : action buttons in data table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56328500/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com