I'm new to Shiny and R in general and could do with some help!
I've been trying to get some googleVis outputs into Shiny without success. I can get gvis outputs for bubble, motion and scatter charts directly in R but not in a shiny app.
I've checked reference code from Mages and others but I don't get an output chart in Shiny.
I've stripped it back to its most basic but can't get the visualisation. Is there anyone who could give me a pointer on what I am doing wrong?
Thanks!
#UI.R
library(shiny)
library(googleVis)
shinyUI(fluidPage(
titlePanel("GoogleVis example"),
sidebarLayout(
sidebarPanel(
h4("GoogleVis and Shiny")
),
mainPanel(
htmlOutput("bubble")
)
))
)
#Server.R
library(shiny)
library(googleVis)
shinyServer(function(input, output, session){
Newyork=read.csv("data/Newyork.csv")
output$bubble<- renderGvis({
gvisBubbleChart(Newyork, idvar="County", xvar="Population", yvar="Crime.Rate")
})
})
Related
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)
How to give titles in Shiny for two side plots, I am trying to do something like following, which is definitely giving me error
shinyUI(
titlePanel("Hello Shiny!"),
mainPanel(
fluidRow(
splitLayout(cellWidth(50%,50%),
tableOutput("Table1", tags$b("Title1")),
plotOutput("Plot",tags$b("Title2)))))
Here is the solution for you:
library(shiny)
ui <- basicPage(
fluidRow(
column(6,box("Title 1",tableOutput("Table1"))),
column(6,box("Title 2",plotOutput("Plot")))))
server <- function(input, output) {
}
shinyApp(ui, server)
It is enough if you just type the text in column container before ..output(..)
I have improved bit your code by replacing splitLayout() to column containers which divide/split the ui into two, same size containers (column(6)=50%) and addition of box(), so your outputs are held there and title will be attached to it (to the box)
I used googleVis to generate a chart object (x) then outputted it as:
cat(x$html$chart, file="y.html")
How do I input this raw .html file into a Shiny server.R and then get it to render properly on the ui? I can't find anything. I've tried with
y <- readLines("y.html")
output$Plot = renderHTML(HTML(y))
and
shinyUI(fluidPage(
titlePanel(""),
sidebarLayout(
sidebarPanel(
...
),
mainPanel({
htmlOutput("Plot")
}
)
)
))
but this just generates a blank area. I omitted sidebarPanel because I know that works with my rendergVis() code that I was running.
Anyone know how to do what I'm trying to do?
I'm trying to populate a dropdown for selectInput from a dataframe in a shiny application and can't seem to get it to work, here is a pared-down version:
datapr<-data.frame(type=c("Analog", "Digital", "Mixed Signal","Analog"),process=c("Bipolar","CMOS","BiCMOS","Bipolar"),funct=c("BJT","Mux","Mux","Regulator"))
If I have that dataframe to start, my shiny application calls and uses it like so:
ui.R
shinyUI({
selectInput("type",h4("Type:"),list("Analog","Digital","Mixed Signal"))
selectInput("process",h4("Process:"),"")
})
server.R
shinyServer(function(input,output,session){
observe({updateSelectInput(session,"process",choices=datapr$process[datapr$type==input$type])
})
What I'm getting out is a number instead of the actual dataframe's entry and cannot seem to use unname(), unique(), factor(), as.list() or anything straight-forward to pull out the entry as is. This used to work before the inception of SelectizeInput was added. Any help is greatly appreciated.
This worked for me:
ui.R
library(shiny)
shinyUI(fluidPage(
selectInput("type",h4("Type:"),list("Analog","Digital","Mixed Signal")),
selectInput("process",h4("Process:"),"")
))
server.R
library(shiny)
datapr<-data.frame(type=c("Analog", "Digital", "Mixed Signal","Analog"),
process=c("Bipolar","CMOS","BiCMOS","Bipolar"),
funct=c("BJT","Mux","Mux","Regulator"))
shinyServer(function(input,output,session){
observe({
updateSelectInput(session,"process",
choices=as.character(datapr$process[datapr$type==input$type]))
})
})
I'd like to deploy a web site using R shiny with a googleVis motion chart,
but when running the app, the motion chart showed up in another window in IE browser.
I'm wondering how the motion chart can show up together along with the R shiny.
Thanks in advance.
server.R
shinyServer(function(input, output) {
project_sub<-subset(project_all, select=c("name", "generation",
"man_cost", "quantity"))
motionchart2<-gvisMotionChart(project_sub, "name", "year2")
output$view_gviz <- renderGvis
({
plot(motionchart2)
})
})
ui.R
shinyUI(fluidPage(
titlePanel("Analysis of Project NRE"),
mainPanel(
h1("Motion Chart"),
h4("A Motion Chart is an alternative to providing a quick visual
overview ofprojects."),
plotOutput("view_gviz")
)
))
I ran into this problem. Try this in your ui.R
# googleVis needs htmlOutput not usual PlotOutput
htmlOutput("view_gviz")