2

使用 R Shiny,是否可以将 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)

非常感谢!

4

1 回答 1

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()返回所选选项的值。

于 2018-09-19T13:21:23.590 回答