Chart not generated in R shiny when run locally using googleVis - r

These are the codes for my UI and server. The issue that I am facing is that when the app is run locally the charts are not being generated.
ui.R
library(googleVis)
library(shiny)
shinyUI(fluidPage(
titlePanel(" Tool"),
sidebarLayout(
sidebarPanel(
radioButtons(inputId="choice", label="What would you like to see?",
choices=c("Overall ","Individual"))
),
mainPanel(
htmlOutput("View")
)
)
))
server.R
library(googleVis)
require(googleVis)
shinyServer(function(input, output) {
n = 100
dates = seq(Sys.Date(), by = 'day', length = n)
x = 10 * rnorm(n)
y = 3 * x + 1 + rnorm(n)
label = rep(LETTERS[1:4], each=25)
label[1] = "D"
my.data = data.frame(Date = dates, x, y, label)
output$view <- renderGvis({
gvisMotionChart(my.data, idvar ='label', xvar = 'x', yvar = 'y', timevar= 'Date')
})
}
)

Looks like you have a couple things going wrong here. First, you should have a library open to shiny in both server.R and ui.R; it looks like you reproduced googleVis twice in server.R. In addition I found you capitalized the 'v' in htmlOutput('view'), but this should match the output$view path in server.R which is not capitalized.
On top of this the radio buttons seem superfluous or I do not understand the intent. Typically radio buttons are used so that their input can be fed to a reactive environment in server.R to change a dataset or some other parameter (see shiny tutorial or this example: https://github.com/rstudio/shiny-examples/blob/master/006-tabsets/server.R).
Code below will produce the plot and I have left the radio buttons even though they serve no purpose.
ui.R
library(googleVis)
library(shiny)
shinyUI(fluidPage(
titlePanel(" Tool"),
sidebarLayout(
sidebarPanel(
radioButtons(inputId="choice", label="What would you like to see?",
choices= c("Overall ","Individual"))
),
mainPanel(
htmlOutput("view")
)
)
))
server.R
library(googleVis)
library(shiny)
shinyServer(function(input, output) {
n = 100
dates = seq(Sys.Date(), by = 'day', length = n)
x = 10 * rnorm(n)
y = 3 * x + 1 + rnorm(n)
label = rep(LETTERS[1:4], each=25)
label[1] = "D"
my.data = data.frame(Date = dates, x, y, label)
output$view <- renderGvis({
gvisMotionChart(my.data,
idvar ='label',
xvar = 'x',
yvar = 'y',
timevar= 'Date')
})
})
Be sure to also open it to a browser after the app is launched. Hope that helps.

Related

Produce a graph from an editable table in Shiny

I have a shiny application with the following ui:
library(rhandsontable)
library(shiny)
library(ggplot2)
ui = fluidPage(
# App title ----
titlePanel("Tabsets"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Tabset w/ plot, summary, and table ----
tabsetPanel(type = "tabs",
tabPanel("Summary", rHandsontableOutput('contents'),
actionButton("saveBtn", "Save changes")
),
tabPanel("Tab",
rHandsontableOutput('contentFinal')),
tabPanel("Dashboard",
plotOutput('dashboard1'))
)
)
)
)
And the following server
library(dplyr)
library(rhandsontable)
options(shiny.maxRequestSize = 9*1024^2)
server = function(input, output) {
values <- reactiveValues()
Post <- c("", "")
list2 <- c(12,13)
df <- data.frame(Post, list2)
output$contents <- renderRHandsontable({
rhandsontable(df, width = 550, height = 300) %>%
hot_col(col = "Post", type = "dropdown")
})
saveData <- eventReactive({input$saveBtn},{
finalDF <- hot_to_r(input$contents)
finalDF$Post <- ifelse(finalDF$Post =="",NA,finalDF$Post)
newDF <- finalDF[complete.cases(finalDF),]
return(newDF)
})
output$contentFinal <- renderRHandsontable(
rhandsontable(saveData())
)
output$dashboard1 <- renderPlot(
ggplot(input$contentFinal, aes(x = Post, y = list2 )) +
geom_bar(stat = "identity")
)
observeEvent(input$saveBtn, saveData())
}
shinyApp(ui = ui, server = server)
The flow is like this:
In the first tab, I bring up data with an empty post column
In this tab, I can add a name for the post and save it.
As soon as I save he rows with values for post become visible in the next tab.
Then the next thing I want to do is to have a visual in the dashboard tab that shows the data. Therefore I create:
output$dashboard1 <- renderPlot(
ggplot(input$contentFinal, aes(x = Post, y = List2 )) +
geom_bar(stat = "identity")
)
This however gives me the following ggplot2 errror:
ggplot2 doesn't know how to deal with data of class list
Any thoughts on what goes wrong here?
The problem is because input$contentFinal is handsontable data. We need to convert it to R object using hot_to_r function.
The ggplot should be plotted using the following:
ggplot(hot_to_r(input$contentFinal), aes(x = Post, y = list2 )) +
geom_bar(stat = "identity")
Hope it helps!

