D3partitionR has some fantastic visualisations for hierarchical and sequential data, however it seems to have a major flaw in Shiny.
The D3partitionROutput function (& renderD3partitionR) don't update the plotted object when the output object is updated.
The functions work perfectly on first execution of a graph however the plotted objects can't be reactively updated.
Does anyone know of a fix or workaround as I really like this package's visualisations?
library(shiny)
library(D3partitionR)
path_in=list(list("A","B","C","D"),list("A","B","F","G"),list("A","D"),list("A","B","END"))
value_in=c(15,29,21,34)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("type_in",
"Plot type:",
choices = c('circleTreeMap', 'partitionChart', 'treeMap')
)
),
# Show a plot of the generated distribution
mainPanel(
D3partitionROutput("part_out")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$part_out <- renderD3partitionR({
type = input$type_in
D3partitionR(data=list(path=path_in,value=value_in)
, type = type)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Update:
I found a similar bug here in Rwordcloud that was resolved by modifying the "render" function in Rwordcloud.js. I looked into renderValue in D3partitionR.js and the function doesn't take an input of 'instance' (as is done in Rwordcloud.js) so it seems it doesn't know when to delete / refresh renderValue. I'm an R guy (and have no js experience) so I don't know how the renderValue function should be changed in D3partitionR.js however I'm pretty sure this is the source of the problem.. Help!
Related
I need to create shiny app which will create a plot basing on dropdown menu choise. The whole computation part is pretty complicated and so is the plot – I created a function which is returning ggplot and I just wanted to show it in the app.
My idea looks as follows:
library(shiny)
source('Analysis/function_external.R')
list_names = c('a', 'b', 'c')
ui <- fluidPage(
selectInput("data", "Select data to plot", choices = list_names)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
observe({function_external(input$data)})
}
# Run the application
shinyApp(ui = ui, server = server)
It is making function run every time I change the input, but it does not show anything. I would really appreciate if you can point me into good direction.
output$my_complicated_plot <- renderPlot({ function_external(input$data) })
Solved the issue.
I found the rshiny script is super hard to debug.
Especially, the rshiny bottom is the RunAPP. If I get the error. I did not see any hints from the Console.
Could I ask how you guys debug the rshiny?
Thanks
Most of all: always keep in mind that you need to test and debug your code. Do not just write code to satisfy requirements. Consider testing and debugging to be a requirement itself. That mind set is a good starting point to follow these rules:
R-Studio provides quite some functionality useful for debugging: step-by-step execution of your code, a trace of function calls, inspection of variables, and the opportunity to run your own code on the console while the app is on hold.
If breakpoints do not work (sometimes they just won't), add browser() to your code which creates a "forced" breakpoint.
Sometimes print() helps getting additional information output to the console.
Clearly separate the business logic from the UI. Use unit tests (testthat). If errors occur, write some sample code to test the business logic outside the shiny app.
Here is an example of how I debug in Shiny:
library(shiny)
ui <- fluidPage(
titlePanel("Old Faithful Geyser Data"),
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output) {
x <- reactive({
faithful[, 2]
})
bins <- reactive({
seq(min(x()), max(x()), length.out = input$bins + 1)
})
observe(print(bins())) # THIS LINE WILL PRINT OUTPUT TO CONSOLE
output$distPlot <- renderPlot({
hist(x(), breaks = bins(), col = 'darkgray', border = 'white')
})
}
shinyApp(ui = ui, server = server)
The observe(print(reactive_object_name())) will print a reactive object to the console, which allows you to inspect what happens to a reactive object when you change inputs in the app.
I am trying to plot a simple histogram inside a shiny app. Outside the app (in R script), the graph gets plotted without any problems, (see here) but the same code produces an odd looking graph from inside the app (see the wrong graph here)
Could you help me figure out what's wrong? Link to dataset: https://drive.google.com/open?id=1ITK0lTWm_mkb9KonHLq4nuKDv-PBUDeR
library(ggplot2)
library(ggthemes)
library(scales)
library(shiny)
# read data
cso_corruption <- read.csv("cso_corruption.csv", header = T, sep = ",")
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
radioButtons(inputId = "x",label ="Measurement Type",choices =
c("freq_business", "freq_cso"), selected = NULL)
),
mainPanel(
plotOutput(outputId = "hist")
)
)
)
server <- function(input, output) {
output$hist <- renderPlot(ggplot(data = na.omit(cso_corruption), aes(x=input$x)) +
geom_histogram(fill="lightgreen", stat="count"))
}
shinyApp(ui = ui, server = server)
Possible reason is the data is not making it into the server.
I would put the csv into another file within the directory, so you can access it using
read.csv("./Data/projectdata.csv")
Therefore it does not get lost when you publish. Also make sure the data is checked when publishing. One last note to include the read.csv function in the server.
After many trial-and-errors, I have figured out a solution. The trick is, in the server, I used aes_string instead of aes. I haven't figured out why aes_string works here, since it is supposed to require the variables to be passed in quotes. But it works for some reason.
I am finally trying to make a Shiny app. I am trying to upload a .xlsx file to the app, and then apply some analysis and download the output as a separate .xlsx file. The code for analysis and taking output works when run directly outside Shiny and I use it on daily, so I am simply trying to call it via source and save the duplicated work. Here is what I am trying with Shiny.
I was having problems in calling the file from the W2S.R script, while avoiding errors. I found a way to avoid the errors. The below code is a barebones model of that. However, now I cannot get the actual input to work (Output works fine, one table output on-screen and one XLSX output off-screen).
I am using W2S <- input$W2S1 inside W2S.R script, but it is not recognising the variable input, which it does if used in the server function directly. How do I get it to work inside the script? Or is there any other workaround?
library(shiny)
ui <- fluidPage(
titlePanel(h1("Goods In Transit Analysis", align="center")),
sidebarLayout(
sidebarPanel(
fileInput("W2S1", label="Select GIT W2S file")
),
mainPanel(
tableOutput("contents")
)
)
)
server <- function(input, output) {
output$contents <- renderTable(if(is.null(input$W2S1)){return(NULL)}
else{source("./W2S.R")})
}
shinyApp(ui = ui, server = server)
I will update once I get the input to work. Please help.
EDIT: Made some progress, as noted above. So updated the new code.
Finally nailed it. I needed an observe function and use the $datapath argument.
library(shiny)
ui <- fluidPage(
# Application title
titlePanel(h1("Goods In Transit Analysis", align="center")),
# Sidebar iputs
sidebarLayout(
sidebarPanel(
fileInput(inputId="W2S", label="Select GIT W2S file")
),
# On Screen output
mainPanel(
h3(textOutput("filePath")),
tableOutput("contents")
)
)
)
# Underlining code for output
server <- function(input, output) {
observe({
source("./Code/W2S.R")
W2S <- input$W2S
output$contents <- renderTable(if(is.null(W2S)){return(NULL)}
else{W2S_F(W2S$datapath)})
})
}
# Run the application
shinyApp(ui = ui, server = server)
I have deployed an application on ShinyApps, but my application has an extra header when deployed.
The header looks like this:
However, I would like to remove this header when the application is deployed. Is it possible to do this? I am using a sample shiny web application from the tutorials.
server.R:
library(shiny)
# Rely on the 'WorldPhones' dataset in the datasets
# package (which generally comes preloaded).
library(datasets)
# Define a server for the Shiny app
shinyServer(function(input, output) {
# Fill in the spot we created for a plot
output$phonePlot <- renderPlot({
# Render a barplot
barplot(WorldPhones[,input$region]*1000,
main=input$region,
ylab="Number of Telephones",
xlab="Year")
})
})
ui.R:
library(shiny)
# Rely on the 'WorldPhones' dataset in the datasets
# package (which generally comes preloaded).
library(datasets)
# Define the overall UI
shinyUI(
# Use a fluid Bootstrap layout
fluidPage(
# Give the page a title
titlePanel("Telephones by region"),
# Generate a row with a sidebar
sidebarLayout(
# Define the sidebar with one input
sidebarPanel(
selectInput("region", "Region:",
choices=colnames(WorldPhones)),
hr(),
helpText("Data from AT&T (1961) The World's Telephones.")
),
# Create a spot for the barplot
mainPanel(
plotOutput("phonePlot")
)
)
)
)
You're not supposed to do that. If you use a paid version of shinyapps.io then you won't have this bar, but if you're using the free version they add it as advertising because they do need to make some money somehow.
(It is possible to remove it, but I really like RStudio and all their work so I don't want to promote ways to make them lose business, sorry...)
If you look at the different plans and pricing options, it clearly says that the free version includes their logo branding
If you want to remove it then you have to pay for it, it's added on the server side. Your other option is to set up a shiny server with Digital Ocean https://www.digitalocean.com/community/tutorials/how-to-set-up-shiny-server-on-ubuntu-14-04