I am able to use the data.tree package to produce a plot. Here is an example of a plot:
library(data.tree)
org <- Node$new("Parent")
org$AddChild("Child_1")
org$AddChild("Child_2")
plot(org)
However, I am not able to render this plot in R-Shiny. I have been able to render most other plots in Shiny. What can I do to render this plot, and why is it not showing up? Here is my Shiny code:
library(shiny); library(data.tree)
ui <- fluidPage(
mainPanel(plotOutput("orgplot") )
)
server <- function(input, output){
rv <- reactiveValues()
org <- Node$new("Parent")
org$AddChild("Child_1")
org$AddChild("Child_2")
output$orgplot <- renderPlot({ plot(org)})
}
shinyApp(ui = ui, server = server)
plot(org) generate widget of class grViz So you can use renderGrViz to show plot in shiny.
Like ( textInput used for example of change name of "parent")
library(shiny);
library(data.tree)
library(DiagrammeR)
ui <- fluidPage(
mainPanel(grVizOutput("xx") ),
textInput("parent","parent","parent")
)
server <- function(input, output){
output$xx=renderGrViz({
org <- Node$new(input$parent)
org$AddChild("Child_1")
org$AddChild("Child_2")
grViz(ToGraphViz(org),engine = "dot")
})
}
shinyApp(ui = ui, server = server)
Update add node dynamically
If You want to add nodes dynamically you can try to add child to node by name( you need some checks to avoid names duplicate)
New functions to draw chart get from #rpm answer
ui <- fluidPage(
mainPanel(uiOutput("add_child_ui"),
grVizOutput("xx") )
)
server <- function(input, output){
#Create reative value to app
vv=reactiveValues(org=NULL,names=NULL)
#create main tree
observe({
vv$org <- Node$new("Parent1")
vv$org$AddChildNode(child = Node$new("1"))
vv$names=vv$org$Get('name') # get names of main tree
})
output$add_child_ui=renderUI({
list(
wellPanel(
selectInput("Name_to_change","Name_to_change",vv$names),
textInput("new_name","new_name",""),
actionButton("Change_name","Change_name")
),
wellPanel(
selectInput("Parent_name","Parent_name",vv$names),
textInput("new_node_name","new_node_name",""),
actionButton("add_child","add_child")
))
})
observeEvent(input$Change_name,{
aa=FindNode(node=vv$org,name = input$Name_to_change)
aa$name=input$new_name # Change name
vv$names=vv$org$Get('name')# get names of new tree
#re-generate chart
output$xx=renderGrViz({
grViz(DiagrammeR::generate_dot(ToDiagrammeRGraph(vv$org)),engine = "dot")
})
})
observeEvent(input$add_child,{
FindNode(node=vv$org,name = input$Parent_name)$AddChildNode(Node$new(input$new_node_name)) # add child
vv$names=vv$org$Get('name')# get names of new tree
#re-generate chart
output$xx=renderGrViz({
grViz(DiagrammeR::generate_dot(ToDiagrammeRGraph(vv$org)),engine = "dot")
})
})
output$xx=renderGrViz({
grViz(DiagrammeR::generate_dot(ToDiagrammeRGraph(vv$org)),engine = "dot")
})
}
shinyApp(ui = ui, server = server)
This code does not work as the ToGraphViz function has been retired from DiagrammeR and replaced with ToDiagrammerGraph. The following change works.
# grViz(ToGraphViz(org),engine = "dot")
grViz(DiagrammeR::generate_dot(ToDiagrammeRGraph(org)))
Also, "parent" is a reserved word so I replaced it "parent2".
Related
I am having problems rendering any visualizations on Shiny. I tried many different plots, none of them worked. Here is the code:
library(shiny)
ui <- fluidPage("Comex",
selectInput("paises","Selecione o Destino da Exportação",PAISES$NO_PAIS, selected = PAISES$NO_PAIS[55]),
plotlyOutput(outputId = "table"))
server <- function(input, output){
output$table <- renderTable({
p <- RPostgres::dbGetQuery(con, paste0("SELECT CO_ANO, NO_PAIS, SUM(VL_FOB)
FROM comex
INNER JOIN paises ON comex.CO_PAIS = paises.CO_PAIS
WHERE (SG_UF_MUN = 'AL') AND (NO_PAIS = '",input$paises,"')
GROUP BY NO_PAIS, CO_ANO"))
View(p)
})}
shinyApp(ui, server)
The SQL command seems fine, as I successfully extracted data with this very code outside the shinyApp structure.
The return value from View(.) is NULL, so your renderTable will always be blank; just make it p.
If your ui contains plotlyOutput then replace renderTable with plotly::renderPlotly. The ui-component for shiny::renderTable is shiny::tableOutput.
renderTable is intended for a tabular display of data.frame-like data, not a plot.
Choose either:
ui <- fluidPage(
...,
tableOutput("table")
...
)
server <- function(input, output, session) {
output$table <- renderTable({
# code that returns a data.frame
})
}
or
ui <- fluidPage(
...,
plotlyOutput("myplot")
...
)
server <- function(input, output, session) {
output$myplot <- plotly::renderPlotly({
# ...
plot_lt(...)
})
}
I am building a Shiny app with a Leaflet map based on a PostgreSQL spatialdatabase.
I succeeded to import spatial data into SpatialPolygonDataFrame and to display it on Leaflet widget.
I am trying to display the data from the SpatialDataFrame with a RenderTable output, but its not working, even by converting it with as.data.frame(spatialdataframe).
Therefore this conversion is enough to dispay the table with view(), kable() or other display functions, but not in Shiny.
Should I make another conversion? Anyone got an idea?
ui <- fluidPage(
titlePanel("AgriPAG"),
sidebarLayout(
mainPanel(
tabsetPanel(
tabPanel(leafletOutput("m",width = "100%", height = 1000)),
tabPanel(tableOutput(as.data.frame(sample_test1)))
)
),
sidebarPanel("curseur")
)
)
server <- function(input,output, session){
data <- reactive({
x <- test1
})
output$mymap <- renderLeaflet({
test1 <- data()
m <- leaflet(data = sample_test1) %>%
addTiles() %>%
setView(lng=-52.3333300, lat=4.9333300 , zoom=10) %>%
addPolygons(data=sample_test1, weight=2, col="black", opacity=0.5)
m
})
output$table <- renderDataTable(as.data.frame(sample_test1))
}
shinyApp(ui = ui, server = server)
renderDataTable does not work with tableOutput. You have to use dataTableOutput instead. In addition, you should add the correct inputId for dataTableOutput.
To get everything to work change: tableOutput(as.data.frame(sample_test1)) in your ui to dataTableOutput('table')
for (i in 1:4){
v <- rnorm(50)
plot(v, main=paste("Iteration ", i))
}
I have code that iterates through and produces a plot each time, like the above. How would I allow a user to click to see the next plot in a Shiny application?
Use the slickR package to make a nice slideshow.
library(shiny)
library(slickR)
library(svglite)
plots <- lapply(1:5, function(i){
xmlSVG({plot(rnorm(50), main=paste0("Iteration ", i))}, standalone = TRUE)
})
#make the plot self contained SVG to pass into slickR
plotsAsSVG <- sapply(plots, function(sv){
paste0("data:image/svg+xml;utf8,",as.character(sv))
})
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
####
),
mainPanel(
slickROutput("slickr", width="500px")
)
)
)
server <- function(input, output) {
output$slickr <- renderSlickR({
imgs <- plotsAsSVG
slickR(imgs)
})
}
# Run the application
shinyApp(ui = ui, server = server)
I am new to R&shiny. I'd like to make a shiny app that the plot can be interactive with subset I choose, but ggplot cannot work with warning
Error in ouptut$Trendplot <- renderPlot({ : object 'ouptut' not found
It will be really appreciated if you can help to figure it works.
The following is my code:
library(shiny)
library(ggplot2)
# Define UI for application that draws a histogram
ui <- pageWithSidebar(
# Application title
headerPanel("Pre-report situation"),
# Sidebar with a slider input for number of bins
sidebarPanel(selectizeInput("DMS", "DMS:", choices = unique(datass$DMS)
)),
# Show a plot of the generated distribution
mainPanel(
h3(textOutput("caption")),
plotOutput("Trendplot"))
)
datass <- read.csv("C:/Users/yyu6/Documents/PR.csv", sep=",", stringsAsFactors = FALSE)
# Define server logic required to draw a histogram
server <- function(input, output) {
formulaText <- reactive({
input$DMS })
datasetInput <- reactive({
selection <- Input$DMS
subset(datass, DMS == selection)
})
output$caption <- renderText({formulaText()
})
ouptut$Trendplot <- renderPlot({
ggplot(datasetInput(), mapping = aes(x=DMS))+geom_histogram(stat = "count")
})
}
# Run the application
shinyApp(ui = ui, server = server)
I'm creating Shiny app and I want to use checkboxGroupInput in order to print out multiple plots. However, I want to print out plots only for the elements of checkboxGroupInput that were checked. There is a similar example in Shiny gallery to create UI elements in a loop that uses lapply. Here is a simplified version of that example to show what I want to do:
#server.R
library(shiny)
library(ggplot2)
shinyServer(function(input, output, session) {
numberInput <- reactive({
input$checkbox
})
lapply(1:10, function(i) {
output[[paste0('b', i)]] <- renderPlot({
qplot(x = rnorm(100, mean = as.numeric(numberInput()[i]))) +
ggtitle(paste("This plot was plotted with", numberInput()[i], "option"))
})
})
})
#ui.R
library(shiny)
shinyUI(fluidPage(
title = 'lapply example',
sidebarLayout(
sidebarPanel(
checkboxGroupInput("checkbox", "Checkbox",
choices = sample(1:10, 5))
),
mainPanel(
lapply(1:10, function(i) {
plotOutput(paste0('b', i))
})
)
)
))
This works, but obviously when Shiny tries to extract numberInput()[i] where i is bigger than number of currently checked elements, there is nothing to extract and instead of a plot there is an error. Therefore I need to somehow tell lapply to iterate only n number of times where n is length(input$checkbox).
I tried to use length(input$checkbox) directly, tried putting that element in the numberInput() reactive statement and returning it as the list, I tried to use reactiveValues() in a following way:
v <- reactiveValues(n = length(input$checkbox))
lapply(1:isolate(v$n), function(i) {
However, in all of those instances Shiny complains about lack of active reactive context.
So, what am I missing? How can I use length of input in lapply outside of reactive context?
I've generally had more luck using this approach (only because it's easier for me to wrap my head around it), but the idea is to render your plots into a UI on the server and then render the UI in ui.R
#server.R
library(shiny)
library(ggplot2)
server <- shinyServer(function(input, output, session) {
output$checks <- renderText(input$checkbox)
output$plots <- renderUI({
plot_output_list <-
lapply(input$checkbox,
function(i){
plotOutput(paste0("plot", i))
})
do.call(tagList, plot_output_list)
})
observe({
for (i in input$checkbox) {
local({
local_i <- i
output[[paste0("plot", local_i)]] <-
renderPlot({
qplot(x = rnorm(100, mean = as.numeric(local_i))) +
ggtitle(paste("This plot was plotted with", local_i, "option"))
})
})
}
})
})
#ui.R
library(shiny)
ui <- shinyUI(fluidPage(
title = 'lapply example',
sidebarLayout(
sidebarPanel(
checkboxGroupInput("checkbox", "Checkbox",
choices = sample(1:10, 5))
),
mainPanel(
verbatimTextOutput("checks"),
uiOutput('plots')
)
)
))
shinyApp(ui = ui, server = server)