Reputation: 1233
I am trying to create an action button in a Shiny DataTable that when clicked links to a specific Amazon product. I modeled my code after R Shiny: Handle Action Buttons in Data Table. When the button is clicked, I need the user to navigate to a specific Amazon product page based on the ASIN in the data.table row.
filteredData <- data.frame(
Name = c('Dilbert', 'Alice', 'Wally', 'Ashok', 'Dogbert'),
ASIN = c("B06Y4VRZTB",
"B06Y4WGPBB",
"B06Y4J9Z9V",
"B06Y4V169H",
"B06Y4TF1D1"),
stringsAsFactors = FALSE,
row.names = 1:5)
shinyInput <- function(FUN, len, id, ...) {
inputs <- character(len)
for (i in seq_len(len)) {
inputs[i] <- as.character(FUN(paste0(id, i), ...))
}
print(inputs)
inputs
}
df <- reactiveValues(data = data.frame(
filteredData %>% mutate(Amazon.Button = shinyInput(actionButton,
nrow(filteredData),
'button_',
label = "Amazon",
onclick = paste0("window.open('https://",
AmazonSiteLink,
"/gp/product/",
ASIN,
"/ref=as_li_tl?ie=UTF8&tag=",
AssociateTag, "')")))
), escape=FALSE)
The issue is that every button ends up with the same URL picking up only the first ASIN value. I want the ASIN value to be what is in the ASIN value for the row.
https://www.amazon.com/gp/product/B06Y4VRZTB/ref=as_li_tl?ie=UTF8&tag=my0000-00'
I tried to create the button with every row, but get other errors such at, "Warning: Error in mutate_impl: Column Amazon.Button
must be length 5 (the number of rows) or one, not 3"
df <- reactiveValues(data = data.frame(
filteredData %>% mutate(Amazon.Button = actionButton(inputId = paste0('button'),
label = "Amazon",
onclick = paste0("window.open('https://",
AmazonSiteLink,
"/gp/product/",
ASIN,
"/ref=as_li_tl?ie=UTF8&tag=",
AssociateTag, "')")))
), escape=FALSE)
Upvotes: 1
Views: 1773
Reputation: 20329
The problem arises from the fact that you try to pass an R
object (list) (actionButton
) to mutate
. The list happens to has length 3
but mutate
expects the same length as the dataframe. What you want is to take this object and convert it to the corresponding string. As you are anyways not using Shiny's
reactivity on these buttons, I would recommend to construct the HTML
yourself instead of using actionButton
(untested, as you did not provide a reprex ):
library(tidyverse)
filteredData %>%
mutate(Amazon.button = map_chr(ASIN,
~ tags$button(class = "btn btn-default",
type = "button",
icon("amazon"),
"Amazon",
onclick = paste0("window.open('https://",
AmazonSiteLink,
"/gp/product/",
.x,
"/ref=as_li_tl?ie=UTF8&tag=",
AssociateTag,
"')")
) %>%
as.character()
)
)
Upvotes: 5