Hello,
I am trying to create a shiny app to connect to a database via an API and return the result as a datatable from the DT library.
However, the table is not displayed. I am not sure if I am using the reactive function properly.
Here there is a reproducible example with an open API:
library(shiny)
library(tidyverse)
library(lubridate)
library(glue)
library(httr)
library(DT)
ui <- fluidPage(
titlePanel("Get name days"),
sidebarLayout(
sidebarPanel(
dateInput(inputId = "date",
label = "Select Date")
),
mainPanel(
DT::dataTableOutput("table_names")
)
)
)
server <- function(input, output) {
date <- reactiveValues()
URL <- reactiveValues()
result <- reactiveValues()
# add leading zero to day
observe(date$day <- ifelse(nchar(day(as.character(input$date))) == 1, paste0("0", day(as.character(input$date))), day(as.character(input$date))))
# add leading zero to month
observe(date$month <- ifelse(nchar(month(as.character(input$date))) == 1, paste0("0", month(as.character(input$date))), month(as.character(input$date))))
observe(URL$url1 <- glue("https://api.abalin.net/get/namedays?day={date$day}&month={date$month}"))
reactive ({r <- GET(URL)
r_content <- content(r, "text", encoding = "ISO-8859-1")
r_json <- fromJSON(r_content, flatten = TRUE)
result$result1 <- cbind(us=r_json$data$name_us,cz= r_json$data$name_cz, sk=r_json$data$name_sk )}
)
output$table_names <- DT::renderDataTable(DT::datatable(
result$result1)
)
}
shinyApp(ui = ui, server = server)