Shiny app not reacting to changes in a Biostrings::DNAString object

I'm working on a shiny app that accepts a DNA sequence (e.g. "ACTGACTG"), does some calculations and plots the result when a button is clicked. When I store a Biostrings::DNAString in a reactiveValues object, my shiny app only reacts to changes if the number of characters of the sequence changes, e.g. if "AA" is entered first, the plot doesn't change if "CC" is then entered but does change if "AAAA" is entered. It responds to all changes if I store the object as a character. Here's a simplified example:
library(shiny)
library(shinyBS)
library(Biostrings)
library(ggplot2)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
ref_seqs <- textInput("ref_seqs", "Sequence", width = "100%",
value = NULL, placeholder = "ATGCTGCTGGTTATTAGATTAGT"),
run_guide <- bsButton("run", 'Run', type = "action",
style = "success", block = TRUE)
),
mainPanel(
plotOutput("reference")
)
)
)
server <- function(input, output) {
ref <- reactiveValues(sq = NULL)
dat <- reactive({
req(input$run)
chrs <- strsplit(as.character(ref$sq),"")[[1]]
data.frame(label = chrs, x = seq_along(chrs))
})
observeEvent(input$run, {
ref$sq <- Biostrings::DNAString(input$ref_seqs)
#ref$sq <- input$ref_seqs
})
output$reference <- renderPlot({
ggplot(dat(), aes(x = x, y = factor(1), label = label)) + geom_text(size = 12)
})
}
shinyApp(ui = ui, server = server)
If I comment out the line ref$sq <- Biostrings::DNAString(input$ref_seqs) and uncomment the line below it, the plot updates upon changes.
Can anyone explain why this happens? Do reactiveValues only work with base types? Thanks!

R Shiny: Create a button that updates a data.frame

I have a randomly generated data.frame. The user can modify a slider to choose the number of points. Then I plot this data.frame.
I want to add a button than when clicked, it performs a modification in the previous randomly generated data.frame (but without regenerating the data.frame). The modification is a voronoid relaxation, and it should be performed once per each time the button is clicked and the graph generated.
Until now, I have not achieved anything similar...
ui.R
library(shiny)
# Define UI for application that draws a histogram
shinyUI(fluidPage(
# Application title
titlePanel("Map Generator:"),
# Sidebar with a slider input for the number of bins
sidebarLayout(
sidebarPanel(
p("Select the power p to generate 2^p points."),
sliderInput("NumPoints",
"Number of points:",
min = 1,
max = 10,
value = 9),
actionButton("GenPoints", "Generate"),
actionButton("LloydAlg", "Relaxe")
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot",height = 700, width = "auto")
)
)
))
server.R
library(shiny)
library(deldir)
shinyServer(function(input, output) {
observeEvent(input$NumPoints,{
x = data.frame(X = runif(2^input$NumPoints,1,1E6),
Y = runif(2^input$NumPoints,1,1E6))
observeEvent(input$LloydAlg, {
x = tile.centroids(tile.list(deldir(x)))
})
output$distPlot <- renderPlot({
plot(x,pch = 20,asp=1,xlim=c(0,1E6),ylim = c(0,1E6))
})
})
})
Of course there is something that I must be doing wrong, but I am quite new into shiny I can't figure it out what I am doing wrong...
This should work (even though I am pretty sure this could be improved):
shinyServer(function(input, output) {
library(deldir)
data = data.frame(
X = runif(2^9, 1, 1E6),
Y = runif(2^9, 1, 1E6)
)
rv <- reactiveValues(x = data)
observeEvent(input$GenPoints, {
rv$x <- data.frame(
X = runif(2^input$NumPoints,1,1E6),
Y = runif(2^input$NumPoints,1,1E6)
)
})
observeEvent(input$LloydAlg, {
rv$x = tile.centroids(tile.list(deldir(rv$x)))
})
output$distPlot <- renderPlot({
plot(rv$x,pch = 20,asp=1,xlim=c(0,1E6),ylim = c(0,1E6))
})
})
So first I initialize the points to plot. I use runif(2^9, 1, 1E6) because the starting value of the sliderInput is 9 all the time.
I also removed the observeEvent from the sliderInput and moved it to the GenPoints actionButton.

