链接R闪亮的selectInput项目以打开文件actionButton

时间:2018-09-19 12:19:18

标签: r shiny onclick buttonclick action-button

使用R闪亮,是否可以将selectInput项目链接到打开文件操作按钮?我想调整操作按钮的onclick参数来实现它。

请在下面找到可复制的示例:

假设在“ www”文件夹中有“ file_1.pdf”和“ file_2.pdf”,我该如何打开与选择输入选择相对应的文件?

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)

非常感谢!

1 个答案:

答案 0 :(得分:2)

你可以

  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')选择ID为file_choice的元素。这是selectInput。然后$('#file_choice').val()返回所选选项的值。