R shinyBS 弹出窗口 window

R shinyBS popup window

我正在做一个项目,我必须在 shiny 中创建一个表单。我目前在 UI 中有一个数据表,其中包含超链接形式的电子邮件。单击超链接后,模式 window 打开,我有另一个 UI 显示要填写的各个字段。我这里有一个保存按钮,单击该按钮后应该会在后端更新我的数据库。

我面临的问题是我无法将每封电子邮件引用到该特定模式 window 并且我的更新查询更新了数据库中的所有记录。有没有办法把所有被点击的记录详情都传入模态window??

我基本上需要知道的是如何更新我点击并打开弹出窗口window的记录??

我附上 UI.R 和 server.R 以供使用。

enter code here

ui.R

library(shiny)
library(DT)
library(shinyBS)
fluidPage(
         fluidRow(
                  actionButton(inputId = "view",label = "Hi")),
                  #actionButton(inputId =  "savepage1", label = "Save"),
                  DT::dataTableOutput('my_table'),
                  bsModal("FormModal", "My Modal", "",textOutput('mytext'),uiOutput("form1"),
                          actionButton("savepage2","Save"),DT::dataTableOutput("table1"),size = "large")

         )

enter code here

server.R

library(shinyBS)
server <- function(session, input, output){
  
uedata<-c("","Prime","Optimus")  ##add source data here
  
  output$form1<-renderUI({
    tagList(
      column(width=6,selectInput("samplevalue","Select Custom Source*",choices=c("Please select",samplevaluedata))),
      column(width=6,textInput("sampletext",label = "Enter Text",value = NULL,placeholder = NULL)))
  })

  on_click_js = "Shiny.onInputChange('mydata', '%s');
  $('#FormModal').modal('show')"
  
  convert_to_link = function(x) {
    as.character(tags$a(href = "#", onclick = sprintf(on_click_js,x), x))
  }
  
  observeEvent(input$view,{
    session$sendCustomMessage(type = "unbinding_table_elements", "my_table")
    output$my_table <- DT::renderDataTable({
      a=dbGetQuery(hcltcprod,paste0("select name,mobile,email,assignedto from public.tempnew order by 3;"))
      a <- data.frame(a,row.names = NULL)
      a$email <- sapply(a$email,convert_to_link)
      a1 <- datatable(a,
                     escape = F,
                     options = list(paging = FALSE, ordering = FALSE, searching = FALSE, rownames = FALSE,
                                    preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node());}'),
                                    drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')))
                                    
      a1
    })
  })
  
  observeEvent(input$my_table_cell_clicked, {
    print(Sys.time())
  })

  observe({
    if(input$savepage2==0)
      return()
    isolate({
      for(i in 1:nrow(a))
     dbGetQuery(hcltcprod,paste0("update public.tempnew set s_text='",input$samplevalue,"',s_value='",input$sampletext,"' where mobile in ('",a$email,"');"))
    })
  })
  
}

由于您的示例已连接到数据库并且您没有提供示例数据,我将使用 mtcars 数据集。基于 link 中的示例,您可以使用以下方法查看所选数据:

rm(list = ls())
library(DT)
library(shiny)
library(shinyBS)
library(shinyjs)
library(shinydashboard)

# This function will create the buttons for the datatable, they will be unique
shinyInput <- function(FUN, len, id, ...) {inputs <- character(len)
                                           for (i in seq_len(len)) {
                                             inputs[i] <- as.character(FUN(paste0(id, i), ...))}
                                           inputs
}

ui <- dashboardPage(
  dashboardHeader(title = "Simple App"),
  dashboardSidebar(
    sidebarMenu(id = "tabs",
                menuItem("Menu Item 1", tabName = "one", icon = icon("dashboard"))
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "one",h2("Datatable Modal Popup"),
              DT::dataTableOutput('my_table'),uiOutput("popup")
      )
    )
  )
)

server <- function(input, output, session) {
  my_data <- reactive({
    testdata <- mtcars
    as.data.frame(cbind(View = shinyInput(actionButton, nrow(testdata),'button_', label = "View", onclick = 'Shiny.onInputChange(\"select_button\",  this.id)' ),testdata))
  })  
  output$my_table <- DT::renderDataTable(my_data(),selection = 'single',options = list(searching = FALSE,pageLength = 10),server = FALSE, escape = FALSE,rownames= FALSE)

  # Here I created a reactive to save which row was clicked which can be stored for further analysis
  SelectedRow <- eventReactive(input$select_button,{
    as.numeric(strsplit(input$select_button, "_")[[1]][2])
  })

  # This is needed so that the button is clicked once for modal to show, a bug reported here
  # https://github.com/ebailey78/shinyBS/issues/57
  observeEvent(input$select_button, {
    toggleModal(session, "modalExample", "open")
  })

  DataRow <- eventReactive(input$select_button,{
    my_data()[SelectedRow(),2:ncol(my_data())]
  })

  output$popup <- renderUI({
    bsModal("modalExample", paste0("Data for Row Number: ",SelectedRow()), "", size = "large",
            column(12,                   
                   DT::renderDataTable(DataRow())

            )
    )
  })

}

shinyApp(ui, server)