I am creating R shiny app, when the user inputs values in the columns "schoolid," "userid," "class," "result," and "Date of the result / Remarks for fail" and clicks the button "Add," the related datable is displayed in "Tab2", it works perfectly at present.
I am looking for a solution to download the DataTable (from Tab2) to the .xpt file. Can someone help me with this.?
Note: I am asking this new inquiry after referring to my previous one.: How to change column values from date format to free-flowing text is dependent on "result" column in R shiny.
Code
library(shiny)
library(stringr)
library(shinydashboard)
library(tidyverse)
library(DT)
ui <- fluidPage(
fluidRow(tabsetPanel(id='tabs',
tabPanel("Tab1",
div(id = "form",
textInput("schoolId", label="SchoolId *" ),
selectInput("userId", label="UserId", choices = c("UserA", "UserB", "UserC"),selected = "UserA"),
textInput("class", label = "class"),
selectInput("result", label="result", choices = c("PASS", "FAIL" )),
dateInput("resultdate", value = NA, label = "Date of the result / Remarks for fail"
, format = "yyyy-mm-dd" )
),
actionButton("add", "Add")
),
tabPanel("Tab2",
tabPanel("View",
conditionalPanel("input.add != 0",
DTOutput("DT2"), hr(), downloadButton('downloadData', 'Download'))
)
)
)
)
)
server <- function(input, output, session) {
store <- reactiveValues()
observeEvent(input$add,{
new_entry <- data.frame(SCHOOLID=input$schoolId, USERID=input$userId
, CLASS= input$class
, RESULT=input$result,
RESULT_DATE = input$resultdate)
if("value" %in% names(store)){
store$value<-bind_rows(store$value, new_entry)
} else {
store$value<-new_entry
}
# If you want to reset the field values after each entry use the following two lines
for(textInputId in c("schoolId", "class")) updateTextInput(session, textInputId, value = "")
updateSelectInput(session, "userId", selected = "UserA")
updateSelectInput(session, "result", selected = "PASS")
updateDateInput(session, "resultdate")
})
output$DT2 <- renderDT({
store$value
})
}
shinyApp(ui, server)
CodePudding user response:
You could try write.xport
from SASxport
package. Try this
library(shiny)
library(stringr)
library(shinydashboard)
library(tidyverse)
library(DT)
library("SASxport")
ui <- fluidPage(
fluidRow(tabsetPanel(id='tabs',
tabPanel("Tab1",
div(id = "form",
textInput("schoolId", label="SchoolId *" ),
selectInput("userId", label="UserId", choices = c("UserA", "UserB", "UserC"),selected = "UserA"),
textInput("class", label = "class"),
selectInput("result", label="result", choices = c("PASS", "FAIL" )),
dateInput("resultdate", value = NA, label = "Date of the result / Remarks for fail"
, format = "yyyy-mm-dd" )
),
actionButton("add", "Add")
),
tabPanel("Tab2",
tabPanel("View",
conditionalPanel("input.add != 0",
DTOutput("DT2"), hr(), downloadButton('downloadData', 'Download'))
)
)
)
)
)
server <- function(input, output, session) {
store <- reactiveValues()
observeEvent(input$add,{
new_entry <- data.frame(SCHOOLID=input$schoolId, USERID=input$userId
, CLASS= input$class
, RESULT=input$result,
RESULT_DATE = input$resultdate)
if("value" %in% names(store)){
store$value<-bind_rows(store$value, new_entry)
} else {
store$value<-new_entry
}
# If you want to reset the field values after each entry use the following two lines
for(textInputId in c("schoolId", "class")) updateTextInput(session, textInputId, value = "")
updateSelectInput(session, "userId", selected = "UserA")
updateSelectInput(session, "result", selected = "PASS")
updateDateInput(session, "resultdate")
})
output$DT2 <- renderDT({
store$value
})
output$downloadData <- downloadHandler(
filename = paste0("mydata", ".xpt"),
content = function(file){
write.xport(store$value, file = file)
}
)
}
shinyApp(ui, server)