简体   繁体   中英

Tree View in R Shiny using shinyTree package

I am creating Tree View in R Shiny using shinyTree package, able to do the same. The code which has been used for server part has list creation. Now, additional requirement is to convert the dataframe into list and import the same to achieve the tree structure using renderTree.

Here is the code which I have written:

#ui part
library(shiny)
library(shinyTree) # Using shinyTree Package

# UI for application that demonstrates a Tree View

shinyUI(
  pageWithSidebar(
    # Application title
    headerPanel("Tree View in Shiny"),

    sidebarPanel(
      helpText(HTML("A simple Shiny Tree example.
                  <hr>Created using shinyTree Package."))
    ),
    mainPanel(
      # Show a simple table.
      shinyTree("tree")
    )
  ))
#--------------------------------------------------------------------

#server part

library(shiny)
library(shinyTree)

# server logic required to generate a tree structure

shinyServer(function(input, output, session) {
  output$tree <- renderTree({
    **list(
      Folder1 = list(File1.1 = structure("",sticon="file"), File1.2 = structure("",sticon="file")),
      Folder2 = list(
        Subfolder2.1 = list(File2.1.1 = structure("",sticon="file"), File2.1.2 = structure("",sticon="file")
                            , File2.1.3=structure("",sticon="file")),
        Subfolder2.2 = list(File2.2.1 = structure("",sticon="file"), File2.2.2 = structure("",sticon="file")),
        File2.3 = structure("",sticon="file")
      )**
    )
  })
})

The star part of the code needs to be replaced with list (that has been converted using dataframe). How I can achieve the same.

Here Recursion method cab be used to get tree structure from a dataframe.

Solution:

Code to convert dataframe to tree (list)

a<-read.csv("Tree.csv") # Importing the dataframe

# Recursion function to get tree structure
gettree<-function(a)
{

# defining tree as list
tree<-list()  

# Condition to identifly if selected column is not last column
if(class(a)!="factor"&&length(a)>1)
{

# getting uniques list of names from the columns to create folder 
b<-unique(a[,1])  

# runnig file name in loop
for(i in b)
{
# check if the flie name i not blank
if(i!="")
{ 
# subset data for content of folder  
subdata<-a[which(a[,1]==i),] 

# if there is only one element for that item change icon as file 
if(length(subdata[,-1])==1)
{tree[[i]]=structure("",sticon="file")}
else{
# call the function recursively  
tree[[i]]<-gettree(subdata[,-1]) 
  }}

}}

# Change icon of last columns as file 
if(class(a)=="factor"||length(a)==1)
{
for(i in a)
{      
  tree[[i]]=structure("",sticon="file")
}

}
return(tree)
}

ui.R

library(shiny)
library(shinyTree) # Using shinyTree Package

# UI for application that demonstrates a Tree View

shinyUI(
pageWithSidebar(
# Application title
headerPanel("Tree View in Shiny"),

sidebarPanel(
  helpText(HTML("A simple Shiny Tree example.
              <hr>Created using shinyTree Package."))
),
mainPanel(

  shinyTree("tree")
)
))

Server.R

# server logic required to generate a tree structure

shinyServer(function(input, output, session) {
a<-read.csv("Tree.csv")

# call the function get tree and pass the data frame
tree<-gettree(a)

# render the list in tree
output$tree <- renderTree({
tree
  })
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM