Ссылка R блестящий selectInput элемент для открытия файла actionButton

Можно ли связать элемент selectInput с кнопкой действия открытия файла с помощью R shiny? Я хотел бы адаптировать аргумент кнопки действия onclick для этого.

Ниже приведен воспроизводимый пример:

Предположим, у нас есть «file_1.pdf» и «file_2.pdf» в папке «www», как я могу открыть файл, соответствующий выбору «Выбор ввода»?

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)

Большое спасибо!


person JeanBertin    schedule 19.09.2018    source источник


Ответы (1)


Ты можешь сделать

  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())"))  

Объяснение: $(...) - это селектор. $('#file_choice') выбирает элемент с идентификатором file_choice. Это selectInput. И $('#file_choice').val() возвращает значение выбранной опции.

person Stéphane Laurent    schedule 19.09.2018
comment
Еще один простой вопрос: предположим, что все файлы png находятся не в папке www, а в подпапке с именем my_files (например), как я могу объединить путь www / myfiles в window.open ($ ('# file_choice'). Val () ) аргумент, чтобы снова использовать селектор? Спасибо! - person JeanBertin; 31.10.2018