问题
Using R shiny, is it possible to link selectInput item to open file action button ? I would like to adapt onclick argument of action button to achieve it.
Please find below a reproductible example:
Supposing we have "file_1.pdf" and "file_2.pdf" on "www" folder, how can I open the file corresponding to select Input choice ?
library(shinydashboard)
library(shiny)
ui <- dashboardPage(
dashboardHeader(title = "Open file app"),
dashboardSidebar(),
dashboardBody(
fluidRow(
selectInput(inputId = "file_choice",label = "Choose the file to open",choices = c("file_1","file_2")),
actionButton("bell","Open the selected file", class = "btn action-button",onclick = "window.open('file_1.pdf')")) #onclick argument must be adapted
)
)
server <- function(input, output) {}
shinyApp(ui, server)
Thanks a lot!
回答1:
You can do
selectInput(inputId = "file_choice",
label = "Choose the file to open",
choices = c("file_1"="Rplot01.png","file_2"="Rplot02.png")),
actionButton("bell","Open the selected file", class = "btn action-button",
onclick = "window.open($('#file_choice').val())"))
Explanation: $(...)
is a selector. $('#file_choice')
selects the element with id file_choice
. This is the selectInput
. And $('#file_choice').val()
returns the value of the selected option.
来源:https://stackoverflow.com/questions/52405732/link-r-shiny-selectinput-item-to-open-file-actionbutton