Olya Yeritspokhyan
Olya Yeritspokhyan

Reputation: 11

R formattable datatable to shiny

i have this code that work normally in RStudio but i wont this in shiny as.datatable() does not work in shiny with renderFormattable

it says "no applicable method for 'as.htmlwidget' applied to an object of class "c('datatables', 'htmlwidget')""

server.R

output$data<-renderFormattable({

  df1<-merge(happiness5[,c(1,3)],
             happiness6[,c(1,3)],
             by.x = "Country",
             by.y = "Country")
  colnames(df1)<-c("Country","Happiness Rank 2015","Happiness Rank 2016")
  df1<-df1%>%
    mutate(`Rank Change`=`Happiness Rank 2015`-`Happiness Rank 2016`)
  
  
  formattable(df1,list(
    `Rank Change` = formatter(
      "span",
      style=~formattable::style(color=ifelse(`Rank Change`>0,"green","red")))))%>%
    as.datatable()
},env = parent.frame(), quoted = FALSE)

Upvotes: 1

Views: 613

Answers (1)

mzuba
mzuba

Reputation: 1275

Change renderFormattable(…) to renderDataTable(…).

Since you converted the formattable to a datatable in your last but one row, you need to tell the renderer that it has to deal with a datatable.

Upvotes: 2

Related Questions