我有一个要保存书签URL的表。到目前为止,当您单击按钮时,它会打开模态。但是,一旦有了第二条记录并单击该按钮,它就不会打开模态。同样,当模态打开时,它现在具有href。如何清除它并仅显示URL?
0
答案 0 :(得分:1)
这就是我想你所追求的:
styles.css
我将您的呼叫转到widget/
,您要求该功能在每行创建10个操作按钮。另外,我更改了library(shiny)
library(RSQLite)
library(data.table)
library(DT)
library(dplyr)
library(shinyjs)
ui <- function(request) {
fluidPage(
useShinyjs(),
DT::dataTableOutput("x1"),
column(
12,
column(3, tags$div(title="forecast", numericInput("budget_input", label = ("Total Forecast"), value = 2))),
column(2, textInput(inputId = "description", label = "Bookmark description", placeholder = "Data Summary")),
column(2, bookmarkButton(id="bookmarkBtn"))),
column(2, actionButton("opt_run", "Run")),
DT::dataTableOutput("urlTable", width = "100%"),
tags$style(type='text/css', "#bookmarkBtn { width:100%; margin-top: 25px;}")
)
}
server <- function(input, output, session) {
con <- dbConnect(RSQLite::SQLite(), "bookmarks.db", overwrite = FALSE)
myBookmarks <- reactiveValues(urlDF = NULL)
observeEvent(input$bookmarkBtn, {
session$doBookmark()
})
observeEvent(input$opt_run, {
cat('HJE')
})
df <- data.table(Channel = c("A", "B","C"),
Current = c("2000", "3000","4000"),
Modified = c("2500", "3500","3000"),
New_Membership = c("450", "650","700"))
output$x1 <- DT::renderDataTable({
input$opt_run
req(input$budget_input)
isolate({
datatable(
df %>% mutate(Current = as.numeric(Current)*(input$budget_input)), selection = 'none', editable = TRUE
)
})
}, server = FALSE)
if(dbExistsTable(con, "Bookmarks")){
tmpUrlDF <- data.table(dbReadTable(con, "Bookmarks"))
myBookmarks$urlDF <- tmpUrlDF[, Timestamp := as.POSIXct(Timestamp, origin="1970-01-01 00:00")]
} else {
myBookmarks$urlDF <- NULL
}
observe({
toExclude <- c("bookmarkBtn", "description", "urlTable_cell_clicked", "urlTable_rows_all", "urlTable_rows_current", "urlTable_rows_selected", "urlTable_search", "urlTable_state", "urlTable_row_last_clicked")
if(!is.null(myBookmarks$urlDF)){
shareBtnExclude <- paste0("shareBtn", seq_len(nrow(myBookmarks$urlDF)))
toExclude <- c(toExclude, shareBtnExclude)
}
delayExclude <- grep("delay", names(input), value = TRUE)
if(length(delayExclude) > 0){
toExclude <- c(toExclude, delayExclude)
}
setBookmarkExclude(toExclude)
})
session$onSessionEnded(function() {
tmpUrlDF <- isolate({myBookmarks$urlDF})
if(!is.null(tmpUrlDF)){
dbWriteTable(con, "Bookmarks", tmpUrlDF, overwrite = TRUE)
}
dbDisconnect(con)
})
onBookmarked(fun=function(url){
if(!url %in% myBookmarks$urlDF$Link){
if(is.null(myBookmarks$urlDF)){
myBookmarks$urlDF <-
unique(
data.table(
Description = input$description,
Link = paste0("<a href='", url, "'>", url, "</a>"),
Share = as.character(actionButton(inputId=paste0("shareBtn", 1), label = "Assessment", onclick = sprintf('Shiny.setInputValue("shareBtn1", "%s", {priority: "event"});', url))),
Timestamp = Sys.time(),
Session = session$token,
User = Sys.getenv("USERNAME")
),
by = "Link"
)
} else {
myBookmarks$urlDF <-
unique(rbindlist(list(
myBookmarks$urlDF,
data.table(
Description = input$description,
Link = paste0("<a href='", url, "'>", url, "</a>"),
Share = as.character(actionButton(inputId=paste0("shareBtn", nrow(myBookmarks$urlDF)+1), label = "Assessment", onclick = sprintf('Shiny.setInputValue("%s", "%s", {priority: "event"});', paste0("shareBtn", nrow(myBookmarks$urlDF)+1), url))),
Timestamp = Sys.time(),
Session = session$token,
User = Sys.getenv("USERNAME")
)
)), by = "Link")
}
}
})
output$urlTable = DT::renderDataTable({
req(myBookmarks$urlDF)
myBookmarks$urlDF[User %in% Sys.getenv("USERNAME")]
}, escape=FALSE, selection = 'none')
observeEvent(lapply(paste0("shareBtn", seq_len(nrow(req(myBookmarks$urlDF)))), function(x) input[[x]]), {
req(myBookmarks$urlDF)
delay(100, {req(input[[paste0("shareBtn", input$urlTable_cell_clicked$row)]])
showModal(urlModal(
input[[paste0("shareBtn", input$urlTable_cell_clicked$row)]],
title = paste("You have selected row", input$urlTable_cell_clicked$row)
))}
)
}, ignoreInit = TRUE)
}
enableBookmarking(store = "url")
shinyApp(ui, server)
参数以直接传递网址。
说实话,我不认为将这些按钮添加到每个数据表行是一个不错的选择,因为您必须跟踪要通过shinyInput
从书签中排除的动态生成的输入(这不会似乎效果不佳)。
编辑:将排除部分放在单独的onclick
中,而不是
setBookmarkExclude
函数似乎可以解决这种情况。
尽管如此,this对于创建由动态创建的按钮触发的观察者非常有用。
单击链接后将URL直接复制到剪贴板的解决方案会更优雅,但应该在另一个问题中加以解决。