简体   繁体   中英

Shiny app to start with different settings

Suppose I have a Shiny app:

library("shiny")

server <- function(input, output) {
  output$plot <- renderPlot({
    par(mar=c(0,0,0,0))
    plot(0:1,0:1, type = "n",xaxs="i",yaxs="i")
    polygon(c(0,1,1,0),c(0,0,1,1),col=input$col, border=NA)
  })
}

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("col","Color",c("red","blue"),"red")
    ),
    mainPanel(plotOutput("plot"))
  )
)

shinyApp(ui = ui, server = server)

This app simply plots a red or blue square depending on the input. Now, I want to use this app from my website, where I want to have two links: a "red app" link, which will open the app with the default choice set to "red", and a "blue app" link, which will open the app with the default choice set to "blue". Other than that, the app should be the same in both cases.

Would there be an easy way to do this / would this be at all possible?

I already found a solution. I can use session$clientData$url_search to extract whatever I put after the url. Hence this app:

library("shiny")

server <- function(input, output, session) {
  output$plot <- renderPlot({
    par(mar=c(0,0,0,0))
    plot(0:1,0:1, type = "n",xaxs="i",yaxs="i")
    polygon(c(0,1,1,0),c(0,0,1,1),col=input$col, border=NA)
  })

  output$colSelect <- renderUI({
    default <- parseQueryString(session$clientData$url_search)$col
    if (is.null(default)) default <- "white"
    selectInput("col","Color",colors(),default)
  })
}

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
     htmlOutput("colSelect") 
    ),
    mainPanel(plotOutput("plot"))
  )
)

shinyApp(ui = ui, server = server, options = list(launch.browser  =TRUE))

Will start with a white box, but if I open it via eg, http://127.0.0.1:7889/?col=blue it will open in a blue app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM