selectInput not appearing in Shiny app embedded in Rmarkdown

Hi everyone,

I'm trying to embed a shiny app in an Rmarkdown document using the function knitr::inlcude_app. Here's my minimal working example for the app:

library(plotly)
library(dplyr)
library(ggplot2)
library(shiny)

d <- data.frame(Group = rep(c("a","b"),each = 20),
                Value = rnorm(40),
                Location = rep(c("USA","EU"),each = 10),
                Time = rep(1:10, 4))


ui <- fluidPage(
  sidebarLayout(
    
    sidebarPanel(
      #--------------------------------------------------------
      
      selectInput("Var1",
                  label = "Variable", #DATA CHOICE 1
                  selected = "a",
                  choices = c(as.character(unique(d$Group)))),
      
      selectInput("Loc1",
                  label = "Select location", #Location filter 1
                  selected = "USA",
                  choices = c(as.character(unique(d$Location)))),
      
      #--------------------------------------------------------
      selectInput("Var2",
                  label = "Variable2", #DATA CHOICE 2
                  selected = "",
                  choices = c("",as.character(unique(d$Group)))),
      
      selectInput("Loc2",
                  label = "Select location", #Location filter 2
                  selected = "",
                  choices = c("",as.character(unique(d$Location))))
    ),


    mainPanel(
      plotlyOutput('plot') #Draw figure
    ),
    position = "left"),
)

server <- function(input, output) {
  
  out <- reactive({
    d %>% filter(Group == input$Var1, Location == input$Loc1)
  })
  
  out2 <- reactive({

    if (input$Var2 =="")
      return(NULL)
    d %>% filter(Group == input$Var2, Location == input$Loc2)
 
  })
  output$plot <- renderPlotly({
    p <- ggplot() +
      geom_line(data = out(), aes(x = Time, y = Value))+ #Add both data sets in one ggplot
      {if (!is.null(out2())) geom_line(data = out2(), aes(x = Time, y = Value), color = "red")}
    theme_bw()
    ggplotly(p)
  })
}
# Run the application 
shinyApp(ui = ui, server = server)

When I push this app to my shinyapps.io account, it appears to work correctly from the browser. I then grab the app in an Rmarkdown document using the include_app function. When I render the document to html, the selectInput statements are excluded from the embedded figure:

Does anyone have a fix for this problem? Embedding the URL via an iframe yields the same results.

Thanks!

I was able to find a work around for this issue using the miniUI package, which was designed for creating Shiny gadgets. An app produced with miniUI and called in an Rmarkdown document via knitr::include_app() will render with UI inputs, although some code wrangling is necessary to make them look pretty.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.