问题
I am having a hard time understanding the workings of plotly tables in R. I am not an experienced programmer, thus my question may look naive.
Please consider the following dataframe:
data<-data.frame(
SeqName=c("1", "2", "3", "4", "5", "6"),
Length=c("440", "511", "1087", "686", "867", "632"),
Cys=c("3", "2", "2", "2", "2", "4"),
NT=c("[NA]", "[B]", "[B]", "[B]", "[B]", "[B]"),
NR=c("[NA]", "[B][M]", "[B]", "[B][M]", "[B][M]", "[NA]"),
RefSeq=c("[NA]", "[B][M]", "[B]", "[B][M]", "[B][M]", "[NA]")
)
I can create the equivalent plotly table using the following:
plot_ly(
type = 'table',
header = list(
values = list(list('<b>SeqName<b>'),
list('<b>Length<b>'),
list('<b>Cys<b>'),
list('<b>NT<b>'),
list('<b>NR<b>'),
list('<b>RefSeq<b>'))),
cells = list(values = list(
c("1", "2", "3", "4", "5", "6"),
c("440", "511", "1087", "686", "867", "632"),
c("3", "2", "2", "2", "2", "4"),
c("[NA]", "[B]", "[B]", "[B]", "[B]", "[B]"),
c("[NA]", "[B][M]", "[B]", "[B][M]", "[B][M]", "[NA]"),
c("[NA]", "[B][M]", "[B]", "[B][M]", "[B][M]", "[NA]"))))
However, I cannot make a direct jump. For instance, I cannot get the following to work:
plot_ly(type = 'table', header = list(values=names(data)), cells=list(list(values=data)))
Please, could anyone clarify why? Thanks in advance.
回答1:
It seems that plot_ly is expecting an unnamed list only for the values argument of the list given to the cells argument, so you can just pass this to it by using unname on your data.frame:
plot_ly(type="table",header=list(values=names(data)), cells=list(values=unname(data)))
来源:https://stackoverflow.com/questions/51543803/translate-a-dataframe-into-a-table-using-r-plotly