Get data in shiny via an API and return DT::dataTableOutput

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)

This sort of reactiveValues() and observe() pattern is really only necessary for maintaining state that isn't explicitly captured by input values.

For this application, you don't really need to do that, so I'd recommend chaining reactive() together. Also, note that reactive() should always return a value, and never be used to perform side effects (i.e., modify another reactiveValues()). On the other hand, observe() is designed for performing side-effects and should never return a value.


server <- function(input, output) {
  
  day_ <- reactive({
    ifelse(
      nchar(day(as.character(input$date))) == 1, 
      paste0("0", day(as.character(input$date))), 
      day(as.character(input$date))
    )
  })
  
  month_ <- reactive({
    ifelse(
      nchar(month(as.character(input$date))) == 1, 
      paste0("0", month(as.character(input$date))), 
      month(as.character(input$date))
    )
  })
  
  url <- reactive({
    glue("https://api.abalin.net/get/namedays?day={day_()}&month={month_()}")
  })
  
  result <- reactive ({
    r <- GET(url()) 
    r_content <- content(r, "text", encoding = "ISO-8859-1")
    r_json <- jsonlite::fromJSON(r_content, flatten = TRUE)
    cbind(us=r_json$data$name_us,cz= r_json$data$name_cz, sk=r_json$data$name_sk )
  })
  
  output$table_names <- DT::renderDataTable({
    result()
  })
  
  
}


shinyApp(ui = ui, server = server)

thank you. That solved my doubts

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.