How to export data from R to postgres pgadmin 4


so, i have 2 API’S. I read them and store them in a data frame and when i try to export it to pgadmin4 to a table , i get errors. i couldn’t find a solution. Here is my code. My aim is to merge 2 api into a data frame and then store it i a table in pgadmin4. Also, these 2 apis don’t have same number of rows, because of which cbind fill is needed, but that doesn’t help. I get the errors as “Error in (function (…, row.names = NULL, check.rows = FALSE, check.names = TRUE, :
arguments imply differing number of rows: 1, 0” above the line of code “json_data1 <- data.frame(json_data1)” and error “Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘dbWriteTable’ for signature ‘“PostgreSQLConnection”, “character”, “list”’” above the line of code “dbWriteTable(con,“newdatatable”, json_data1 )”. Any help would be very appreciated, i am struck here

drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, 
             password="abc" )
dbExistsTable(con, "product_product")
json_file1<- ""
 json_data1 <- fromJSON(file=json_file1)
 json_data1 <-data.frame(json_data1)
dbWriteTable(con, "newTable", json_data1, row.names=FALSE, append=TRUE)

json_file2<- ""
json_data2 <- fromJSON(file=json_file2)

 json_data2 <- data.frame(json_data2)

combineddata = rbind(json_data1,json_data2)
# dbWriteTable(con, newdatatable,"combineddata",overwrite = T ) 
 # dbWriteTable(con,"newdatatable", x)


Some of the entries in the json file seem to have “missing” data in the “brand name” tag. This seem to crash the import with “fromJSON”:

You will probably need to “sanitize” somehow the data before you can use it.


How do i do that? is it data cleaning in R or cbind fill to just fill some values in missing spaces?


You’d need to go over the imported json and fill-in the missing data with NA.

Something like this could work, in this case:


json_file1<- ""
json_data1 = fromJSON(file = json_file1)

fix_missing <- function(x){
  x["brand_name"][[1]] <- ifelse(!is.null(x["brand_name"][[1]]),  
json_data1 = lapply(json_data1[["result"]], FUN = function(x) fix_missing(x)) %>% 
#> # A tibble: 79 x 26
#>    state emp_code salesman mobile role_name order_date area_name zone_name
#>    <chr> <chr>    <chr>    <chr>  <chr>     <chr>      <chr>     <chr>    
#>  1 Trip… 2107… 89746… Front Li… 2017-09-02 Ram Nagar West Tri…
#>  2 Trip… 2107… 89746… Front Li… 2017-09-02 Krishna … West Tri…
#>  3 Trip… 2107… 89746… Front Li… 2017-09-02 Ram Nagar West Tri…
#>  4 Trip… 2107… 89746… Front Li… 2017-09-02 Krishna … West Tri…
#>  5 Trip… 2107… 89746… Front Li… 2017-09-02 Krishna … West Tri…
#>  6 Trip… 3272     rajan.d… 96120… Front Li… 2017-09-02 Reshamba… West Tri…
#>  7 Trip… 3272     rajan.d… 96120… Front Li… 2017-09-02 Dhaleswar West Tri…
#>  8 Trip… 3272     rajan.d… 96120… Front Li… 2017-09-02 Dhaleswar West Tri…
#>  9 Trip… 3272     rajan.d… 96120… Front Li… 2017-09-02 Reshamba… West Tri…
#> 10 Trip… 2107… 89746… Front Li… 2017-09-02 Krishna … West Tri…
#> # ... with 69 more rows, and 18 more variables: territory_name <chr>,
#> #   beat_name <chr>, beat_type <chr>, distributor_name <chr>,
#> #   distributor_code <chr>, outlet_name <chr>, outletid <dbl>,
#> #   prod_name <chr>, prod_code <chr>, outlet_type <chr>, categ_name <chr>,
#> #   brand_name <chr>, qty_sales <chr>, line_type <chr>, is_mss <chr>,
#> #   is_focus <chr>, is_unscheduled <chr>, total_sales <chr>

Created on 2018-02-19 by the reprex package (v0.2.0).


Thank you. it works great. i have some more doubts. is it possible to auto refresh the API’s? in the sense, no need to read in a new api everytime, but automatically refresh it?


Hi. I do not know if I follow, sorry. What do you mean by “refreshing the API”?


in my code, i had named something like json file1 and json file2. those are links that contains data in the form of json. is it possible that these links can be auto refreshed and the updated data can be viewed when administrator adds new data into the json file of the link?


You could wrap your script in a function that when run interrogates the website and retrieves the results so that you can do:

mydata <- get_mydata()

, but you can not avoid accessing the “website” if you want to update the results.

However, if the name of the json file to be accessed changes, you should account for that by modifying
the “address” to be fetched. For example by changing the dates in

You could use string replacement functions available in package stringr, for that.


Alright. Thank you so much. your explanation is greatly helping me. i have one more doubt. i have a shiny app that gives bar graphs based on input selected from user thru drop down menu. many such drop downs are there. they seems to be clumsy, seen on a single page. i want to have radio buttons and select whichever drop down type user wants to see and based on that , the graph, only for that particular drop down is shown.


Glad it helped.

concerning your last question, I do not know much about shiny. I think you’d be better off by starting a new thread. Consider however that if you do not provide some additional details/examples I do not think that you will get many replies.


so, this is my code, and “mydata1” is a csv file with 2 columns. one column is distributor_name and it has the following data
distributor_name<-data.frame(“Chhutu Enterprise” , “Durga Agency”, “DeyS Agency House”, “S.P Traders”) . Next column is outlet_type and it has outlet_type <-data.frame(“Grocer”,“Grocer”, “Puja Outlet”, “Self Service”)
. i am able to show drop downs for specific option chosen by user, but graphs are not being plotted.


mydata1 = read.csv("C:\\Users\\BPO18\\Documents\\Book1.csv")                                                                                                                                                                                                                                                                      

ui <- fluidPage(

titlePanel ("Hello"),
  column(4, wellPanel(
    radioButtons("rb", "Please select an option",
                 choices = c("Distributor_wise", "Outlet_type_wise"))),


shinyServer <- function(input, output) {

 output$radiobuttonChoiceOutput <- renderUI({
       Distributor_wise = checkboxGroupInput("distributorInput", 
                                             choices = unique(mydata1$distributor_name),
                                             selected = mydata1$distributor_name[1]),
       Outlet_type_wise =  checkboxGroupInput("outlettypeInput", 
                                              h3("Outlet type wise"), 
                                              choices = unique(mydata1$outlet_type),
                                              selected = mydata1$outlet_type[1])

output$distributorOutput <-  renderPlot({
if(input$rb == "Distributor_wise"){
filtered <-
  mydata1 %>%
  dplyr :: filter(distributor_name == input$distributorInput)

ggplot(filtered, aes(total_sales)) +
}, height = 400, width = 800)

output$outlettypeOutput <- renderPlot({
if(input$rb == "Outlet_type_wise"){
filtered <-
  mydata1 %>%
  dplyr :: filter(outlet_type == input$outlettypeInput
ggplot(filtered, aes(total_sales)) +
}, height = 400, width = 800)

shinyApp(ui=ui, server = shinyServer)


alright. sure. thank you so much.