rPlot won't show in Shiny app

I have a app, where I want to use rCharts and polycharts, to create a plot with a tooltip. But the plot doesn't show up, when I load the shiny app, and I can't figure out why.
Here is my ui.R:
library(shiny)
require(rCharts)
shinyUI(fluidPage(
titlePanel("Please work..."),
sidebarLayout(
sidebarPanel( "Test:",
uiOutput("TestSelection")),
mainPanel("Plots (show me the money, please!):",
showOutput("tempplot","polycharts")
)
)
))
And here's my server.R:
library(shiny)
require(rCharts)
#Test of rcharts and shiny.
df1 <- read.csv("//KED-FILE/ASIC-Shared/users/jhertel/Work/R_workspace/oban_test_data.csv",quote="")
shinyServer(function(input, output, session){
output$TestSelection <- renderUI({
df <- df1
selectInput("TestSel", "Test Variable", ls(df, pattern = ".*?_meas|.*?_calc"))
})
output$tempplot <- renderChart2({
dataPlot <- df1[,c("DUT", input$TestSel, "Temperature")]
r1 <- rPlot(input$TestSel ~ Temperature,
data = dataPlot,
type = "point",
tooltip = "#!function(item){return item.DUT}!#",
sample = FALSE)
r1$guides(
x = list(
min = pretty( dataPlot$Temperature ) [1],
max = tail( pretty( dataPlot$Temperature ), 1 ),
numticks = length( pretty( dataPlot$Temperature ) ),
labels = pretty( dataPlot$Temperature )
),
y = list(
min = pretty( dataPlot[, input$TestSel] ) [1],
max = tail( pretty( dataPlot[, input$TestSel] ), 1 )
)
)
return(r1)
})
})
I've tried different things, such as options(RCHART_LIB = 'polycharts'), using as.formula around input$TestSel ~ Temperature, adding a reactive element on the TestSelection-ui, I've tried changing browser, from internal, to Firefox, to Chrome.
I am suspecting the input$TestSel ~ Temperature to be the reason, as when I inspect the javascript with Chrome, it doesn't look like input$TestSel has been interpreted to the desired value. But I am a total noob to javascript, so I wouldn't know. EDIT: I am fairly certain that my error occurs here, as setting the changing input$TestSel to a desired value results in the desired plot... Still don't know how to solve it, though.
When just using R the plot shows up as desired in the viewer.

How to print out the data frame along with ggplot2 charts in the same page in shiny

I am a little confused about printing the dynamic output while using R in shiny. The following are my code in shiny, it can just print out the ggplot2 charts, but not along with the new data frame. I just wanna know how to print out "mydata" at the same time. Thanks.
library("reshape2")
library("ggplot2")
Server.R
shinyServer(function(input, output) {
output$main_plot <- renderPlot({
#Draw gragh to compare the forecast values with real data
mydata <- data.frame(years, A, B)
df <- melt(mydata, id = 'years', variable = 'series')
print(ggplot(df, aes(years,value)) + geom_line(aes(colour = series), size=1.5))
})
Ui.R
shinyUI(pageWithSidebar(
# Sidebar with controls to select a dataset
# of observations to view
sidebarPanel(
selectInput("dataset", "Choose a dataset:",
choices = c("Netflix"))),
plotOutput(outputId = "main_plot", height = "500px")
))
Pull out the data frame into a reactive expression, so you can use it from two different outputs. (This is the reactive analog to introducing and reusing a variable.)
server.R
shinyServer(function(input, output) {
mydata <- reactive({
data.frame(years, A, B)
})
output$main_plot <- renderPlot({
#Draw gragh to compare the forecast values with real data
df <- melt(mydata(), id = 'years', variable = 'series')
print(ggplot(df, aes(years,value)) + geom_line(aes(colour = series), size=1.5))
})
output$data <- renderTable({ mydata() })
})
ui.R
shinyUI(pageWithSidebar(
# Sidebar with controls to select a dataset
# of observations to view
sidebarPanel(
selectInput("dataset", "Choose a dataset:",
choices = c("Netflix"))),
plotOutput(outputId = "main_plot", height = "500px"),
tableOutput(outputId = "data")
))

Resources