【问题标题】:R shinyBS popup windowR闪亮BS弹出窗口
【发布时间】:2017-02-27 15:43:40
【问题描述】:

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

我面临的问题是我无法将每封电子邮件引用到该特定模式窗口,并且我的更新查询会更新数据库中的所有记录。有没有办法将所有被点击的记录详细信息传递到模态窗口中??

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

我正在附加 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

服务器.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,"');"))
    })
  })
  
}

【问题讨论】:

  • 看看我的例子,原来的问题不是很感谢stackoverflow.com/questions/38575655/…
  • 嘿,非常感谢。它解决了一些问题,但我仍然无法将数据从父窗口传递到模式窗口。 selectedRow() 只发送被点击的按钮编号。有没有办法将数据也传递到模态窗口?
  • 您的 a 变量应该是反应式,在您上面的代码中,写入 DB 的 observe 语句不知道 a 是什么。尝试对数据使用 reactiveValuesreactive 表达式。此外,使用观察者处理大量数据使用也不是很明智
  • 您好,感谢您的快速回复。欣赏它。我已将代码更改为类似于您建议的示例的模型。所以我没有对数据使用反应式表达,也没有使用 Observer。问题是点击数据表只返回button_1,不返回行数据。如何从按钮按下获取行数据?

标签: shiny shinydashboard shinybs


【解决方案1】:

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

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)

【讨论】:

  • 嘿,谢谢,正是我想要的。但是您的 dataRow() 的 eventReactive 并不总是有效。我将其转换为数据框以提取查询的特定值,但它仅有时会生成输出。不知道为什么。我想我必须弄清楚这一点。无论如何感谢所有的帮助。欣赏它。
  • 它确实有效,但您需要每次都重置对该行的点击。因为它会记住它
  • 作为问题的扩展,我现在正在使用相同的闪亮输入功能创建一个复选框输入。该行显示为 Check = shinyInput(checkboxInput,label = "Check", nrow(a), 'box_', value = FALSE) 。复选框已创建。有没有办法跟踪这个复选框的点击事件?操作按钮的点击事件在这里不起作用。
  • 您可能应该像以前一样提出一个新问题并提供示例,因为这里越来越混乱
  • 好的,看起来差不多。我在这里发布了一个新问题。 stackoverflow.com/questions/40372745/…提前感谢您的帮助。
猜你喜欢
  • 2018-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-23
  • 1970-01-01
  • 2017-01-10
  • 2017-12-10
  • 2016-09-23
相关资源
最近更新 更多