Object not found when I tried shiny i r markdown

...image

It works well when I run the shiny app independently from rmarkdown but when I put it inside, I always got this error.

According to this link,Cannot use shiny in Rmarkdown I should install an older version of rmarkdown.
I tried 3 different version:

if (!require("devtools")) install.packages("devtools")
devtools::install_github("rstudio/rmarkdown")

install_version('rmarkdown', version = '1.10',dependencies=TRUE)

install_version('rmarkdown', version = '1.12',dependencies=TRUE)

But they all don't work.

Any one can help me with this?

Thank you a lot.

Hi,

Issues with Shiny code can stem from both the reactive Shiny code itself or the regular R code used in the functions. In order for us to help you with your question, please provide us a minimal reprocudible example (Reprex) where you provide a minimal (dummy) dataset and code that can recreate the issue. One we have that, we can go from there. For help on creating a Reprex, see this guide:

Good luck!
PJ

Thank you, PJ. My app is really super simple. Just call a very small dataset and they filter on two columns. I am attaching the code here. Please notice that this code works well independently but not inside rmarkdown. And I need it to work in rmarkdown. Thank you.

df <- data.frame("city" = "New York", "num_of_bedroom" = c(2,3), "indicator" = c(1,2))
df$city = as.character(df$city)

ui <- shinyUI(fluidPage(

Sidebar with a slider input for number of bins

sidebarLayout(
sidebarPanel(
selectInput("city", "Choose a city:",
choices = "New York",
selected = "New York"),
selectInput("number_of_bedroom", "Choose a room number:",
choices = c(2,3,4),
selected = 2)
),

# Display plot

mainPanel(
  tabsetPanel(  
    tabPanel("Zipcode ranking", dataTableOutput("table"))
  )
)

)
))

server <- shinyServer(function(input, output) {

dataInput <- reactive({

out <- filter(df, city ==  input$city & num_of_bedroom == input$number_of_bedroom)
out})

output$table <- renderDataTable({
dataInput()
},options = list(lengthMenu = c(30, 60, 100), pageLength = 5))

})

shinyApp(ui, server)

Hi,

The filter function is part of the dplyr package. For some reason, it is not an issue if not loaded in Shiny, but you need to explicitly do this for the markdown document it seems. This is my code that is working:

{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(shiny)
library(dplyr)
{r, echo=FALSE}
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("city", "Choose a city:",
                  choices = "New York",
                  selected = "New York"),
      selectInput("number_of_bedroom", "Choose a room number:",
                  choices = c(2,3,4),
                  selected = 2)
    ),
    # Display plot
    
    mainPanel(
      tabsetPanel(  
        tabPanel("Zipcode ranking", dataTableOutput("table"))
      )
    )
  )
)

server <- function(input, output, session) {
  df <- data.frame("city" = "New York", "num_of_bedroom" = c(2,3), indicator = c(1,2))
  df$city = as.character(df$city)
  
  dataInput <- reactive({
    
    out <- filter(df, city ==  input$city & num_of_bedroom == input$number_of_bedroom
    out

  })
  
  output$table <- renderDataTable({
    dataInput()
  },options = list(lengthMenu = c(30, 60, 100), pageLength = 5))
  
}

shinyApp(ui, server)

Few extra comments:

  • When creating a data frame, you don't need to quote the name of the column:
#This
df <- data.frame("city" = "New York", "num_of_bedroom" = c(2,3), indicator = c(1,2))

#Can be this
df <- data.frame(city = "New York", num_of_bedroom = c(2,3), indicator = c(1,2))
  • There is no need in saving your data frame in a local variable and then calling it again to return the frame:
#This ...
 dataInput <- reactive({    
    out <- filter(df, city ==  input$city & num_of_bedroom == input$number_of_bedroom
    out
  })

#Can be this...
 dataInput <- reactive({    
    filter(df, city ==  input$city & num_of_bedroom == input$number_of_bedroom
  })

  • Finally, you could use a full dplyr implementation
#This....
filter(df, city ==  input$city & num_of_bedroom == input$number_of_bedroom

#Can be this...
df %>% filter(city == input$city, num_of_bedroom == input$number_of_bedroom )

Hope this helps,
PJ

Hi PJ. Bravo. Yes, it works now. Thank you sooooo much!

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