I'm trying to access data from a Google spreadsheet and display it as a table in a Shiny app. After confirmation that the spreadsheet was accessed, the app continues to run without displaying anything. Printing the data to the console works, however.
server.R
library(shiny)
library(googlesheets)
shinyServer(function(input, output) {
sheet <- gs_title("Google Sheet")
data <- gs_read_csv(sheet)
output$table <- renderTable{
data
}
})
ui.R
library(shiny)
shinyUI(pageWithSidebar(
mainPanel(
dataTableOutput('table')
)
))
In server.Ruse renderDataTable({}) if you use dataTableOutput()
This code works:
library(shiny)
library(googlesheets)
server <- function(input, output) {
sheet <- gs_title("Google Sheet")
data <- gs_read_csv(sheet)
output$table <- renderDataTable({
data
})
}
ui <- fluidPage(sidebarLayout(sidebarPanel("Test"),
mainPanel(dataTableOutput('table'))
)
)
shinyApp(ui = ui, server = server)
Related
I have a shiny application that lets the user filter data, eventually the user should be able to download the filtered data but I cannot access the filtered/shown data from reactable.
An MWE would be the following: (Note that the getReactableState() function does not return the filtered data but would work if one had to select all filtered data.)
library(shiny)
library(reactable)
ui <- fluidPage(
reactableOutput("table"),
verbatimTextOutput("table_state")
)
server <- function(input, output) {
output$table <- renderReactable({
reactable(iris, filterable = TRUE)
})
output$table_state <- renderPrint({
print(getReactableState("table")) #< wrong code here...
# the goal would be to get the rows which are currently shown here
})
}
shinyApp(ui, server)
Not a full answer, but at least it allows to download the filtered data as a CSV (solution from here):
tags$button("Download as CSV", onclick = "Reactable.downloadDataCSV('table')")
The full solution looks like this:
library(shiny)
library(reactable)
ui <- fluidPage(
tags$button("Download as CSV", onclick = "Reactable.downloadDataCSV('table')"),
reactableOutput("table")
)
server <- function(input, output) {
output$table <- renderReactable({
reactable(iris, filterable = TRUE)
})
}
shinyApp(ui, server)
I'm new to shiny, so don't mind me if my question is simple.
I want to take a path as an input from the user and generate the data frame. I've done this so far:
library(shiny)
ui <- fluidPage(
textInput("data_path", "Please enter the path of your data: ")
tableOutput("data_glimpse")
)
server <- function(input, output){
data <- read.csv(input$data_path)
output$data_glimpse <- renderTable({
glimpse(data)
})
}
shinyApp(ui = ui, server = server)
But it's not working right. I don't get any pages to enter my path!
Any help?
I think it is easier to upload the file directly. But if you want to keep this structure, you can try the following. To make it work you have to add to your path the name of the file plus .csv, e.g. /sample.csv
library(shiny)
ui <- fluidPage(
textInput("data_path", "Please enter the path of your data: "),
tableOutput("data_glimpse")
)
server <- function(input, output){
dataTable <- reactive({
data <- read.csv(input$data_path)
})
output$data_glimpse <- renderTable({
dplyr::glimpse(dataTable())
})
}
shinyApp(ui = ui, server = server)
I am trying to build a shiny application where a user can input a date through the shiny application to then to trigger an R script to leverage that date.
I was wondering how do you link the date input from shiny to be leverage in an R script?
function(input, output) {
# You can access the value of the widget with input$date, e.g.
output$value <- renderPrint({ input$date })
}
Here's a minimal working example:
library(shiny)
ui <- fluidPage(
dateRangeInput("date","Date:"),
textOutput("value")
)
server <- function(input, output) {
output$value <- renderPrint({
x <- input$date
# script using x goes here
paste("choice:",x[1],"to",x[2]) # example output
})
}
shinyApp(ui = ui, server = server)
I'm new to R-shiny and I'm trying to develop an R-shiny application that would allow the user to upload an XML file, view the table as a data frame, and then download it as a CSV.
I've so far had no luck anywhere trying to find info on parsing XML's into R-shiny. Below is some code that I found from someone else's post on here which allows you to read in an XML and it will display the raw text, but I'm looking to get a proper data frame so that I can do some analysis on the data that the user uploads.
library(shiny)
ui <- fluidPage(
fileInput("File", "Choose file"),
tableOutput("Data")
)
server <- function(input, output, session) {
Data <- eventReactive(input$File, {
read_xml(input$File$datapath)
})
output$Data <- renderTable({
head(xml_text(Data()))
})
}
shinyApp(ui, server)
The code to do what I need in R is very simple, but converting this over to R-shiny is causing me lots of trouble.
data <- xmlParse("C:/filepath/data.xml")
df <- xmlToDataFrame(data, nodes =getNodeSet(data, "//nm:Row",
namespaces=c(nm = "urn:schemas-microsoft-com:office:spreadsheet")))
Can anyone help please?
Thanks in advance!
You can use package XMLthe same way you do in base R:
library(shiny)
library(XML)
ui <- fluidPage(
fileInput("File", "Choose file"),
tableOutput("Data")
)
server <- function(input, output, session) {
df <- eventReactive(input$File, {
xmlToDataFrame(input$File$datapath)
})
output$Data <- renderTable( head( df()))
}
shinyApp(ui, server)
Update: I've managed to solve the issue by writing a function that converts the "table" node of the xml into a data frame, and calling this function from within the server.
converter <- function(xml_data){
data <- xmlParse(xml_data)
df <- xmlToDataFrame(data, nodes =getNodeSet(data, "//nm:Row",
namespaces=c(nm = "urn:schemas-microsoft-com:office:spreadsheet")))
colnames(df) <- df[1,]
df <- df[-1, ]
return(df)
}
ui <- fluidPage(
fileInput("File", "Choose file"),
tableOutput("Data")
)
server <- function(input, output, session) {
df <- eventReactive(input$File, {
converter(input$File$datapath)
})
output$Data <- renderTable( head( df()))
}
shinyApp(ui, server)
I want to selectInput from reactive data frame column as code below but it is not showed anything:
library(shiny)
library(data.table)
ui <- fluidPage(
selectInput('region','Select region',choice=tableOutput('region'),selected=NULL)
)
server <- function(input, output, session){
data<- reactive(fread('murders.csv')) # this file contain 'region' column
output$region <- renderTable(data()$region)
}
shinyApp(ui = ui, server = server)
But when I read data outside server function (not reactive) the selectinput is working normal:
library(shiny)
library(data.table)
ui <- fluidPage(
selectInput('region','Select region',choice=data$region,selected=NULL)
)
data<- fread('murders.csv') # this file contain 'region' column
server <- function(input, output, session){
}
shinyApp(ui = ui, server = server)
I think it is better to read file in reactive mode in server function, could you show me how to select input from data column in reactive mode ?
Unless you are planning on changing the murder csv file, there is no need for it to be reactive, and it can be a global value. If you are keen on it not being a global you can transform the ui in a function and load the data inside.
Version 1
library(shiny)
library(data.table)
ui <- function(request){
data <- fread("murders.csv")
fluidPage(
selectInput('region','Select region',choice=data$region,selected=NULL)
)
}
server <- function(input, output, session){
}
shinyApp(ui = ui, server = server)
If you really want, by some reason, to load it inside the server what you are looking for is updateSelectInput. See version below
Version 2
library(shiny)
library(data.table)
ui <- fluidPage(
selectInput('region','Select region',choices=NULL, selected=NULL)
)
server <- function(input, output, session){
data <- fread("murders.csv")
updateSelectInput(session, "region", choices=data$region)
}
shinyApp(ui = ui, server = server)
And as I said there is no need for it to be reactive, but if you really want it to be reactive, you have to access the reactive inside a reactiveEnvironment, in this case observeEvent seems the most adequate:
Version 3
library(shiny)
library(data.table)
ui <- fluidPage(
selectInput('region','Select region',choices=NULL, selected=NULL)
)
server <- function(input, output, session){
data <- reactive(fread("murders.csv"))
observeEvent(data(),updateSelectInput(session, "region", choices=data()$region))
}
shinyApp(ui = ui, server = server)