2

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!

JeanBertin
  • 633
  • 1
  • 7
  • 23

1 Answers1

2

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.

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • Just another simple question: supposing all png files are not located in www folder but in a subfolder called my_files ( for instance ), how can I concatenate www/myfiles path within "window.open($('#file_choice').val())" argument to be able to use the selector again? Thks ! – JeanBertin Oct 31 '18 at 13:21