链接 R 闪亮的 selectInput 项目以打开文件 actionButton [英] Link R shiny selectInput item to open file actionButton

查看:68
本文介绍了链接 R 闪亮的 selectInput 项目以打开文件 actionButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

假设我们在www"文件夹中有file_1.pdf"和file_2.pdf",我如何打开与select Input choice 对应的文件?

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)

非常感谢!

推荐答案

你可以做

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

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.

这篇关于链接 R 闪亮的 selectInput 项目以打开文件 actionButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