I have been attempting to create a Shiny timeseries plot using NVD3 library. Am relatively new to R, Shiny and NVD3. The problem is that when I run the ShinyApp, no chart renders on the browser. Using chromes developer tools, I can see that the div for myChart is created and populated with data, but not understanding why I cannot see the chart itself.
Would appreciate any and all help on this matter...
My code is like so:
#ui.R
require(rCharts)
shinyUI(pageWithSidebar(
headerPanel("Population Trend By Age Group:"),
sidebarPanel(
selectInput(inputId = "agegrp",
label = "Choose Agegroup",
choices = c("0-4",
"5-9",
"10-14",
"15-19",
"20-24",
"25-29",
"30-34",
"35-39",
"40-44",
"45-49",
"50-54",
"55-59",
"60-64",
"65-69",
"70-74",
"75-79",
"80-84",
"85+"
),
selected = "0-4")
),
mainPanel(
showOutput("myChart", "nvd3")
)
))
server.R:
#server.R
require(rCharts)
data <- read.csv("https://raw.githubusercontent.com/kilimba/data/master/data2.csv")
agegroup_mapping <- read.csv("https://raw.githubusercontent.com/kilimba/data/master/agegroup.csv")
data <- merge(data,agegroup_mapping,by.x="agegrp",by.y="agegroup")
shinyServer(function(input, output) {
output$myChart <- renderChart({
selection <- subset(data,mapping == input$agegrp)
plot <- nPlot(n ~ year,
data = selection,
type = "lineChart",
group = "sex")
# Add axis labels and format the tooltip
plot$yAxis(axisLabel = "Population", width = 62)
plot$xAxis(axisLabel = "Year")
plot$save("ac.html")
return(plot)
})
})
Thanks,
Tumaini
Use renderChart2 instead of renderChart.
rm(list = ls())
library(shiny)
library(rCharts)
data <- read.csv("https://raw.githubusercontent.com/kilimba/data/master/data2.csv")
agegroup_mapping <- read.csv("https://raw.githubusercontent.com/kilimba/data/master/agegroup.csv")
data <- merge(data,agegroup_mapping,by.x="agegrp",by.y="agegroup")
ui =pageWithSidebar(
headerPanel("Population Trend By Age Group:"),
sidebarPanel(
selectInput(inputId = "agegrp",
label = "Choose Agegroup",
choices = c("0-4","5-9","10-14","15-19","20-24","25-29","30-34","35-39",
"40-44","45-49","50-54","55-59","60-64","65-69","70-74","75-79","80-84","85+"),selected = "0-4"),width=2),
mainPanel(
showOutput("myChart", "nvd3")
)
)
server = function(input, output) {
output$myChart <- renderChart2({
#selection <- data[data$mapping == "0-4",]
selection <- data[data$mapping == input$agegrp,]
selection <- subset(data,mapping == input$agegrp)
plot <- nPlot(n ~ year,
data = selection,
type = "lineChart",
group = "sex")
# Add axis labels and format the tooltip
plot$yAxis(axisLabel = "Population", width = 62)
plot$xAxis(axisLabel = "Year")
plot$set(width=1600, height=800)
plot$save("ac.html")
plot
})
}
runApp(list(ui = ui, server = server))
Related
I am new to the world of RShiny and i think reactivity is bit complex to understand. I am trying to make a datatable output based on row and column condition given as per user selectinput dropdown buttons. My DataTable is editable and i want to store the updated table after user edited the cells of the datatable in a new variable but i am stuck.
I tried couple of chunks suggested on stackoverflow but none of them worked for me mostly using proxytable or reactivity. I want to store the datatable after i hit proceed button. Any help would be much appreciated.
Here is my code:
library(shiny)
library(shinyWidgets)
library(shinythemes)
library(DT)
library(data.table)
#ui
shinyApp(
ui = fluidPage(
theme = shinythemes::shinytheme("flatly"),
titlePanel("Trial"),
sidebarLayout(
shiny::sidebarPanel(
#to take multiple user input
shiny::textAreaInput(
"text_input",
label = "Write input"
),
#to slect the columns to be added
shinyWidgets::pickerInput(
inputId = "somevalue",
label = "Columns to add",
choices = colnames(df),
options = list(`actions-box` = TRUE),
multiple = TRUE
),
#action button tot show the table
shinyWidgets::actionBttn(
"show_table",
label = "Show",
size = "sm",
color = "default",
block = TRUE
), width = 2
),
mainPanel(
shiny::tabsetPanel(type = "tabs",
shiny::tabPanel("Table", DT::dataTableOutput("table")),
actionBttn("proceed","proceed")
),width = 10
)
)
),
server = function(input, output,session) {
#to add reactivity to the show button
df_filter <- reactive({
text_input <- trimws(strsplit(input$text_input, ",")[[1]])
df_filter <- df[df$make %chin% text_input, input$somevalue]
})%>% shiny::bindEvent(input$show_table)
#to output hte dt table with the filters
output$table <- DT::renderDT({
DT::datatable(df_filter(),
editable = 'cell',
options = list(scrollX = TRUE , lengthChange = FALSE, autoWidth = TRUE)
# editable = list(target = "row", disable = list(columns = c(2, 4, 5))))
)
})%>% shiny::bindEvent(df_filter())
}
)
I am quite new to R shiny and I am trying to build a small shiny app but I don't know where I went wrong.
I am trying to get multiple user input via text area to filter my table output. Moreover, i want to control the columns to show in the table as well. Code is running fine for showing the columns but it is working only with one input value in the text area, it is not working with multiple user inputs.
I want to filter the table output with multiple user inputs as well.
For example for this code snippet it should return table when I write "honda,audi,bmw" in the text area input.
library(shiny)
library(shinyWidgets)
library(DT)
df <-mtcars
#ui
shinyApp(
ui = fluidPage(
titlePanel("Trial 1"),
sidebarLayout(
sidebarPanel(
#to take multiple user input
textAreaInput(
"text_input",
label = "Write multiple input separated by comma"
),
#to slect the columns to be added
pickerInput(
inputId = "somevalue",
label = "Columns to add",
choices = colnames(df),
options = list(`actions-box` = TRUE),
multiple = TRUE
),
#action button tot show the table
actionBttn(
"show_table",
label = "Show",
size = "sm",
color = "default",
block = TRUE
),
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Table", DT::dataTableOutput("table")),
tabPanel("Summary", verbatimTextOutput("summary"))
)
)
)
),
server = function(input, output,session) {
data <- observeEvent(input$show_table,{
text_input <- trimws(strsplit(input$text_input, ",")[[1]])
output$summary <- renderPrint({
summary(data())
})
output$table <- DT::renderDT({
df_sub <- df[df$make %chin% input$text_input, input$somevalue]
#df_sub = df[ ,input$somevalue]
datatable(df_sub,
caption = "PLease enter the changes by double clicking the cell",
editable = 'cell')
})
})
}
)
There isn't a 'make' variable in the data. I guess you refer to the first word of the row name as the make of the car. Then the strings you entered could be matched with the make of the car.
server = function(input, output,session) {
data <- observeEvent(input$show_table,{
brand <- word(rownames(df), 1)
text_input <- strsplit(input$text_input, ",")[[1]]
df_sub <- df[brand %in% text_input, input$somevalue]
output$summary <- renderPrint({
summary(df_sub)
})
output$table <- DT::renderDT({
datatable(df_sub,
caption = "PLease enter the changes by double clicking the cell",
editable = 'cell')
})
output$test <- renderText({
text_input
})
})}
I'm trying to click on a category in a pie chart built with highcharts and use the category to filter data in a line chart in R shiny app.
You can capture the click using the hc_plotOptions settings, like so:
library(shiny)
library(highcharter)
ui <- fluidPage(
column(3,
highchartOutput("hcontainer",height = "300px")
),
column(3,
textOutput("clicked")
)
)
server <- function(input, output){
click_js <- JS("function(event) {Shiny.onInputChange('pieclick',event.point.name);}")
output$hcontainer <- renderHighchart({
highchart() %>%
hc_chart(type = "pie") %>%
hc_add_series(data = list(
list(y = 3, name = "cat 1"),
list(y = 4, name = "dog 11"),
list(y = 6, name = "cow 55"))) %>%
hc_plotOptions(series = list(events = list(click = click_js)))
})
output$clicked <- renderText({
input$pieclick
})
}
shinyApp(ui, server)
Following the previous answer from #porkChop you can also add below code to your hc_plotOptions in order to get a selection visualization.
hc_plotOptions(
series = list(
stacking = FALSE, allowPointSelect = TRUE ,events = list(click = click_js))
)
I am currently working on a project aiming to create an interface which can do statistical analysis. A good reference to my goal would be something like the following website: https://rich.shinyapps.io/regression/
The issues I have regard a reactive Regression. The user is supposed to choose data input which will then be used in a Regression. Unfortunately I have to create subsets of the data frame before being able to process the input due to the conception of the data...
This input is stored in the variables X, Y and Z and can be shown using "paste", but the regression doesn't work.enter code here
Any suggestions?
library("shiny")
ui <- fluidPage((pageWithSidebar(
headerPanel("Dynamic Analysis"),
sidebarPanel(
selectInput (
inputId = "Country", label = "Choose a country", choices = c(levels(eurostat$GEO))
),
selectInput (
inputId = "Indice1", label = "Choose a dependent variable X", choices = c(levels(eurostat$INDIC_NA), 1)
),
selectInput (
inputId = "Indice2", label = "Choose an independent variable Y", choices = c(levels(eurostat$INDIC_NA), 1)
),
selectInput (
inputId = "Indice3", label = "Choose an independent variable Z", choices = c(levels(eurostat$INDIC_NA), 1)
),
selectInput (
inputId = "Unit1", label = "Choose a Unit", choices = c(levels(eurostat$UNIT), 1)
)
),
mainPanel(tableOutput("regTab"),
textOutput("test")
)
# mainPanel("Table",tableOutput="table")
)))
#This function is used to subset the desired data from the dataset
subsetting_num = function(Country, Indice, Unit, npar=TRUE,print=TRUE){
as.numeric(gsub(",", "" ,droplevels(subset(subset(subset(ES2, GEO== Country, Value!=0), INDIC_NA== Indice), UNIT == Unit)$Value)))
}
server <- shinyServer(function(input, output) {
X = reactive({subsetting_num(input$Country, input$Indice1, input$Unit1)})
Y = reactive({subsetting_num(input$Country, input$Indice2, input$Unit1)})
Z = reactive({subsetting_num(input$Country, input$Indice3, input$Unit1)})
# regression formula
runRegression <- reactive({
lm(X ~ Y + Z)
})
#Summary Regression
output$regTab <- renderTable({
if(!is.null(X)){
summary(runRegression())$coefficients
} else {
print(data.frame(Warning="Please select Model Parameters."))
}
})
#Depict the reactive values
output$test <- renderText({
paste("Subset", X())
})
})
shinyApp(ui = ui, server = server)
I am fairly new to DT in Shiny and would like to add text colour to specific columns in a table, I can do this using formatStyle as per the below example code chunk. However, I would also like to add the same text colour to the corresponding column name (header), is there an easy way to do this?
library(shiny)
library(DT)
ui = fluidPage(DT::dataTableOutput('fDataTable'))
server = function(input, output) {
output$fDataTable = DT::renderDataTable({
DT::datatable(iris) %>%
formatStyle(columns = 1, color = "red") %>%
formatStyle(columns = 3, color = "blue")
})
}
app = list(ui = ui, server = server)
runApp(app)
Any help would be greatly appreciated.
You can do this by adding CSS to the colnames of the table you are rendering (you also need to set escape to FALSE or the html will be escaped).
Here's an example:
library(shiny)
library(DT)
ui = fluidPage(DT::dataTableOutput('fDataTable'))
server = function(input, output) {
output$fDataTable = DT::renderDataTable({
iris_coloured <- iris
colnames(iris_coloured)[c(1,3)] <- paste0('<span style="color:',c("red","blue"),'">',colnames(iris)[c(1,3)],'</span>')
DT::datatable(iris_coloured,escape=F) %>%
formatStyle(columns = 1, color = "red") %>%
formatStyle(columns = 3, color = "blue")
})
}
app = list(ui = ui, server = server)
runApp(app)