Link R shiny selectInput item to open file actionButton

跟風遠走 提交于 2021-02-19 06:22:48

问题


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.

Please find below a reproductible example:

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)

Thanks a lot!


回答1:


You can do

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

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.



来源:https://stackoverflow.com/questions/52405732/link-r-shiny-selectinput-item-to-open-file-actionbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!