首页 > 解决方案 > 由 selectInput 选择的内容的 shinyapps.IO 自定义 URL

问题描述

我想在 shinyapps.IO 中生成一个自定义 URL(或愿意升级到适当的企业工具),基于selectInput(). 在下面的示例中,如果我发布到 shinyapps.IO,则 URL 将为https://myDomain.shinyapps.io/myAppName/

我想要 5 个唯一的 URL,基于 selectInput() 中用户选择的选项。

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(tidyverse)

#################### UI   ###################
ui <- dashboardPagePlus(
  ###### Header ####
  
  header = dashboardHeaderPlus(
    title = NULL,
    titleWidth = '250',
    disable = FALSE,
    enable_rightsidebar = FALSE,
    .list = NULL,
    left_menu = tagList(
      selectInput(
        inputId = "options",
        label = "Select an option",
        choices = c('Option1', 'Option2', 'Option3', 'Option4', 'Option5'))
    ) #end left_menu
  ), #close Header
  
  
  ###### Sidebar ####
  sidebar = dashboardSidebar(disable = TRUE),
  
  footer = dashboardFooter(NULL),
  ###### Body ####
  body = dashboardBody(
    uiOutput('optionSelected')
  ) #close dashboardBody
) # closes Dashboard Page Plus

#################### SERVER   #################### 
server = function(input, output, session) { 
 
  output$optionSelected <- renderUI({
    input$options
  }
  )
}

shinyApp(ui = ui, server = server)

我在https://community.rstudio.com/t/vanity-urls-with-connect-via-deployapp/18927/4阅读了有关“虚荣 URL”的信息,但这似乎不太像我正在寻找的解决方案.

感谢您的任何建议。

标签: rshinyshiny-servershinyapps

解决方案


就像我在评论中提到的那样,我认为您正在寻找书签,请参阅?shiny::enableBookmarking()

对于书签,您必须对代码进行三处修改。制作

  1. ui代码一个函数

    ui <- function(request){...}
    
  2. 在您的用户界面中包含一个书签触发器/按钮

    bookmarkButton()
    
  3. 在启动应用程序之前启用书签。

    enableBookmarking("url")
    

最小的可重现示例是:

ui <- function(request) {
  fluidPage(
    selectInput("options", "opt", choices = c('Option1', 'Option2')),
    bookmarkButton()
  )
}
server <- function(input, output, session) { }
enableBookmarking("url")
shinyApp(ui, server)

自动生成 url

port_nr <- 3033
input_id <- "select_opt"
choices <- c('Option1', 'Option2')
paste0("http://127.0.0.1:", port_nr, "/?_inputs_&", input_id, "=", 
   URLencode(choices, reserved = TRUE))

您的示例将显示为:

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)

ui <- function(request) {
    dashboardPagePlus(
    ###### Header ####
    
    header = dashboardHeaderPlus(
      title = NULL,
      titleWidth = '250',
      disable = FALSE,
      enable_rightsidebar = FALSE,
      .list = NULL,
      left_menu = tagList(
        selectInput(
          inputId = "options",
          label = "Select an option",
          choices = c('Option1', 'Option2', 'Option3', 'Option4', 'Option5'))
      ) #end left_menu
    ), #close Header
    
    
    ###### Sidebar ####
    sidebar = dashboardSidebar(disable = TRUE),
    
    footer = dashboardFooter(NULL),
    ###### Body ####
    body = dashboardBody(
      uiOutput('optionSelected'),
      bookmarkButton()
    ) #close dashboardBody
  ) # closes Dashboard Page Plus
}
#################### SERVER   #################### 
server = function(input, output, session) { 
  
  output$optionSelected <- renderUI({
    input$options
  }
  )
}
enableBookmarking("url")
shinyApp(ui = ui, server = server)

推荐阅读