I am recently trying to make a basic interactive Shiny app, where users can also download the pdf files as well. And, I am receiving Warning: Error in render: could not find function "render"
error when trying to run the app. I could not figure out what the problem is. Here is my code so far
library(shiny)
library(rnaturalearth)
library(DT)
library(leaflet)
library(readxl)
library(tidyverse)
library(scales)
library(ggtext)
library(showtext)
library(showtextdb)
library(plotly)
library(shinyjs)
# Create a fake dataset with population and economy information
fake_data <- read_excel("data.xlsx")
ui <- fluidPage(
## enables shinyjs
useShinyjs(),
sidebarLayout(
sidebarPanel = sidebarPanel(
## Select Players
selectizeInput(inputId = "selected_city", label = "Şehir Seçiniz", choices = unique(fake_data$Vilayet), multiple = FALSE,
width = "100%"),
hr(),
## Start these buttons off as disabled
disabled(actionButton("render_report_button","Render Report",width = "100%")),
disabled(downloadButton("download_rendered_report", "Download Report", style = "width:100%;"))
),
mainPanel = mainPanel(
## simple plots
DTOutput(outputId = "fake_table")
)
)
)
### Server
server <- function(input, output, session){
selected_city_data <- reactive({
req(input$selected_city)
fake_data %>%
filter(Vilayet %in% input$selected_city)
})
## Make the table
fake_table_raw <- reactive({
selected_city_data() %>%
datatable(options = list(paging = FALSE, searching = FALSE, info = FALSE))
})
## Send plots to output
output$fake_table <- renderDT({fake_table_raw()})
## enable rendering if players are selected, else, disable everything
observeEvent(input$selected_city,{
if(!is.null(input$selected_city)){
enable("render_report_button")
}else{
disable("render_report_button")
disable("download_rendered_report")
report_loc(NULL)
}
}, ignoreNULL = FALSE)
## Render report on click. store result to reactive val
report_loc <- reactiveVal()
observeEvent(input$render_report_button,{
## always write to a new dir
output_dir <- tempfile()
dir.create(output_dir)
## store data from shiny in env for report, getting values from reactive
render_env <- new.env()
render_env$selected_city_data <- selected_city_data()
render(
input = "report.Rmd", ## capitalization must match
output_dir = output_dir,
envir = render_env
)
## update report_loc to have rendered file
report_loc(list.files(output_dir, full.names = TRUE))
})
## enable downloading if report is rendered
observeEvent(report_loc(),{
if(!is.null(report_loc())){
enable("download_rendered_report")
}else{
disable("download_rendered_report")
}
}, ignoreNULL = FALSE)
## download rendered report!
output$download_rendered_report <- downloadHandler(
filename = function(){
basename(report_loc())
},
content = function(file){
file.copy(report_loc(), file, overwrite = TRUE)
}
)
}
shinyApp(ui = ui, server = server)
I appreciate any suggestions, comments and/or feedback on how to figure out this problem. Thank you for your attention beforehand.