【问题标题】:Link R shiny selectInput item to open file actionButton链接 R 闪亮的 selectInput 项以打开文件 actionButton
【发布时间】:2019-02-23 14:54:01
【问题描述】:

使用 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)

非常感谢!

【问题讨论】:

    标签: r shiny onclick buttonclick action-button


    【解决方案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') 选择 id 为 file_choice 的元素。这是selectInput。而$('#file_choice').val() 返回所选选项的值。

    【讨论】:

    • 只是另一个简单的问题:假设所有 png 文件都不位于 www 文件夹中,而是位于名为 my_files 的子文件夹中(例如),我如何在 "window.open($( '#file_choice').val())" 参数是否能够再次使用选择器?谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    • 2015-05-07
    • 2018-07-09
    • 2018-07-25
    • 2018-10-19
    • 2016-07-28
    • 2020-07-31
    相关资源
    最近更新 更多